将主动消息从 Azure 函数发送到 botservice - 节点 [英] Sending Proactive Messages from Azure functions to botservice - node

查看:19
本文介绍了将主动消息从 Azure 函数发送到 botservice - 节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 botframework v4,但从 v3 开始,我没有找到任何与我在下面使用的代码类似的文档,但对于 v4,关于从 Azure Function App 发送主动消息

I am using botframework v4, but coming over from v3, I have not found any documentation that is similar to the code I use below but for v4, regarding sending proactive messages from Azure Function App

以下是我之前使用过但无法适应的代码:

Below is the code I previously used but am having trouble adapting:

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.

This works fine with v3 but not v4.

如果可能的话,我还想找到一种方法让用户退出:

If possible I would also like to find a way to log a user out:

await botAdapter.signOutUser(innerDc.context, this.connectionName);

这就是我在 bot 本身中执行此操作的方式,但再次从 Azure Functions 执行此操作被证明是困难的.

This is how I do it in the bot itself, but doing so from Azure Functions again is proving difficult.

任何帮助将不胜感激.

推荐答案

很高兴您正在从 v3 迁移到 v4!你看过 向用户发送主动通知?此示例非常简单,可以在 Azure 函数中使用.

Great that you are making the move from v3 to v4! Have you had a look at Send proactive notifications to users? This example is pretty straight forward and can be used within an Azure function.

首先,您通过调用 TurnContext.getConversationReference(context.activity); 在您的机器人中检索对话参考.这是您可以在主动功能中用于打开对话的参考.在您的情况下,您通过请求正文将其提供给主动功能,因此我将在我的示例中执行相同的操作.

First you retrieve the Conversation Reference in your bot by calling TurnContext.getConversationReference(context.activity);. This is the reference you could use in your proactive function to open the conversation. In your case you provide that via the request body to a proactive function, so I will do the same in my example.

我的主动端点示例是用 Typescript 编写的,但它在纯 Javascript 中的工作方式相同.在 Azure Functions 中创建 HTTP 触发器并使用以下代码.为了清楚起见,我已经添加了内联评论.

My proactive endpoint example is written in Typescript, however it works the same way in plain Javascript. Create a HTTP trigger in Azure Functions and use the following code. I have added comments inline for clarity.

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 函数发出请求,在其中将对话引用作为 application/json 类型的主体传递.

In the end you could make a request to this Azure Function, where you pass the Conversation Reference as body of the type application/json.

使用 signOutUser 等功能扩展此示例很简单.您可以调用 continueConversation 函数中的所有函数,就像在普通机器人中一样.如果您愿意,您甚至可以在那里接收适配器对象.

Extending this example with features like signOutUser is simple. You can call all functions within the continueConversation function, just as in a normal bot. You can even receive the adapter object there if you wish.

await adapter.continueConversation(conversationReference, async turnContext => {
    // Sign user out
    turnContext.adapter.signOutUser(turnContext, 'connection-name');
});

这篇关于将主动消息从 Azure 函数发送到 botservice - 节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆