node.js - Microsoft Azure Bot Framework SDK 4:使用 Node js 从 bot 向特定用户发送主动消息
问题描述
通过在数据库中保存 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);
botbuilder SDK 4 如何做到这一点?我知道 Rest API,但希望通过 SDK 本身来实现这一点,因为 SDK 是机器人和用户之间更优选的通信方式。
提前致谢。
解决方案
BotFramework v4 SDK 中的主动消息使您能够继续与个人用户对话或向他们发送通知。
首先,您需要TurnContext
从botbuilder
库中导入,以便获取对话参考。
const { TurnContext } = require('botbuilder');
然后,在该onTurn
方法中,您可以调用该getConversationReference
方法TurnContext
并将结果引用保存在数据库中。
/**
* @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
从适配器调用该方法以向特定用户发送消息或通知。
await this.adapter.continueConversation(reference, async (proactiveTurnContext) => {
await proactiveTurnContext.sendActivity('Hello, this is a notification')
});
推荐阅读
- c++ - 用于智能指针的 std::atomic 的部分模板特化
- kubernetes - Kubernetes pod资源限制/配额占主机资源的百分比(相对)而不是使用绝对值?
- python - 尝试使用调度库来调度函数。收到“无效的时间格式”错误
- selenium-webdriver - 如何在并行运行时截取屏幕截图
- nginx - 使用 VHOST 的单个 VPS 中的多个域
- javascript - Material-ui 有条件禁用收音机
- python-3.x - 将 NaN 替换为组的现有值
- python - 清理字典嵌套字典和字典列表中的字典键
- php - 谷歌视觉 API 中的奇怪错误
- node.js - 如何使用 MERN 通过 CheckboxList 分配用户权限