本文档介绍怎么完成qabot对接任意第三方api服务
一、概述
首要这不是qabot的才能,而是微信敞开对话渠道的功用,在这里只是供给一个对接计划,完成思路:
1.运用云渠道供给的函数核算供给的http触发器来完成一个连接器(接口转换)
2.将连接器的https地址注册到简略问答或高级问答的的hook中
1.1 事务完成流程计划
1.接纳对话渠道推送的音讯
2.解析音讯中的用户仅有标识和原始问题
3.调用第三方api服务回来内容
4.将内容重新打包后回来给微信对话敞开渠道
1.2 会话对接样例
机器人对接回复音讯的规范音讯格式
1.2.1 H5恳求
{
"ans_node_id":25875176,
"ans_node_name":"Moments",
"answer":"打印一张你的相片放在口袋,今日我便是身藏巨款的人,你最宝贵",
"answer_open":1,
"answer_type":"text",
"confidence":1,
"create_time":"1670173803685",
"from_user_name":"o9U-85gMu2xUm5B5G2sA0WRxn9b0",
"msg_id":"8650151a",
"request_id":1530536540,
"skill_id":"",
"skill_name":"Moments",
"status":"FAQ",
"title":"说句情话",
"to_user_name":"157787",
"msgtype":"callback"
}
1.2.2 机器人恳求
{
"ans_node_id":25875186,
"ans_node_name":"通用问题",
"answer":"说不清为什么爱你,但你便是我不爱别人的理由。",
"answer_open":1,
"answer_type":"text",
"confidence":1,
"create_time":"1670174749822",
"from_user_name":"tyutluyc/5854608913@chatroom",
"msg_id":"62643ff8",
"request_id":50529027,
"skill_id":"",
"skill_name":"通用问题",
"status":"FAQ",
"title":"来呀情话",
"to_user_name":"5jr7aQC0iN3m97szkMcmAsdyae4u8B",
"msgtype":"callback"
}
1.3 核心逻辑
基于node.js的完成,其他语言完成可参考逻辑
const nlu = body.nlu
const text = nlu.query
const userid = nul.from_user
const res = 调用api
const answer = res.xxx
nul.anwser = anwser
三、示例代码
对话渠道简略问答对接自定义API
3.1 微信对话渠道推送到自定义服务音讯的body
{
"touser":"",
"nlu":{
"ans_node_id":25875176,
"ans_node_name":"Moments",
"answer":"打印一张你的相片放在口袋,今日我便是身藏巨款的人,你最宝贵",
"answer_open":1,
"answer_type":"text",
"article":"",
"bid_stat":{
"curr_time":"20221205-01:07:24",
"err_msg":"",
"latest_time":"20221205-01:07:24",
"latest_valid":true,
"up_ret":0
},
"confidence":1,
"create_time":"1670173803685",
"dialog_session_status":"NONE",
"dialog_status":"NONE",
"event":"",
"from_user_name":"o9U-85gMu2xUm5B5G2sA0WRxn9b0",
"intent_confirm_status":"",
"is_default_answer":false,
"list_options":false,
"msg":[
{
"ans_node_id":25875176,
"ans_node_name":"Moments",
"article":"",
"confidence":1,
"content":"",
"debug_info":"",
"event":"",
"list_options":false,
"msg_type":"text",
"opening":"",
"request_id":1530536540,
"resp_title":"说句情话",
"scene_status":"",
"session_id":"b86dbb39f1b644b48157f566710563e2",
"status":"FAQ",
"take_options_only":false
}
],
"msg_id":"8650151a",
"opening":"",
"request_id":1530536540,
"ret":0,
"scene_status":"",
"session_id":"b86dbb39f1b644b48157f566710563e2",
"skill_id":"",
"skill_name":"Moments",
"slot_info":[
],
"slots_info":[
],
"status":"FAQ",
"take_options_only":false,
"title":"说句情话",
"to_user_name":"157787",
"msgtype":"callback"
},
"query":"说句情话"
}
3.2 自定义接口回来音讯时只需要将赋值 body.answer 改变原始音讯的answer
exports.handler = async (event, context, callback) => {
const ctx = event
console.debug(ctx)
const params = ctx.pathParameters
console.debug(params)
const queryStringParameters = ctx.queryStringParameters || {}
const HOST = 'api.vvhan.com'
const qs = queryStringParameters
const body = {}
const method = 'GET'
const path = `/api/love`
let headers = {}
var uri = 'http://' + HOST + path
let opt = {
method,
uri,
qs,
body,
headers,
json: true
}
console.debug(opt)
try {
let res = await rp(opt)
console.debug(res)
let body = JSON.parse(ctx.body)
body.nlu.answer = res
const nlu = body.nlu
console.debug(nlu)
const msg = {
"isBase64Encoded": false,
"statusCode": 200,
"headers": {},
"body": JSON.stringify(nlu)
}
callback(null, msg);
}
catch (err) {
callback(null, err);
}
};
3.3 回来的音讯body到微信
"ans_node_id":25875176,
"ans_node_name":"Moments",
"answer":"打印一张你的相片放在口袋,今日我便是身藏巨款的人,你最宝贵",
"answer_open":1,
"answer_type":"text",
"article":"",
"bid_stat":{
"curr_time":"20221205-01:07:24",
"err_msg":"",
"latest_time":"20221205-01:07:24",
"latest_valid":true,
"up_ret":0
},
"confidence":1,
"create_time":"1670173803685",
"dialog_session_status":"NONE",
"dialog_status":"NONE",
"event":"",
"from_user_name":"o9U-85gMu2xUm5B5G2sA0WRxn9b0",
"intent_confirm_status":"",
"is_default_answer":false,
"list_options":false,
"msg":[
{
"ans_node_id":25875176,
"ans_node_name":"Moments",
"article":"",
"confidence":1,
"content":"",
"debug_info":"",
"event":"",
"list_options":false,
"msg_type":"text",
"opening":"",
"request_id":1530536540,
"resp_title":"说句情话",
"scene_status":"",
"session_id":"b86dbb39f1b644b48157f566710563e2",
"status":"FAQ",
"take_options_only":false
}
],
"msg_id":"8650151a",
"opening":"",
"request_id":1530536540,
"ret":0,
"scene_status":"",
"session_id":"b86dbb39f1b644b48157f566710563e2",
"skill_id":"",
"skill_name":"Moments",
"slot_info":[
],
"slots_info":[
],
"status":"FAQ",
"take_options_only":false,
"title":"说句情话",
"to_user_name":"157787",
"msgtype":"callback"
}