【发布时间】: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