【问题标题】:Botmaster with socket.io and watson-conversation-ware带有 socket.io 和 watson-conversation-ware 的 Botmaster
【发布时间】:2018-03-11 14:14:58
【问题描述】:

我正在使用 masterbot、socket.io、watson-conversation 开发一个聊天机器人。问题是在开始发送消息之前,我无法理解接收 watson 欢迎消息需要做什么。现在我只是在发送一些文本后接收消息,我需要的是在发送任何消息之前接收第一条消息。是否可以使用botmaster?

const SessionWare = require('botmaster-session-ware');
const WatsonConversationWare = require('botmaster-watson-conversation-ware');
const express = require('express'); 
const port = process.env.PORT || 3000;
const app = express(); 

// Rota para index.html
app.use(express.static(__dirname + '/public')); 

const server = app.listen(port, '0.0.0.0', () => {  
    console.log('Server listening at port %d', port);
});

const Botmaster = require('botmaster');
const SocketioBot = require('botmaster-socket.io');

const botmaster = new Botmaster({
  server,
});

const socketioSettings = {
  id: 'SOME_BOT_ID_OF_YOUR_CHOOSING',
  server,
};

const socketioBot = new SocketioBot(socketioSettings);
botmaster.addBot(socketioBot);

const watsonConversationWareOptions = {
  settings: {
    username: ,
    password: ,
    url: 'https://gateway.watsonplatform.net/conversation/api/',
    version: 'v1',
    version_date: '2018-02-16'
  },
  workspaceId: 
}

// declaring middleware
const watsonConversationWare = WatsonConversationWare(watsonConversationWareOptions);
botmaster.use(watsonConversationWare);

botmaster.use({
  type: 'incoming',
  name: 'watson-middleware',
  controller: (bot, update) => {
    return bot.sendTextCascadeTo(update.watsonUpdate.output.text, update.sender.id);
  }
});

// This will make our context persist throughout different messages from the
// same user
const sessionWare = new SessionWare();
botmaster.useWrapped(sessionWare.incoming, sessionWare.outgoing);

botmaster.on('error', (bot, err) => { 
  console.log(err.stack); 
}); 

var socket = io('?botmasterUserId=wantedUserId');

var form = document.getElementById('form');
var textInput = document.getElementById('text-input');
var messages = document.getElementById('messages');

form.onsubmit = function(event) {
  event.preventDefault();
  if (!textInput.value) {
    return;
  }
  messages.insertAdjacentHTML('beforeend',
    `<li class="user-message">${textInput.value}</li>`);
  
  const update = {
    message: {
      text: textInput.value
    }
  };
  socket.send(update);
  textInput.value = '';
};

socket.on('message', function(botmasterMessage){
  var textMessage = botmasterMessage.message.text;

  messages.insertAdjacentHTML('beforeend',
    `<li class="botmaster-message">${textMessage}</li>`);
});

【问题讨论】:

    标签: javascript node.js socket.io chatbot watson-conversation


    【解决方案1】:

    如您所见,您需要发送一条空消息来接收欢迎消息

    带有空消息的 POST 示例:

    回应:

    Obs.: 我的服务器需要一个像我的示例一样的 JSON,此配置将取决于您在后端的代码,例如您的示例将是 message 中的 inputValue.value对象,请尝试使用inputValue.value || ""

    • 详细了解使用 Watson Conversation here 发送消息的方法。

    【讨论】:

      猜你喜欢
      • 2018-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-05
      相关资源
      最近更新 更多