如何在MS Team Bot中安排和发送消息 [英] How to schedule and send the message in MS teams bot

查看:87
本文介绍了如何在MS Team Bot中安排和发送消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have created a teams bot and had a service written in .NET core to handle the user's messages to reply accordingly. However I wanted to schedule a message and send it to the user(i.e. initiate conversation from bot).
I have gone through the online available sources, most of which refer the documentation for sending proactive message. But it didn't help as in my scenario I want to initiate a conversation on a specific time of the day and I am not getting in which event handler I can write the code.
I also tried Azure functions as it can be scheduled and try to write code for sending message in teams, but I got some package related errors which I am not able to resolve.
I am looking for a solution in the service I already have, if possible. Nonetheless anything will work. A sample of code will be very helpful, as I am not much experienced in bot programming.
Thanks in advance.

推荐答案

我认为我的经验会对您有所帮助.在收到要求让Teams机器人在特定时间向聊天组发送主动消息的请求的前几天.我的想法是在我的代码中创建一个Api并设置一个时间触发器以使我的函数调用此Api .而且确实有效.

I think my experience will help you. Some days before I got a request to make Teams bot send proactive message to a chat group at a specific time. My idea was creating an Api in my code and setting a time trigger to make my function call this Api. And it really worked.

这是我的功能代码,您可以在

Here's my function code, and you can know about how to set time trigger in this 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();
        }
    }

这是我的主动消息代码:

Here's my proactive message code:

您可以通过使用 groupChatConversationId serviceUrl 当目标消息接收者时,Filder可以捕获请求详细信息正在与机器人聊天. BotClientId BotClientSecret 来自Azure广告应用程序.

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对话机器人程序的新手,我想我的另一个答案将帮助您文档.

By the way, if you are new to Teams conversation bot program, I think my another answer will help you to create a bot. And to know about how to send proactive message, watch this document.

这篇关于如何在MS Team Bot中安排和发送消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆