The idea is I'll use Twitter's streaming api to grab the newest mentions of my account, feed that data to watson convo api and have watson spit out a response; then I'll give that response to twitter again to reply to the person who mentioned me. In order for watson convo api to work properly, I'll have to pass back response.context to watson.
I've got everything down except for the passing back response.context part. Does anyone have any idea how to do this?
var twitter = require('../statics/twitterAPI_KEY_Dev');
var watson = require('../statics/watsonAPI_KEY_Dev');
function botReplyInit() {
var stream = twitter.stream('statuses/filter', { track: '@Felicia_Bot' });
stream.on('tweet', function(tweet) {
var dataProcessed = false;
var data;
if (!dataProcessed) {
var data = tweet.text.replace(/@(\w+)/g, '').replace(/#(\w+)/g, '');
//cleans up the tweet so the @ and # are deleted
}
var username = tweet.user.screen_name;
var statusID = tweet.id_str;
var endConversation = false;
var error = {};
var res = { output: {}, intents: [] };
processWatson(error, res);
//how do we move this intialization outside of the stream.on function so it only
//get called once in the entire lifetime of the app??
function processWatson(err, response) {
if (err) {
console.log(err);
}
if (response.output.action === 'end_conversation') {
endConversation = true;
}
if (response.intents.length > 0) {
var result = '@' + username + ' ' + response.output.text[0];
twitter.post('statuses/update', {
status: result,
in_reply_to_status_id: tweet.id_str
},
function(err, response) {
if (err) {
console.log(err);
return;
}
else {
console.log('I posted ' + result);
}
});
}
if(!endConversation && !dataProcessed) {
watson.message({
input: { text: data },
context: response.context
},
processWatson);
dataProcessed = true;
}
}
})
}
botReplyInit();
referring to the API documentation for the message method of the Watson Conversation service - https://www.ibm.com/watson/developercloud/conversation/api/v1/#send_message
The Response from message contains the context object, which is what you need to return back to the service on subsequent invocations of the message API. On conversation initialisation the context object is not required.
Your application will need to keep a track of the context object, and if you are going to allow multiple simultaneous conversations you will need to be able to map context object to conversation.