Node.js: 如何使用 MS Bot Framework 在 Slack 中发送直接消息?
Node.js: How to send a direct message in Slack with MS Bot Framework?
我正在尝试使用 LUIS 创建一个 Slack 机器人,当机器人在添加到的频道中看到问候语时,它会直接向发送问候语的用户发送一条消息。
我查看了 Issue #431 并编写了一个机器人。这是我的代码:
var builder = require('botbuilder');
var restify = require('restify');
// Setup Restify Server
var server = restify.createServer();
server.listen(process.env.port || process.env.PORT || 3978, function () {
console.log("%s listening to %s", server.name, server.url);
});
server.get(/.*/, restify.serveStatic({
'directory': '.',
'default': 'index.html'
}));
// Create Chat Bot
var connector = new builder.ChatConnector({
appId: process.env.MICROSOFT_APP_ID,
appPassword: process.env.MICROSOFT_APP_PASSWORD
});
var bot = new builder.UniversalBot(connector, {
persistConversationData: true // need persistent data for dictionary
});
server.post('/api/messages', connector.listen());
// Create LUIS recognizer that points at our model and add it as the root '/' dialog
var model = (omitted);
var recognizer = new builder.LuisRecognizer(model);
var dialog = new builder.IntentDialog({ recognizers: [recognizer] });
bot.dialog('/', dialog);
// Add intent handlers
dialog.matches('Greeting', [
function(session, args, next) {
var language = builder.EntityRecognizer.findEntity(args.entities, 'Language');
next({ response: language });
},
function(session, results) {
bot.beginDialog({
text: 'Hello',
to: {channelId: "emulator", address:"User1", id:"(omitted)", isBot:false},
from: { channelId:"emulator", address:"Bot1", id:"(omitted)", isBot:true}
}, '/');
}
]);
但是,目前当机器人收到问候语时,它会给出以下错误消息:
ERROR: ChatConnector: startConversation - address is invalid.
Error: Invalid address.
at ChatConnector.startConversation (C:\..\node_modules\botbuilder\lib\bots\ChatConnector.js:173:18)
at C:\..\node_modules\botbuilder\lib\bots\UniversalBot.js:308:27
at UniversalBot.tryCatch (C:\..\node_modules\botbuilder\lib\bots\UniversalBot.js:381:13)
at UniversalBot.ensureConversation (C:\..\node_modules\botbuilder\lib\bots\UniversalBot.js:302:14)
at C:\..\node_modules\botbuilder\lib\bots\UniversalBot.js:163:19
at C:\..\node_modules\botbuilder\lib\bots\UniversalBot.js:337:53
at UniversalBot.tryCatch (C:\..\node_modules\botbuilder\lib\bots\UniversalBot.js:381:13)
at C:\..\node_modules\botbuilder\lib\bots\UniversalBot.js:337:23
at UniversalBot.tryCatch (C:\..\node_modules\botbuilder\lib\bots\UniversalBot.js:381:13)
at UniversalBot.lookupUser (C:\..\node_modules\botbuilder\lib\bots\UniversalBot.js:324:14)
(我省略了部分目录)
我已经查看了问题 #687,但我仍然无法找出问题所在。我怎样才能让机器人工作?
我正在使用 Botbuilder v3.4.4 和 Node v4.6.0。
我认为去这里的方法是:
- 将
session.message.address
保存在某个地方,因为稍后您将不得不在您正在做的 bot.beginDialog
中使用它。
- 在开始新对话之前,您需要删除对话对象,因为您要创建新对话
- 使用地址开始对话。
会是这样的
// consider making this an array insted
var address
// probably in the function that matches the greeting
address = session.message.address;
// in the step where you want to send the direct messsage
var newConversationAddress = Object.assign({}, address);
delete newConversationAddress.conversation;
// begin dialog with address without conversation
bot.beginDialog(newConversationAddress,...
查看 CreateNewConversation 示例。您会看到正在做一些非常相似的事情。
我正在尝试使用 LUIS 创建一个 Slack 机器人,当机器人在添加到的频道中看到问候语时,它会直接向发送问候语的用户发送一条消息。
我查看了 Issue #431 并编写了一个机器人。这是我的代码:
var builder = require('botbuilder');
var restify = require('restify');
// Setup Restify Server
var server = restify.createServer();
server.listen(process.env.port || process.env.PORT || 3978, function () {
console.log("%s listening to %s", server.name, server.url);
});
server.get(/.*/, restify.serveStatic({
'directory': '.',
'default': 'index.html'
}));
// Create Chat Bot
var connector = new builder.ChatConnector({
appId: process.env.MICROSOFT_APP_ID,
appPassword: process.env.MICROSOFT_APP_PASSWORD
});
var bot = new builder.UniversalBot(connector, {
persistConversationData: true // need persistent data for dictionary
});
server.post('/api/messages', connector.listen());
// Create LUIS recognizer that points at our model and add it as the root '/' dialog
var model = (omitted);
var recognizer = new builder.LuisRecognizer(model);
var dialog = new builder.IntentDialog({ recognizers: [recognizer] });
bot.dialog('/', dialog);
// Add intent handlers
dialog.matches('Greeting', [
function(session, args, next) {
var language = builder.EntityRecognizer.findEntity(args.entities, 'Language');
next({ response: language });
},
function(session, results) {
bot.beginDialog({
text: 'Hello',
to: {channelId: "emulator", address:"User1", id:"(omitted)", isBot:false},
from: { channelId:"emulator", address:"Bot1", id:"(omitted)", isBot:true}
}, '/');
}
]);
但是,目前当机器人收到问候语时,它会给出以下错误消息:
ERROR: ChatConnector: startConversation - address is invalid.
Error: Invalid address.
at ChatConnector.startConversation (C:\..\node_modules\botbuilder\lib\bots\ChatConnector.js:173:18)
at C:\..\node_modules\botbuilder\lib\bots\UniversalBot.js:308:27
at UniversalBot.tryCatch (C:\..\node_modules\botbuilder\lib\bots\UniversalBot.js:381:13)
at UniversalBot.ensureConversation (C:\..\node_modules\botbuilder\lib\bots\UniversalBot.js:302:14)
at C:\..\node_modules\botbuilder\lib\bots\UniversalBot.js:163:19
at C:\..\node_modules\botbuilder\lib\bots\UniversalBot.js:337:53
at UniversalBot.tryCatch (C:\..\node_modules\botbuilder\lib\bots\UniversalBot.js:381:13)
at C:\..\node_modules\botbuilder\lib\bots\UniversalBot.js:337:23
at UniversalBot.tryCatch (C:\..\node_modules\botbuilder\lib\bots\UniversalBot.js:381:13)
at UniversalBot.lookupUser (C:\..\node_modules\botbuilder\lib\bots\UniversalBot.js:324:14)
(我省略了部分目录)
我已经查看了问题 #687,但我仍然无法找出问题所在。我怎样才能让机器人工作?
我正在使用 Botbuilder v3.4.4 和 Node v4.6.0。
我认为去这里的方法是:
- 将
session.message.address
保存在某个地方,因为稍后您将不得不在您正在做的bot.beginDialog
中使用它。 - 在开始新对话之前,您需要删除对话对象,因为您要创建新对话
- 使用地址开始对话。
会是这样的
// consider making this an array insted
var address
// probably in the function that matches the greeting
address = session.message.address;
// in the step where you want to send the direct messsage
var newConversationAddress = Object.assign({}, address);
delete newConversationAddress.conversation;
// begin dialog with address without conversation
bot.beginDialog(newConversationAddress,...
查看 CreateNewConversation 示例。您会看到正在做一些非常相似的事情。