我正在开发一个聊天机器人,使用masterbot机器人,socket.io,watson-conversation。问题是,在我开始发送消息之前,我不能理解我需要做些什么来接收watson欢迎消息。现在我只是在发送一些文本后收到消息,我需要的是在发送任何消息之前收到第一条消息。有没有可能使用僵尸主机?
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>`);
});
发布于 2018-03-12 20:45:34
如您所见,您需要发送一个空消息来接收欢迎消息。
发布包含空消息的示例:

响应:

JSON Obs.:我的服务器需要一个,就像我的例子一样,这个配置将取决于你后端的代码,就像你的例子将是message对象中的inputValue.value一样,试着使用inputValue.value || ""。
有关使用Watson Conversation here.发送消息方法的详细信息,请参阅
https://stackoverflow.com/questions/49220972
复制相似问题