简体   繁体   English

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

I am able to send message to specific users with older botbuilder SDK 3.13.1 by saving message.address field in database. 通过将message.address字段保存在数据库中,我可以使用较旧的botbuilder SDK 3.13.1向特定用户发送消息。

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

How can this be done with botbuilder SDK 4? 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. 我知道Rest API,但想用SDK本身来实现这一点,因为SDK是机器人与用户之间通信的首选方式。

Thanks in advance. 提前致谢。

Proactive Messages in the BotFramework v4 SDK enable you to continue conversations with individual users or send them notifications. BotFramework v4 SDK中的主动消息使您能够继续与单个用户对话或向他们发送通知。

First, you need to import TurnContext from the botbuilder library so you can get the conversation reference. 首先,您需要从botbuilder库中导入TurnContext ,以便获取会话参考。

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

Then, in the onTurn method, you can call the getConversationReference method from TurnContext and save the resulting reference in a database. 然后,在onTurn方法中,可以从TurnContext调用getConversationReference方法,并将生成的引用保存在数据库中。

/**
 * @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 
    ...
}

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. 最后,您可以从数据库中检索引用,并从适配器中调用continueConversation方法以向特定用户发送消息或通知。

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 . 有关主动消息的更多信息,请参阅GitHub上文档或此示例 Hope this is helpful. 希望这会有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Bot Framework从Cosmos DB检索用户地址,以便发送主动消息NODE JS - Bot Framework retrieve user address from Cosmos DB in order to send a Proactive message NODE JS Microsoft Bot Builder - 向用户发送主动消息 - Microsoft Bot Builder - send proactive message to user bot框架按用户地址发送主动消息 - bot framework send proactive message by user address 如何在 Microsoft Bot Framework v.3 (Node.js) 中发送欢迎消息并自动加载特定对话框? - How to send welcome message AND load a specific dialog automatically in Microsoft Bot Framework v.3 (Node.js)? 我的机器人如何询问用户或机器人需要用户回复以发送消息 intelegraf node.js 机器人? - How my bot can ask to users or bot need reply from users to send message intelegraf node.js bot? 如何在使用瀑布对话框(Node js)的同时使用微软机器人框架将英雄卡片发送到 fb 信使 - How to send hero cards to the fb messenger using microsoft bot framework while using waterfall dialogs (Node js) 使用Node.js Microsoft Bot Framework显示图像 - Display images using the Node.js Microsoft Bot Framework Microsoft Bot Framework-Node Js与Salesforce的集成 - Microsoft Bot Framework - node Js integration with Salesforce 使用 SDK V4 中的 Microsoft Bot 框架创建 Alexa Bot - Creating An Alexa Bot Using The Microsoft Bot Framework in SDK V4 如何从Microsoft Bot Framework发送SMS(使用Twilio通道)? - How to send SMS (using Twilio channel) from Microsoft Bot Framework?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM