.net-core - 如何在 MS Teams 机器人中安排和发送消息

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

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

最佳答案

我想我的经验会对你有所帮助。几天前,我收到一个请求,要求让 Teams 机器人在特定时间向聊天组发送主动消息。我的想法是在我的代码中创建一个 Api 并设置一个时间触发器以使我的函数调用此 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();
}

这是我的 Controller :

[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:<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="52672c2c2c3465603112263a203733367c2460" rel="noreferrer noopener nofollow">[email protected]</a>";
            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 .

关于.net-core - 如何在 MS Teams 机器人中安排和发送消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65092100/

相关文章:

azure-active-directory - 使用 Azure AD 1.0 终结点客户端凭据流调用 Microsoft Graph API

node.js - Bot框架(v4)使用HeroCards在轮播中提示选择不进入下一步

c# - 自适应卡片能否以模板方式用于自适应对话?

.net-core - 机器人配置不包含 ID 为 `luis` 的服务类型 `basic-bot-LUIS` 。 (.NET 核心)

azure - 限制 Azure 中的应用程序注册权限

json - 使用图形 api 在线共享点中创建多项选择字段的 POST 正文是什么?

c# - 无法在 Windows 10 上安装 dotnet-ef 工具

.net-core - 缺少packages.config文件

asp.net-core - .NET Core 锁定文件

asp.net - Net Core 2.0寻找appsettings.Production.json