我需要做的是在回复某些内容后让我的机器人等待用户响应,以便他可以以结束messege的流程来回复示例:
用户:点击“入门”按钮
启动:我可以为您提供以下(付款)(选项2)(选项3)
用户:点击付款
Bot :您能输入帐单号码吗?
用户:3922509234
.............................
Bot :好的,谢谢,我们将处理您的付款
这是facebook应用与机器人通信的方式
app.post('/webhook', function(request, response) {
var data = request.body;
if(data.object == 'page'){
data.entry.forEach(function(pageEntry){
pageEntry.messaging.forEach(function (messagingEvent) {
if(messagingEvent.message){
console.log("MESSAGE---------------------> ",messagingEvent);
var messageInfo = functions.reciveMessage(messagingEvent);
console.log("MESSAGEINFO ", messageInfo);
evaluateMessage(messageInfo);
}
if(messagingEvent.postback){
console.log("POSTBACK--------------------> ",messagingEvent);
var postBackInfo = functions.recivePostBack(messagingEvent);
evaluatePostBack(postBackInfo);
}
});
});
response.sendStatus(200);
}
});
在此评估用户输入的关键字
function evaluateMessage(messageInfo, type) {
if(functions.isWordContain(messageInfo.senderMessage,'Pagar')||
functions.isWordContain(messageInfo.senderMessage,'pagar')||
functions.isWordContain(messageInfo.senderMessage,'Factura')||
functions.isWordContain(messageInfo.senderMessage,'factura')){
var message = functions.sendTextMessage(messageInfo.senderId, "Por
favor ingresa el numero de tu contrato");
callSendAPI(message);
}
}
messageInfo将检查是否包含关键字,然后将消息返回给用户。
我如何让机器人等待用户输入,以便他可以回复
我正在使用带有Express的Node.Js的机器人构建平台,请帮忙
答案 0 :(得分:0)
我找到了一种简单的方法,尽管将全局变量设为一个更好的方法
var verifier = {
origin:null,
state:false
};
然后当它进入消息验证器并使用某种令牌修改json并将状态设置为true
function evaluateMessage(messageInfo) {
if (functions.isWordContain(messageInfo.senderMessage,'Pagar')||
functions.isWordContain(messageInfo.senderMessage,'pagar')||
functions.isWordContain(messageInfo.senderMessage,'Factura')||
functions.isWordContain(messageInfo.senderMessage,'factura')){
var message = functions.sendTextMessage(messageInfo.senderId, "Por favor
ingresa el numero de tu contrato");
verifier.origin = 'pagoFactura';
verifier.state = true;
callSendAPI(message);
}
}
稍后消息输入的地方,我只是问该状态是否为真,哪个是起源,并取决于我是否发送消息
app.post('/webhook', function(request, response) {
var data = request.body;
if(data.object == 'page'){
data.entry.forEach(function(pageEntry){
pageEntry.messaging.forEach(function (messagingEvent) {
if(messagingEvent.message){
if(verifier.state == false){
console.log("MESSAGE---------------------> ",messagingEvent);
var messageInfo = functions.reciveMessage(messagingEvent);
console.log("MESSAGEINFO ", messageInfo);
evaluateMessage(messageInfo);
}else {
console.log("verifierOrigin ", verifier.origin);
var messageInfo = functions.reciveMessage(messagingEvent);
switch (verifier.origin) {
case 'pagoFactura':
var message = functions.sendTextMessage(messageInfo.senderId,
"Gracias su pago se a procesado exitosamente");
callSendAPI(message);
break;
default:
}
}
}
if(messagingEvent.postback){
console.log("POSTBACK--------------------> ",messagingEvent);
var postBackInfo = functions.recivePostBack(messagingEvent);
evaluatePostBack(postBackInfo);
}
});
});
response.sendStatus(200);
}
});
如果有人找到更好的方法,请也发表评论,我知道这不是仅用于测试目的的最佳方法