这是我的代码
var isInput = false;
app.post('/webhook', function (req, res) {
var data = req.body;
if (data.object === 'page') {
data.entry.forEach(function(entry) {
var pageID = entry.id;
var timeOfEvent = entry.time;
entry.messaging.forEach(function(event) {
if (event.message) {
receivedMessage(event);
} else if(event.postback){
receivedPostback(event);
}
else {
console.log("Webhook received unknown event: ", event);
}
});
});
res.sendStatus(200);
}
});
function receivedMessage(event) {
var senderID = event.sender.id;
var recipientID = event.recipient.id;
var timeOfMessage = event.timestamp;
var message = event.message;
console.log("Received message for user %d and page %d at %d with message:",
senderID, recipientID, timeOfMessage);
console.log(JSON.stringify(message));
var messageId = message.mid;
var messageText = message.text;
var messageAttachments = message.attachments;
if (messageText) {
switch (messageText) {
case 'Vote':
delayText(senderID, "Send your message ", 1000);
isInput = true;
break;
default:
if(isInput){
token = messageText;
sendTextMessage(senderID, "token: " + token)
isInput = false;
} else {
sendTextMessage(senderID, "isInput: " + isInput)
}
break;
}
}
}
我想执行
if (isInput) {
token = messageText;
sendTextMessage(senderID, "token: " + token)
isInput = false
}
在默认情况下,但它永远不会起作用。
正如您在上面的代码中看到的,如果用户发送消息“投票”,我的聊天机器人将回复“发送您的消息”并将isInput更新为true。但是当我再次向机器人发送消息时,它说isInput
是错误的。我应该如何让isInput
更新为真?
答案 0 :(得分:1)
每次调用receivedMessage()方法时,它都会将isInput变量设置为false。 试试这段代码
{{1}}
答案 1 :(得分:1)
此代码永远不会执行,因为isInput
始终为false
。查看switch语句上方的代码。
...
isInput = false; // this is always reseting isInput to false
...
if (isInput) {
token = messageText;
sendTextMessage(senderID, "token: " + token)
isInput = false;
}