简体   繁体   English

与Twitter API结合使用时,如何在Watson Conversation API中传递response.context?

[英]How do I pass response.context back in Watson Conversation API when used in conjunction with Twitter API?

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; 我的想法是,我将使用Twitter的流式api来获取我的帐户的最新提及,将数据提供给watson convo api并让watson做出回应。 then I'll give that response to twitter again to reply to the person who mentioned me. 然后我将再次回复Twitter,以回复提到我的人。 In order for watson convo api to work properly, I'll have to pass back response.context to watson. 为了使Watson Convo api正常工作,我必须将response.context传回给Watson。

I've got everything down except for the passing back response.context part. 除了传回response.context部分外,我已经做好了一切。 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 请参阅Watson Conversation服务的消息方法的API文档-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. 消息的响应包含上下文对象,这是您在随后调用消息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. 您的应用程序将需要跟踪上下文对象,如果要允许多个同时对话,则需要能够将上下文对象映射到对话。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM