Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
280 views
in Technique[技术] by (71.8m points)

node.js - Microsoft Bot framework: Sending Message on connect

I'm new with Microsoft Bot framework. Right now I'm testing my code on Emulator. I want to send Hello message as soon as your connect. Following is my code.

var restify = require('restify');
var builder = require('botbuilder');

var server = restify.createServer();
server.listen(process.env.port || process.env.PORT || 3978, function () {
   console.log('%s listening to %s', server.name, server.url); 
});

var connector = new builder.ChatConnector({
   appId: "-- APP ID --",
   appPassword: "-- APP PASS --"
});
var bot = new builder.UniversalBot(connector);
server.post('/api/message/',connector.listen());

bot.dialog('/', function (session) {
    session.send("Hello");
    session.beginDialog('/createSubscription');
});

Above code sends Hello message when user initiate a conversation. I want to send this message as soon as user connects.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Hook into the conversationUpdate event and check when the bot is added. After that, you can just post a message or begin a new dialog (as in the code below I extracted from the ContosoFlowers Node.js sample, though there are many others doing the same).

// Send welcome when conversation with bot is started, by initiating the root dialog
bot.on('conversationUpdate', function (message) {
    if (message.membersAdded) {
        message.membersAdded.forEach(function (identity) {
            if (identity.id === message.address.bot.id) {
                bot.beginDialog(message.address, '/');
            }
        });
    }
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...