【问题标题】:Microsoft Bot framework: Sending Message on connectMicrosoft Bot 框架:在连接时发送消息
【发布时间】:2017-08-20 06:14:17
【问题描述】:

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

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 消息。我想在用户连接后立即发送此消息。

【问题讨论】:

    标签: node.js botframework


    【解决方案1】:

    挂钩conversationUpdate 事件并检查何时添加机器人。之后,您可以发布一条消息或开始一个新的对话(如下面我从ContosoFlowers Node.js sample 中提取的代码,尽管有很多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, '/');
                }
            });
        }
    });
    

    【讨论】:

    • 谢谢...正是我想要的。
    • 我还能获得所有连接到机器人的用户的列表吗?
    • 嗯,我不知道有什么办法做到这一点。您应该手动执行此操作,存储开始与机器人对话的用户。
    • 那么会有一些方法检查用户当前是否连接?
    • 不...如果连接的意思是“积极与机器人交谈”,那么...再次,只需使用传入的消息并在那里添加一些逻辑。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-29
    • 2017-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多