【问题标题】:How to send the notification to Bot using azure fucntion?如何使用 azure 功能向 Bot 发送通知?
【发布时间】:2020-12-14 13:49:15
【问题描述】:

我已经使用 Bot 框架的 SDK v3 C# 构建了一个机器人,并且我还实现了对话 id 将存储在 azure 表中的主动消息和所有相关信息。当用户发起对话时,机器人将识别用户。我想每天使用时间触发功能将通知发送到机器人服务。但我无法在 azure function app 的输出绑定中找到 bot 框架。

如果没有 bot 框架作为输出绑定,我该怎么做。

==============================代码测试============= ========================== 我已经测试了代码,它给了我同样的错误,请看一下。

===========================积分================= ======================= 这是我的整合。如果我删除 Http 绑定作为输出错误将保持不变

================================Json文件代码========== ======================= 这是我的函数 json 文件代码。

【问题讨论】:

    标签: azure-functions botframework


    【解决方案1】:

    我有一个这样的场景:需要一个团队对话机器人在每天的特定时间向群聊发送主动消息。

    我的方法是:在函数中设置一个时间触发器,当它触发时,我的函数会调用一个我自己创建的API,这个API会执行发送主动消息的方法。

    这是我的功能代码,希望对您有所帮助:

    #r "Newtonsoft.Json"
    using System.Net;
    using System.IO;
    using System.IO.Compression;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.Extensions.Primitives;
    using Newtonsoft.Json;
    
    public static void Run(TimerInfo myTimer, ILogger log)
    {
        log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
    
    
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://xxxxxxxx.azurewebsites.net/api/sendProactiveMesg");
    request.Method = "GET";
    
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    Stream myResponseStream = response.GetResponseStream();
    
    }
    

    ============更新============

    根据您的错误信息,您似乎在某些地方错过了配置,但我不知道为什么会这样。我将分享我在创建时间触发器方面的经验。保存代码后,每分钟触发一次。如果这没有帮助,你能描述一下你的配置吗?因为我从来没有遇到过'none value'的时间触发错误。

    =========================更新2 ==================== ========= 我的代码:

    public async Task sendtoPersonal()
            {
                string teamInternalId = "19:3fxxx7d8c@unq.gbl.spaces";
                string serviceUrl = "https://smba.trafficmanager.net/amer/";
                string mentionUserPrincipalName = "xxx";
                string tenantId = "72fxxxx47";
                string botClientID = "e8caxxxx8c";
                string botClientSecret = "5zxxxxJA";
                AppCredentials.TrustServiceUrl(serviceUrl);
                ConnectorClient connectorClient = new ConnectorClient(new Uri(serviceUrl), new MicrosoftAppCredentials(botClientID, botClientSecret), true);
                TeamsChannelAccount user2 = (await connectorClient.Conversations.GetConversationMembersAsync(teamInternalId)).Select((ChannelAccount channelAccount) => JObject.FromObject(channelAccount).ToObject<TeamsChannelAccount>()).First((TeamsChannelAccount user) => user.UserPrincipalName == mentionUserPrincipalName);
                //Activity personalMessageActivity = MessageFactory.Text("Personal message from the Bot!");
                IMessageActivity personalMessageActivity = await showTeamStatus();
                ConversationParameters conversationParameters = new ConversationParameters
                {
                    Members = new List<ChannelAccount>
                {
                    user2
                },
                    ChannelData = new TeamsChannelData
                    {
                        Tenant = new TenantInfo
                        {
                            Id = tenantId
                        }
                    }
                };
                ConversationResourceResponse response = await connectorClient.Conversations.CreateConversationAsync(conversationParameters);
                await connectorClient.Conversations.SendToConversationAsync(response.Id, (Activity)personalMessageActivity);
            }
    

    【讨论】:

    • @Aaditya 我已经更新了我对您的错误消息的回答,如果这不起作用,我愿意了解更多细节。
    • 您好,在我看来,您已经实现了向用户发送主动消息,这意味着您的后端程序有一个方法可以获取conversationId 和其他必要的参数,并使用这些参数发送主动消息。所以我上面的“API”是创建一个方法来调用上面的方法并将这个方法公开为一个API。我将在回答中显示我的 API。等一下。
    • @Aaditya 只是尝试一下。我的解决方案是您自己的代码。我的方法“sendtoPersonal”用于发送主动消息。我想让这个方法被时间触发器调用,所以我用路由'api/sendProactiveMesg'创建了另一个方法,当我访问'xxx.azurewebsites.net/api/sendProactiveMes'时,我的机器人会向某人发送一条消息。和url一样的场景被时间触发器调用。
    • 如果您可以在浏览器上触发您的方法并获得正确的响应(我的意思是代码没问题并正确发送消息),那么只需比较上面的屏幕截图。我已经发布了我的功能代码和集成以及 function.json,试图让它们保持不变?还是重启触发器?
    猜你喜欢
    • 2017-08-15
    • 2019-07-07
    • 2017-05-04
    • 2021-04-04
    • 2021-02-11
    • 1970-01-01
    • 2019-06-13
    • 2018-05-19
    • 2020-08-29
    相关资源
    最近更新 更多