Microsoft Bot 框架:连接时发送消息

Microsoft Bot framework: Sending Message on connect

我是 Microsoft Bot 框架的新手。现在我正在模拟器上测试我的代码。我想在您连接后立即发送问候消息。以下是我的代码。

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

以上代码在用户发起对话时发送 Hello 消息。我想在用户连接后立即发送此消息。

挂钩 conversationUpdate 事件并检查何时添加了机器人。在那之后,你可以 post 一条消息或开始一个新的对话(就像我从 ContosoFlowers Node.js sample, though there are many others 中提取的下面的代码一样)。

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