gpt4 book ai didi

javascript - js 上的 FB-bot、gupshup

转载 作者:行者123 更新时间:2023-12-03 05:37:39 25 4
gpt4 key购买 nike

我正在寻找异步调用函数问题的解决方案,特别是 gupshup api 中的 context.simplehttp.makePost(url, param, header); 。我需要按具体顺序发送多条消息。

这是代码:

function MessageHandler(context, event) {
if (event.messageobj.type == "msg") {
if (Array_CH(hi_array, message)) {
var contextParam = JSON.stringify(event.contextobj);
var url = "https://api.gupshup.io/sm/api/bot/" + event.botname + "/msg";
var param = "context=" + contextParam + "&message=";
var header = { "apikey": "b3843dad0a484ad3ca19b5358be6c809", "Content-Type": "application/x-www-form-urlencoded" };
var catalogue;
var button;
var param1;
param = "context=" + contextParam + "&message=Hey.\nContest called The vivid Emotions needs your assessment.\nLook what emotions here. Do you agree?\nOr are your feelings totally different?";
context.simplehttp.makePost(url, param, header);
catalogue = {
"type": "catalogue",
"msgid": "cat_212",
"items": [
{
"title": "Design1",
"subtitle": "Quisque consectetur ornare malesuada.\n Nunc dapibus nec felis.",
"imgurl": "http://sv777.ru/images/stati/239465865852987356diz.jpg",
"options": [
{
"type": "url",
"title": "About",
"url": "http://laminat-parket.net/product/room-yasen-dizajn"
}
]
},
{
"title": "Design2",
"subtitle": "Donec purus sem, mollis a fermentum sit amet, pretium sed lectus.\n Ut at mattis purus.",
"imgurl": "http://laminat-parket.net/upload/productions/55794d8198b65.jpg",
"options": [
{
"type": "url",
"title": "About",
"url": "http://laminat-parket.net/product/room-yasen-dizajn"
}
]
},
{
"title": "Design3",
"subtitle": "Proin nec turpis leo.\n Nam metus augue, aliquam vitae laoreet sed, dignissim a risus.",
"imgurl": "http://www.askwallpapers.com/pic/201502/2880x1800/askwallpapers.com-17168.jpg",
"options": [
{
"type": "url",
"title": "About",
"url": "http://laminat-parket.net/product/room-yasen-dizajn"
}
]
},
{
"title": "Design4",
"subtitle": "Phasellus vitae justo sed velit faucibus hendrerit.\n Aenean scelerisque eros ac tortor tincidunt feugiat.",
"imgurl": "http://www.kidsworldstudio.net/wp-content/uploads/2015/11/bunk-bed-plans-for-kids-bedroom-ideas-home-interior-design-3353-loft-bed-for-kids-loft-bed-for-kids1.jpg",
"options": [
{
"type": "url",
"title": "About",
"url": "http://laminat-parket.net/product/room-yasen-dizajn"
}
]
},
{
"title": "Design5",
"subtitle": "Pellentesque risus nibh, porttitor vitae diam ut, facilisis aliquet velit.\n Quisque scelerisque massa urna. ",
"imgurl": "http://www.wallpaper4k.com/download.php?res=interior%2FWP67USXQ.jpg",
"options": [
{
"type": "url",
"title": "About",
"url": "http://laminat-parket.net/product/room-yasen-dizajn"
}
]
}
]
};
button = {
"msgid": "bt_2",
"type": "survey",
"question": "Put your Like and change this rating!",
"options": [
{
"type": "url",
"title": "Yes",
"url": "http://www.wallpaper4k.com/uk/wallpapers/interior%20design/wp67usxq.html",
"webview_height_ratio": "full"
},
{
"type": "url",
"title": "Add your own project",
"url": "http://www.wallpaper4k.com/uk/wallpapers/interior%20design/wp67usxq.html",
"webview_height_ratio": "full"
},
{
"type": "text",
"title": "Need own bot?",
}
]
};
param = "context=" + contextParam + "&message=" + JSON.stringify(catalogue);
param1 = "context=" + contextParam + "&message=" + JSON.stringify(button);
context.simplehttp.makePost(url, param, header);
context.simplehttp.makePost(url, param1, header);
return;
}
}
}

但每次发送的消息都是随机顺序的。

最佳答案

要按具体顺序发送多条消息,您需要先让一个 HTTP 调用完成其执行,然后再进行第二次调用。使用 Gupshup 的 IDE 进行 HTTP 调用时,有两种方法可以处理异步行为 -

  1. 使用默认的 HttpResponseHandler。每次 HTTP 调用执行完成时都会调用此处理程序。

  2. 在进行 HTTP 调用时定义您自己的回调函数。喜欢 -context.simplehttp.makePost(url, formParams, headers, function)

我建议使用第二个选项,因为它可以更轻松地处理多个 HTTP 调用。

使用 #2 按顺序发送 3 条消息的伪代码是 -

function MessageHandler(context, event) {
context.simplehttp.makePost(url, formParams, headers, secondMsg);
//sending 1st message
}
function secondMsg(event,context){
context.simplehttp.makePost(url, formParams, headers, thirdMsg); //sending 2nd message
}
function thirdMsg(event,context){
context.sendResponse("I am the last message");
}

注意 - 使用context.sendResponse发送消息并退出代码执行,直到机器人收到下一条消息。

有关更多详细信息,请查看document在 gupshup.io 上

关于javascript - js 上的 FB-bot、gupshup,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40662843/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com