1 В избранное 0 Ответвления 0

OSCHINA-MIRROR/stonelf-looking4help

Клонировать/Скачать
webhook 2.5 КБ
Копировать Редактировать Web IDE Исходные данные Просмотреть построчно История
emu Отправлено 12.02.2020 21:30 7f593e9
'use strict';
const app = require("tcb-admin-node");
exports.main = async (event, context, callback) => {
if (event.httpMethod != 'POST') {
console.log(JSON.stringify(event))
return {
'isBase64Encoded': false,
'statusCode' : 403,
'headers' : {'Content-Type':'text/html; charset=utf-8'},
'body' : 'invalid_request_method'
};
}
// 从 body 中获取数据
var data = null;
try {
data = JSON.parse(event.body);
console.log(JSON.stringify(data))
}catch(e){
console.log(JSON.stringify(event))
return {
'isBase64Encoded': false,
'statusCode' : 403,
'headers' : {'Content-Type':'text/html; charset=utf-8'},
'body' : 'invalid_data_format'
};
}
if (data) {
let json = {
"answer_id":data.payload.answer_id,
"qq":data.payload.qq,
"weixin_openid":data.payload.weixin_openid,
"source":getAnswer(data,0,"q-1-yRh2","text"),
"attachment":getAnswer(data,0,"q-10-NYLq","file_name_src"),
"attachmentSrc":getAnswer(data,0,"q-10-NYLq","file_name_dst"),
"helpSeeker":getAnswer(data,0,"q-2-LUYh","text"),
"phoneOfHelpSeeker":getAnswer(data,0,"q-8-tvM9","text"),
"idOfHelpSeeker":getAnswer(data,0,"q-3-iLje","text"),
"family":getAnswer(data,0,"q-5-Qza9","text"),
"address":getAnswer(data,0,"q-4-4TUk","text"),
"phone":getAnswer(data,0,"q-7-Qq5r","text"),
"status":getAnswer(data,0,"q-9-zh9f","text"),
"datauirements":getAnswer(data,0,"q-6-LEh8","text")
}
// response 200
app.init({env: process.env.ENV})
const db = app.database();
const _ = db.command
let collection = db.collection(process.env.DB);
await collection.add(json);
return {
'isBase64Encoded': false,
'statusCode' : 200,
'headers' : {'Content-Type':'text/html; charset=utf-8'},
'body' : JSON.stringify(json)
};
}else{
console.log(JSON.stringify(event))
return {
'isBase64Encoded': false,
'statusCode' : 403,
'headers' : {'Content-Type':'text/html; charset=utf-8'},
'body' : 'invalid_data_format'
};
}
console.log("异常退出")
console.log(JSON.stringify(event));
};
function getAnswer(data,answerIndex,questionID,property){
var questions = data.payload.answer[answerIndex].questions;
for(var i=0;i<questions.length;i++){
if(questions[i].id==questionID){
return questions[i][property];
}
}
return null
}

Опубликовать ( 0 )

Вы можете оставить комментарий после Вход в систему

1
https://api.gitlife.ru/oschina-mirror/stonelf-looking4help.git
git@api.gitlife.ru:oschina-mirror/stonelf-looking4help.git
oschina-mirror
stonelf-looking4help
stonelf-looking4help
master