microsoft/teams.net
Publicmirrored from https://github.com/microsoft/teams.netAvailable
core/samples/scenarios/proactive.cs
43lines · modecode
| 1 | #!/usr/bin/dotnet run |
| 2 | |
| 3 | #:sdk Microsoft.NET.Sdk.Worker |
| 4 | |
| 5 | #:project ../../src/Microsoft.Bot.Core/Microsoft.Bot.Core.csproj |
| 6 | |
| 7 | using Microsoft.Bot.Core.Hosting; |
| 8 | using Microsoft.Bot.Core; |
| 9 | using Microsoft.Bot.Core.Schema; |
| 10 | |
| 11 | var builder = Host.CreateApplicationBuilder(args); |
| 12 | builder.Services.AddBotApplicationClients(); |
| 13 | builder.Services.AddHostedService<Worker>(); |
| 14 | |
| 15 | var host = builder.Build(); |
| 16 | host.Run(); |
| 17 | |
| 18 | public class Worker(ConversationClient conversationClient, ILogger<Worker> logger) : BackgroundService |
| 19 | { |
| 20 | const string conversationId = "a:17vxw6pGQOb3Zfh8acXT8m_PqHycYpaFgzu2mFMUfkT-h0UskMctq5ZPPc7FIQxn2bx7rBSm5yE_HeUXsCcKZBrv77RgorB3_1_pAdvMhi39ClxQgawzyQ9GBFkdiwOxT"; |
| 21 | |
| 22 | protected override async Task ExecuteAsync(CancellationToken stoppingToken) |
| 23 | { |
| 24 | while (!stoppingToken.IsCancellationRequested) |
| 25 | { |
| 26 | if (logger.IsEnabled(LogLevel.Information)) |
| 27 | { |
| 28 | CoreActivity proactiveMessage = new() |
| 29 | { |
| 30 | Text = $"Proactive hello at {DateTimeOffset.Now}", |
| 31 | ServiceUrl = new Uri("https://smba.trafficmanager.net/amer/56653e9d-2158-46ee-90d7-675c39642038/"), |
| 32 | Conversation = new() |
| 33 | { |
| 34 | Id = conversationId |
| 35 | } |
| 36 | }; |
| 37 | var aid = await conversationClient.SendActivityAsync(proactiveMessage, stoppingToken); |
| 38 | logger.LogInformation($"Activity {aid} sent"); |
| 39 | } |
| 40 | await Task.Delay(1000, stoppingToken); |
| 41 | } |
| 42 | } |
| 43 | } |