botkit中间件 - 如何使用sendToWatson更新上下文?

时间:2018-01-15 08:28:52

标签: watson-conversation

我使用https://github.com/watson-developer-cloud/botkit-middleware#implementing-app-actions作为参考。 我的对话中的上下文不会更新。

这是我的bot-facebook.js。

function checkBalance(context, callback) {
   var contextDelta = {
     user_name: 'Henrietta',
     fname: 'Pewdiepie'
  };
  callback(null, context);
}
var checkBalanceAsync = Promise.promisify(checkBalance);
var processWatsonResponse = function (bot, message) {
  if (message.watsonError) {
    console.log(message.watsonError);
    return bot.reply(message, "I'm sorry, but for technical reasons I can't respond to your message");
  }
  if (typeof message.watsonData.output !== 'undefined') {
    //send "Please wait" to users
    bot.reply(message, message.watsonData.output.text.join('\n'));  

    if (message.watsonData.output.action === 'check_balance') {
      var newMessage = clone(message);
      newMessage.text = 'check new name';  

      checkBalanceAsync(message.watsonData.context).then(function (contextDelta) {
        console.log("contextDelta: " + JSON.stringify(contextDelta));
        return watsonMiddleware.sendToWatsonAsync(bot, newMessage, contextDelta);
    }).catch(function (error) {
      newMessage.watsonError = error;
    }).then(function () {
      return processWatsonResponse(bot, newMessage);
    });
   }
  }
 };
controller.on('message_received', processWatsonResponse);  

我的沃森谈话中欢迎节点的JSON编辑器。

{
  "context": {
    "fname": "",
    "user_name": ""
},
  "output": {
    "text": {
      "values": [
        "Good day :) My name is Doug and I am a chatbot."
      ],
      "selection_policy": "random"
    },
    "action": "check_balance"
  }
}  

我尝试过多种可以想象的方式。

我是否需要在json编辑器中执行fname: <?contextDelta.fname?>之类的操作?

1 个答案:

答案 0 :(得分:0)

您没有检查对话框中的上下文。

JSON编辑器中的上下文对象用于在上下文中存储捕获的数据, 所以你的节点实际上清空了上下文变量。 您可能需要从对话框中删除该上下文初始化,

要查看上下文变量的值,必须在输出中使用它 "Good day, $fname :) My name is Doug and I am a chatbot."