如何在 MS teams bot 中安排和发送消息

How to schedule and send the message in MS teams bot

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

我想我的经验会对你有所帮助。几天前,我收到了让 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();
        }
    }

这是我的主动消息代码:

You can get groupChatConversationId and serviceUrl by using Filder to catch the request detail when your target message receiver is chatting with the bot. BotClientId and BotClientSecret are from Azure ad application.

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 对话机器人程序的新手,我想我的另一个答案会对您有所帮助 . And to know about how to send proactive message, watch this document