microsoft/teams.net
Publicmirrored fromhttps://github.com/microsoft/teams.netAvailable
core/samples/Proactive/Worker.cs
34lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using Microsoft.Teams.Bot.Core; |
| 5 | using Microsoft.Teams.Bot.Core.Schema; |
| 6 | |
| 7 | namespace Proactive; |
| 8 | |
| 9 | public class Worker(ConversationClient conversationClient, ILogger<Worker> logger) : BackgroundService |
| 10 | { |
| 11 | private const string ConversationId = "a:17vxw6pGQOb3Zfh8acXT8m_PqHycYpaFgzu2mFMUfkT-h0UskMctq5ZPPc7FIQxn2bx7rBSm5yE_HeUXsCcKZBrv77RgorB3_1_pAdvMhi39ClxQgawzyQ9GBFkdiwOxT"; |
| 12 | private const string FromId = "28:56653e9d-2158-46ee-90d7-675c39642038"; |
| 13 | private const string ServiceUrl = "https://smba.trafficmanager.net/teams/"; |
| 14 | |
| 15 | protected override async Task ExecuteAsync(CancellationToken stoppingToken) |
| 16 | { |
| 17 | while (!stoppingToken.IsCancellationRequested) |
| 18 | { |
| 19 | if (logger.IsEnabled(LogLevel.Information)) |
| 20 | { |
| 21 | CoreActivity proactiveMessage = new() |
| 22 | { |
| 23 | ServiceUrl = new Uri(ServiceUrl), |
| 24 | From = new() { Id = FromId }, |
| 25 | Conversation = new() { Id = ConversationId } |
| 26 | }; |
| 27 | proactiveMessage.Properties["text"] = $"Proactive hello at {DateTimeOffset.Now}"; |
| 28 | var aid = await conversationClient.SendActivityAsync(proactiveMessage, cancellationToken: stoppingToken); |
| 29 | logger.LogInformation("Activity {Aid} sent", aid.Id); |
| 30 | } |
| 31 | await Task.Delay(1000, stoppingToken); |
| 32 | } |
| 33 | } |
| 34 | } |
| 35 | |