microsoft/teams.net
Publicmirrored fromhttps://github.com/microsoft/teams.netAvailable
core/samples/CoreBot/Program.cs
46lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using Azure.Monitor.OpenTelemetry.AspNetCore; |
| 5 | using Microsoft.Teams.Bot.Core; |
| 6 | using Microsoft.Teams.Bot.Core.Hosting; |
| 7 | using Microsoft.Teams.Bot.Core.Schema; |
| 8 | |
| 9 | WebApplicationBuilder webAppBuilder = WebApplication.CreateSlimBuilder(args); |
| 10 | webAppBuilder.Services.AddOpenTelemetry().UseAzureMonitor(); |
| 11 | webAppBuilder.Services.AddBotApplication<BotApplication>(); |
| 12 | WebApplication webApp = webAppBuilder.Build(); |
| 13 | BotApplication botApp = webApp.UseBotApplication<BotApplication>(); |
| 14 | |
| 15 | webApp.MapGet("/", () => "CoreBot is running."); |
| 16 | |
| 17 | botApp.OnActivity = async (activity, cancellationToken) => |
| 18 | { |
| 19 | string replyText = $"CoreBot running on SDK {BotApplication.Version}."; |
| 20 | |
| 21 | replyText += $"<br /> Received Activity `{activity.Type}`."; |
| 22 | |
| 23 | //activity.Properties.Where(kvp => kvp.Key.StartsWith("text")).ToList().ForEach(kvp => |
| 24 | //{ |
| 25 | // replyText += $"<br /> {kvp.Key}:`{kvp.Value}` "; |
| 26 | //}); |
| 27 | |
| 28 | |
| 29 | string? conversationType = "unknown conversation type"; |
| 30 | if (activity.Conversation.Properties.TryGetValue("conversationType", out object? ctProp)) |
| 31 | { |
| 32 | conversationType = ctProp?.ToString(); |
| 33 | } |
| 34 | |
| 35 | replyText += $"<br /> To conv type: `{conversationType}` conv id: `{activity.Conversation.Id}`"; |
| 36 | |
| 37 | CoreActivity replyActivity = CoreActivity.CreateBuilder() |
| 38 | .WithType(ActivityType.Message) |
| 39 | .WithConversationReference(activity) |
| 40 | .WithProperty("text", replyText) |
| 41 | .Build(); |
| 42 | |
| 43 | await botApp.SendActivityAsync(replyActivity, cancellationToken); |
| 44 | }; |
| 45 | |
| 46 | webApp.Run(); |
| 47 | |