【问题标题】:Bot Response based on user session using botframework (NodeJS)使用 botframework (NodeJS) 基于用户会话的机器人响应
【发布时间】:2017-11-17 10:58:58
【问题描述】:

如何在 NodeJS 中根据用户会话发送机器人响应。

我使用 botframework 创建了一个示例 NodeJS 应用程序,并连接到 IBM watson 来处理用户查询以获得适当的响应,并以 10 秒的延迟发回响应。

我已经生成了 ngrok URL 并为网络频道配置了它的机器人框架。当我在多个浏览器中同时进行测试时,我得到了相同的响应,而不是基于用户会话和用户请求。

它将最新的请求处理响应提供给每个用户。我已经复制了下面的示例 nodejs 代码。

有人可以查看我的代码并建议我如何根据用户会话/对话 ID 发送响应吗?

NodeJS sample code :

'use strict';

var restify = require('restify');
var builder = require('botbuilder');
var Conversation = require('watson-developer-cloud/conversation/v1'); 
require('dotenv').config({silent: true});

var server = restify.createServer();

var contexts = { CandidateID : "89798" , Location : "USA"  }
var workspace= 'XXXXXXXXXXXXXXXX';

let name,id,message,addr,watson;
var savedAddress;

server.listen(process.env.port || process.env.PORT || 3000, function () {
  console.log('%s listening to %s', server.name, server.url);
});

// Create the service wrapper
var conversation = new Conversation({
    username: 'XXXXXXXXXXXXXXXX',
    password: 'XXXXXXXXXXXXXXXX',
    url: 'https://gateway.watsonplatform.net/conversation/api',
    version_date: 'XXXXXXXXXXXXXXXX'
});

// setup bot credentials
var connector = new builder.ChatConnector({
  appId: 'XXXXXXXXXXXXXXXX',
  appPassword: 'XXXXXXXXXXXXXXXX'
});

var bot = new builder.UniversalBot(connector,);

server.post('/api/messages', connector.listen());

// root dialog
bot.dialog('/', function (session, args) {
	
    name = session.message.user.name;
	id = session.message.user.id;
	message = session.message.text;
	savedAddress = session.message.address;
		 
    var payload = {
        workspace_id: workspace,
        context: contexts,
        input: {
            text: session.message.text
        }
    };
	
    var conversationContext = {
        workspaceId: workspace,
        watsonContext: contexts
    };

    if (!conversationContext) {
        conversationContext = {};
    }

    payload.context = conversationContext.watsonContext;
		
    conversation.message(payload, function(err, response) {
        if (err) {
            console.log(err);
            session.send(err);
        } else {			
            console.log(JSON.stringify(response, null, 2));          
            conversationContext.watsonContext = response.context;
			watson = response.output.text;
        }
    });	
  
	console.log(message);
  
	setTimeout(() => {
		startProactiveDialog(savedAddress);
	}, 10000);
});

// handle the proactive initiated dialog
bot.dialog('/survey', function (session, args, next) {
  if (session.message.text === "done") {
    session.send("Great, back to the original conversation");
    session.endDialog();
  } else {
   session.send(id + ' ' + watson);
  }
});

// initiate a dialog proactively 
function startProactiveDialog(address) {
  bot.beginDialog(address, "*:/survey");
}

【问题讨论】:

  • 嗨@ganeshkumar,有更新吗?

标签: node.js botframework ibm-watson


【解决方案1】:

您需要确保它来回传递该用户会话的所有系统上下文。我们的 botkit 中间件基本上为您完成了这项工作。

https://github.com/watson-developer-cloud/botkit-middleware

【讨论】:

    【解决方案2】:

    您似乎正在利用https://github.com/Microsoft/BotBuilder-Samples/tree/master/Node/core-proactiveMessages/startNewDialog 的示例向特定用户发送基于对话的主动消息。

    示例仅供参考,它只保存了最新的转换,它只声明了一个变量savedAddress,每次新用户进来时,新的会话地址都会分配给savedAddress并覆盖旧值。

    您可以尝试定义一个对象来保存用户地址以便快速测试:

    var savedAddress = {};
    bot.dialog('/', function (session, args) {
    
      savedAddress[session.message.address.id] = session.message.address;
    
      var message = 'Hey there, I\'m going to interrupt our conversation and start a survey in a few seconds.';
      session.send(message);
    
      message = 'You can also make me send a message by accessing: ';
      message += 'http://localhost:' + server.address().port + '/api/CustomWebApi';
      session.send(message);
    
      setTimeout(() => {
        startProactiveDialog(savedAddress[session.message.address.id]);
      }, 5000);
    });
    

    对于生产阶段,您可以利用 Redis 或其他一些缓存存储来存储此地址列表。

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-08-13
      • 2017-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-07
      • 2021-10-05
      相关资源
      最近更新 更多