2 Star 11 Fork 3

emu/looking4help

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
webhook 2.50 KB
一键复制 编辑 原始数据 按行查看 历史
emu 提交于 2020-02-13 02:30 . 对接腾讯问卷的webhook
'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
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
HTML
1
https://gitee.com/stonelf/looking4help.git
git@gitee.com:stonelf/looking4help.git
stonelf
looking4help
looking4help
master

搜索帮助