我很难理解Node.js SDK何时触发操作。 起初,我认为仅当我们使用故事网页明确地调用它时才会触发操作。
我刚才意识到情况并非如此。
例如,当用户的上下文没有被正确清除时,可以运行某些动作而不是另一个动作(或者有时可以针对单个消息运行两个动作)。即使某些行为不是当前故事的一部分。
在Node.js SDK中触发操作的条件是什么?
答案 0 :(得分:1)
这是我为FB Bot设置的内容,因此您需要更改recipientId机制以满足您的需求。
// CALL THIS TO SEND MESSAGES TO WIT
function sendToWit(sessionId, messageText) {
// This will run all actions until nothing left to do
wit.runActions(sessionId, // Current session
messageText, sessions[sessionId].context // Current session state
).then(function (context) {
// Waiting for further messages to proceed.
if (context['done']) {
delete sessions[sessionId];
}
// Updating current session state
sessions[sessionId].context = context;
}).catch(function (err) {
console.error('WIT ERROR MSG: ', err.stack || err);
});
}
//Wit Actions
var actions = {
// Basic text message response
send: function send(request, response) {
var sessionId = request.sessionId,
context = request.context,
entities = request.entities,
recipientId = sessions[sessionId].fbid,
text = response.text;
return new Promise(function (resolve, reject) {
callMySendAPI(recipientId, text);
return resolve();
});
}
, // All of your custom actions will go here ie;
myCustomAction: function (request) {
console.log('myCustomAction Called');
var sessionId = request.sessionId;
var recipientId = sessions[sessionId].fbid;
return new Promise(function (resolve, reject) {
//Call some API and do lookup by recipientId, you could lookup by anything ie; context.city for weather lookup etc..
callSomeAPI(recipientId).then(function (result) {
context.something = result.something; //Parsed JSON
return Promise.resolve(context);
}).catch(error => {
// something went wrong
});
return resolve(context);
});
}
}
答案 1 :(得分:0)