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.
1 Answers
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();
}
Here's my 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();
}
}
Here's my proactive message code:
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:[email protected]";
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());
}
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.