Microsoft Bot框架:在连接上发送消息 [英] Microsoft Bot framework: Sending Message on connect

查看:95
本文介绍了Microsoft Bot框架:在连接上发送消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

推荐答案

进入conversationUpdate事件并检查何时添加了漫游器.之后,您可以发布一条消息或开始一个新对话框(如我从其他一样).

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

这篇关于Microsoft Bot框架:在连接上发送消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆