Node.js: How to send a direct message in Slack with MS Bot Framework?

孤街醉人 提交于 2019-12-12 22:46:56

问题


I am trying to create a Slack bot using LUIS where when the bot sees a greeting in a channel it is added to, it sends a direct message to the user that sent the greeting.

I have looked at Issue #431 and wrote a bot. Here is my code:

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}
        }, '/');
    }
]);

However, currently when the bot receives a greeting, it gives the following error message:

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)

(I've omitted part of directory)

I have looked at Issue #687 but still I couldn't figure out the problem. How can I make the bot work?

I am using Botbuilder v3.4.4 and Node v4.6.0.


回答1:


The way to go here I think is:

  1. Save the session.message.address somewhere as you will have to use it later in the bot.beginDialog you are doing.
  2. Before beginning the new dialog, you need to remove the conversation object as you want to create a new conversation
  3. Use the address to begin the dialog.

It would be something like

// 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,...

Take a look to the CreateNewConversation sample. You will see that something pretty similar is being done.



来源:https://stackoverflow.com/questions/42152599/node-js-how-to-send-a-direct-message-in-slack-with-ms-bot-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!