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

查看:77
本文介绍了将主动消息从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);

这是我在机器人本身中的操作方式,但是再次从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天全站免登陆