Microsoft Azure Bot Framework SDK 4:使用Node js从机器人向特定用户发送主动消息 [英] Microsoft Azure Bot Framework SDK 4: Send proactive message to specific users from bot using Node js

查看:68
本文介绍了Microsoft Azure Bot Framework SDK 4:使用Node js从机器人向特定用户发送主动消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过将message.address字段保存在数据库中,我可以使用较旧的botbuilder SDK 3.13.1向特定用户发送消息.

I am able to send message to specific users with older botbuilder SDK 3.13.1 by saving message.address field in database.

    var connector = new builder.ChatConnector({
        appId: process.env.MicrosoftAppId,
        appPassword: process.env.MicrosoftAppPassword,
        openIdMetadata: process.env.BotOpenIdMetadata
    });
    var bot = new builder.UniversalBot(connector);
    var builder = require('botbuilder');
    var msg = new builder.Message().address(msgAddress);
    msg.text('Hello, this is a notification');
    bot.send(msg);

如何使用botbuilder SDK 4做到这一点?我知道Rest API,但想用SDK本身来实现这一点,因为SDK是机器人与用户之间更优选的通信方式.

How can this be done with botbuilder SDK 4? I am aware of the Rest API but want to achieve this with the SDK itself because the SDK is the more preferred way of communication between the bot and user.

谢谢.

推荐答案

主动消息使您可以继续与单个用户进行对话或向他们发送通知.

Proactive Messages in the BotFramework v4 SDK enable you to continue conversations with individual users or send them notifications.

首先,您需要从botbuilder库中导入TurnContext,以便获取会话参考.

First, you need to import TurnContext from the botbuilder library so you can get the conversation reference.

const { TurnContext } = require('botbuilder');

然后,在onTurn方法中,可以从TurnContext调用getConversationReference方法并将结果引用保存在数据库中.

Then, in the onTurn method, you can call the getConversationReference method from TurnContext and save the resulting reference in a database.

/**
 * @param {TurnContext} turnContext A TurnContext object representing an incoming message to be handled by the bot.
 */
async onTurn(turnContext) {
    ...
    const reference = TurnContext.getConversationReference(turnContext.activity);
    //TODO: Save reference to your database 
    ...
}

最后,您可以从数据库中检索引用并从适配器调用continueConversation方法,以向特定用户发送消息或通知.

Finally, you can retrieve the reference from the database and call the continueConversation method from the adapter to send specific users a message or notification.

await this.adapter.continueConversation(reference, async (proactiveTurnContext) => {
    await proactiveTurnContext.sendActivity('Hello, this is a notification')
});

有关主动消息的更多信息,请查看

For more information about proactive messages, take a look at the documentation or this example on GitHub. Hope this is helpful.

这篇关于Microsoft Azure Bot Framework SDK 4:使用Node js从机器人向特定用户发送主动消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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