【问题标题】:How to schedule and send the message in MS teams bot如何在 MS 团队机器人中安排和发送消息
【发布时间】:2021-01-18 15:33:15
【问题描述】:

我创建了一个团队机器人,并有一个用 .NET 核心编写的服务来处理用户的消息以进行相应的回复。但是我想安排一条消息并将其发送给用户(即从机器人发起对话)。
我浏览了在线可用资源,其中大部分参考了发送主动消息的文档。但这并没有帮助,因为在我的场景中,我想在一天中的特定时间发起对话,而我没有进入可以编写代码的事件处理程序。
我还尝试了 Azure 功能,因为它可以被安排并尝试编写代码以在团队中发送消息,但我遇到了一些我无法解决的包相关错误。
如果可能的话,我正在寻找我已经拥有的服务的解决方案。尽管如此,任何事情都会奏效。 代码示例将非常有帮助,因为我在机器人编程方面没有太多经验。
提前致谢。

【问题讨论】:

标签: .net-core microsoft-graph-api botframework chatbot microsoft-teams


【解决方案1】:

我想我的经验会对你有所帮助。几天前,我收到了让 Teams 机器人在特定时间向聊天组发送主动消息的请求。我的想法是creating an Api in my code and setting a time trigger to make my function call this Api。而且确实奏效了。

这是我的函数代码,你可以在document了解如何设置时间触发:

#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://xxxteamsbot.azurewebsites.net/api/sendProactiveMesg");
request.Method = "GET";

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
}

这是我的控制器:

[Route("api/sendProactiveMesg")]
    [ApiController]
    public class ProactiveController : Controller
    {
        public async Task sendProactiveMesg()
        {
            ProactiveMesgChannel a = new ProactiveMesgChannel();
            await a.sendtoGroupChat();
            //ProactiveMesgPersonal a = new ProactiveMesgPersonal();
            //await a.sendtoPersonal();
        }
    }

这是我的主动消息代码:

您可以通过使用获取 groupChatConversationIdserviceUrl 当您的目标消息接收者时,过滤器捕获请求详细信息 正在与机器人聊天。 BotClientIdBotClientSecret 来自 Azure 广告应用程序。

public async Task sendtoGroupChat()
        {
            
            string groupChatConversationId = "19:5~~~f72c@thread.v2";
            string serviceUrl = "https://s~~~t/amer/";
            string botClientID = "e~~~c";
            string botClientSecret = "5z~~~A";
            AppCredentials.TrustServiceUrl(serviceUrl);
            ConnectorClient connectorClient = new ConnectorClient(new Uri(serviceUrl), new MicrosoftAppCredentials(botClientID, botClientSecret), true);
            IMessageActivity message = await showTeamStatus();
            await connectorClient.Conversations.SendToConversationAsync(groupChatConversationId, (Activity)message);
        }

        private async Task<IMessageActivity> showTeamStatus()
        {
            GetAnswersDetail detail = new GetAnswersDetail();
            List<ResData> res = await detail.GetDetailByIds();
            HeroCard card = new HeroCard();
            card.Title = "Status In " + getMonth();
            string html = "<div>Here is the detail view";
            html = (card.Text = html + "</div> ");
            return MessageFactory.Attachment(card.ToAttachment());
        }

顺便说一句,如果您是 Teams 对话机器人程序的新手,我想我的另一个答案将帮助您 create a bot。要了解如何发送主动消息,请观看此document

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-26
    相关资源
    最近更新 更多