且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Microsoft Azure Bot Framework SDK 4:使用Node js从机器人向特定用户发送主动消息

更新时间:2023-11-19 12:30:34

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

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

有关主动消息的更多信息,请查看 GitHub上的示例.希望这会有所帮助.

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