我想我的经验会对你有所帮助。几天前,我收到了让 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();
}
}
这是我的主动消息代码:
您可以通过使用获取 groupChatConversationId 和 serviceUrl
当您的目标消息接收者时,过滤器捕获请求详细信息
正在与机器人聊天。
BotClientId 和 BotClientSecret 来自 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。