Here is something that I have setup for an FB Bot, so you will need to change the recipientId mechanism to suit your needs.
// 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);
});
}
}