[英]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.