更新时间: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.