node.js - 将主动消息从 Azure 函数发送到 botservice - 节点
问题描述
我正在使用 botframework v4,但是从 v3 过来,我没有找到任何与我在下面使用的代码类似的文档,但对于 v4,关于从 Azure Function App 发送主动消息
以下是我之前使用但无法适应的代码:
var builder = require('botbuilder');
// setup bot credentials
var connector = new builder.ChatConnector({
appId: process.env.MICROSOFT_APP_ID,
appPassword: process.env.MICROSOFT_APP_PASSWORD
});
module.exports = function (context, req) {
if (req.body) {
var savedAddress = req.body.channelAddress;
var inMemoryStorage = new builder.MemoryBotStorage();
var bot = new builder.UniversalBot(connector).set('storage', inMemoryStorage);
sendProactiveMessage(savedAddress, bot)
}
};
function sendProactiveMessage(address, bot) {
var msg = new builder.Message().address(address);
msg.textLocale('en-US');
var img = {
attachments: [{
contentType: "image/jpg",
contentUrl: latestUrl,
}]
};
msg.addAttachment(img.attachments[0]);
msg.text('hello');
bot.send(msg);
}
这适用于 v3 但不适用于 v4。
如果可能的话,我还想找到一种方法来注销用户:
await botAdapter.signOutUser(innerDc.context, this.connectionName);
这就是我在机器人本身中执行此操作的方式,但再次从 Azure Functions 执行此操作被证明是困难的。
任何帮助,将不胜感激。
解决方案
太好了,您正在从 v3 迁移到 v4!您是否看过向用户发送主动通知?此示例非常简单,可以在 Azure 函数中使用。
首先,您通过调用在您的机器人中检索对话参考TurnContext.getConversationReference(context.activity);
。这是您可以在主动功能中用于打开对话的参考。在您的情况下,您通过请求正文将其提供给主动功能,因此我将在我的示例中执行相同的操作。
我的主动端点示例是用 Typescript 编写的,但是它在纯 Javascript 中的工作方式相同。在 Azure Functions 中创建 HTTP 触发器并使用以下代码。为了清楚起见,我已经添加了内联评论。
const { BotFrameworkAdapter } = require('botbuilder');
// Create adapter.
// If you need to share this adapter in multiple functions, you could
// instantiate it somewhere else and import it in every function.
const adapter = new BotFrameworkAdapter({
appId: process.env.MicrosoftAppId,
appPassword: process.env.MicrosoftAppPassword
});
module.exports = async function (context, req) {
// Validate if request has a body
if (!req.body) {
context.res = {
status: 400,
body: "Please pass a conversation reference in the request body"
};
return;
}
// Retrieve conversation reference from POST body
const conversationReference = req.body;
// Open the conversation and retrieve a TurnContext
await adapter.continueConversation(conversationReference, async turnContext => {
// Send a text activity
// Depending on the channel, you might need to use https://aka.ms/BotTrustServiceUrl
await turnContext.sendActivity('Proactive hello');
});
context.res = {
body: 'Message sent!'
};
};
最后,您可以向此 Azure 函数发出请求,在其中将对话引用作为 type 的主体传递application/json
。
使用诸如 signOutUser 之类的功能扩展此示例很简单。您可以调用函数内的所有continueConversation
函数,就像在普通机器人中一样。如果您愿意,您甚至可以在那里接收适配器对象。
await adapter.continueConversation(conversationReference, async turnContext => {
// Sign user out
turnContext.adapter.signOutUser(turnContext, 'connection-name');
});
推荐阅读
- python - 无法在本地服务器上运行项目,显示目录中的文件而不是应用程序
- android - 活动未收到通知意图
- mysql - 如何过滤以相同字符开头的mysql选择?
- html - 向nodejs后端发送不完整数据的网页
- android - SomeApp“从未使用过”
- postgresql - 根据单独的时区列转换 UTC - Postgres
- oauth-2.0 - 如何防止刷新被盗的访问令牌
- nginx - Yii::$app->response->sendFile() 在 NGINX 上不起作用
- c++ - main.cpp:65:16:错误:“*”标记之前的预期主表达式
- scala - DynamoDB - 基于非关键条件从集合中删除项目