microsoft/teams.net
Publicmirrored fromhttps://github.com/microsoft/teams.netAvailable
core/samples/AllInvokesBot/Program.cs
163lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using System.Text.Json; |
| 5 | using System.Text.Json.Nodes; |
| 6 | using AllInvokesBot; |
| 7 | using Microsoft.Teams.Apps; |
| 8 | using Microsoft.Teams.Apps.Handlers; |
| 9 | using Microsoft.Teams.Apps.Handlers.TaskModules; |
| 10 | using Microsoft.Teams.Apps.Schema; |
| 11 | using Microsoft.Teams.Core.Hosting; |
| 12 | |
| 13 | WebApplicationBuilder webAppBuilder = WebApplication.CreateSlimBuilder(args); |
| 14 | webAppBuilder.Services.AddTeamsBotApplication(); |
| 15 | WebApplication webApp = webAppBuilder.Build(); |
| 16 | |
| 17 | TeamsBotApplication bot = webApp.UseBotApplication<TeamsBotApplication>(); |
| 18 | |
| 19 | // ==================== MESSAGE - SEND SIMPLE CARD ==================== |
| 20 | bot.OnMessage(async (context, cancellationToken) => |
| 21 | { |
| 22 | Console.WriteLine("✓ OnMessage"); |
| 23 | |
| 24 | JsonObject card = Cards.CreateWelcomeCard(); |
| 25 | |
| 26 | TeamsAttachment attachment = TeamsAttachment.CreateBuilder() |
| 27 | .WithAdaptiveCard(card) |
| 28 | .Build(); |
| 29 | |
| 30 | await context.SendActivityAsync(new MessageActivity([attachment]), cancellationToken); |
| 31 | }); |
| 32 | |
| 33 | // ==================== ADAPTIVE CARD ACTION ==================== |
| 34 | bot.OnAdaptiveCardAction(async (context, cancellationToken) => |
| 35 | { |
| 36 | Console.WriteLine("✓ OnAdaptiveCardAction"); |
| 37 | AdaptiveCardActionValue? value = context.Activity.Value; |
| 38 | AdaptiveCardAction? action = value?.Action; |
| 39 | string? verb = action?.Verb; |
| 40 | Dictionary<string, object>? data = action?.Data; |
| 41 | |
| 42 | Console.WriteLine($" Verb: {verb}"); |
| 43 | Console.WriteLine($" Data: {JsonSerializer.Serialize(data)}"); |
| 44 | |
| 45 | // Handle file upload request |
| 46 | if (verb == "requestFileUpload") |
| 47 | { |
| 48 | JsonObject fileConsentCard = Cards.CreateFileConsentCard(); |
| 49 | TeamsAttachment fileConsentCardResponse = TeamsAttachment.CreateBuilder() |
| 50 | .WithContent(fileConsentCard).WithContentType(AttachmentContentTypes.FileConsentCard) |
| 51 | .WithName("file_consent.json").Build(); |
| 52 | await context.SendActivityAsync(new MessageActivity([fileConsentCardResponse]), cancellationToken); |
| 53 | |
| 54 | return AdaptiveCardResponse.CreateMessageResponse("File Consent requested!"); |
| 55 | } |
| 56 | |
| 57 | string? message = data != null && data.TryGetValue("message", out object? msgValue) ? msgValue?.ToString() : null; |
| 58 | |
| 59 | JsonObject adaptiveActionCard = Cards.CreateAdaptiveActionResponseCard(verb, message); |
| 60 | TeamsAttachment adaptiveActionCardResponse = TeamsAttachment.CreateBuilder().WithAdaptiveCard(adaptiveActionCard).Build(); |
| 61 | await context.SendActivityAsync(new MessageActivity([adaptiveActionCardResponse]), cancellationToken); |
| 62 | |
| 63 | return AdaptiveCardResponse.CreateMessageResponse("Action submitted!"); |
| 64 | }); |
| 65 | |
| 66 | // ==================== TASK MODULE - FETCH ==================== |
| 67 | bot.OnTaskFetch(async (context, cancellationToken) => |
| 68 | { |
| 69 | Console.WriteLine("✓ OnTaskFetch"); |
| 70 | TeamsAttachment taskModuleCardResponse = TeamsAttachment.CreateBuilder() |
| 71 | .WithAdaptiveCard(Cards.CreateTaskModuleCard()).Build(); |
| 72 | return TaskModuleResponse.CreateBuilder() |
| 73 | .WithType(TaskModuleResponseTypes.Continue) |
| 74 | .WithTitle("Task") |
| 75 | .WithHeight("medium") |
| 76 | .WithWidth("medium") |
| 77 | .WithCard(taskModuleCardResponse) |
| 78 | .Build(); |
| 79 | |
| 80 | }); |
| 81 | |
| 82 | // ==================== TASK MODULE - SUBMIT ==================== |
| 83 | bot.OnTaskSubmit(async (context, cancellationToken) => |
| 84 | { |
| 85 | Console.WriteLine("✓ OnTaskSubmit"); |
| 86 | return TaskModuleResponse.CreateBuilder() |
| 87 | .WithType(TaskModuleResponseTypes.Message) |
| 88 | .WithMessage("Done") |
| 89 | .Build(); |
| 90 | }); |
| 91 | |
| 92 | // ==================== FILE CONSENT ==================== |
| 93 | bot.OnFileConsent(async (context, cancellationToken) => |
| 94 | { |
| 95 | Console.WriteLine("✓ OnFileConsent"); |
| 96 | |
| 97 | FileConsentValue? value = context.Activity.Value; |
| 98 | string? action = value?.Action; |
| 99 | FileUploadInfo? uploadInfo = value?.UploadInfo; |
| 100 | object? consentContext = value?.Context; |
| 101 | |
| 102 | if (action == "accept") |
| 103 | { |
| 104 | Console.WriteLine($" File accepted!"); |
| 105 | |
| 106 | // Upload the file |
| 107 | string? uploadUrl = uploadInfo?.UploadUrl?.ToString(); |
| 108 | string? fileName = uploadInfo?.Name; |
| 109 | string? contentUrl = uploadInfo?.ContentUrl?.ToString(); |
| 110 | string? uniqueId = uploadInfo?.UniqueId; |
| 111 | |
| 112 | if (uploadUrl != null && contentUrl != null) |
| 113 | { |
| 114 | // Create sample file content |
| 115 | string fileContent = "This is a sample file uploaded via file consent!"; |
| 116 | byte[] fileBytes = System.Text.Encoding.UTF8.GetBytes(fileContent); |
| 117 | int fileSize = fileBytes.Length; |
| 118 | |
| 119 | using HttpClient httpClient = new(); |
| 120 | using ByteArrayContent content = new(fileBytes); |
| 121 | content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream"); |
| 122 | content.Headers.ContentRange = new System.Net.Http.Headers.ContentRangeHeaderValue(0, fileSize - 1, fileSize); |
| 123 | |
| 124 | try |
| 125 | { |
| 126 | HttpResponseMessage uploadResponse = await httpClient.PutAsync(uploadUrl, content, cancellationToken); |
| 127 | Console.WriteLine($" Upload Status: {uploadResponse.StatusCode}"); |
| 128 | |
| 129 | if (uploadResponse.IsSuccessStatusCode) |
| 130 | { |
| 131 | JsonObject fileInfoContent = Cards.CreateFileInfoCard(uniqueId, uploadInfo?.FileType); |
| 132 | |
| 133 | TeamsAttachment fileUploadResponse = TeamsAttachment.CreateBuilder() |
| 134 | .WithName(fileName) |
| 135 | .WithContentType(AttachmentContentTypes.FileInfoCard) |
| 136 | .WithContentUrl(contentUrl != null ? new Uri(contentUrl) : null) |
| 137 | .WithContent(fileInfoContent).Build(); |
| 138 | |
| 139 | await context.SendActivityAsync(new MessageActivity([fileUploadResponse]), cancellationToken); |
| 140 | } |
| 141 | else |
| 142 | { |
| 143 | Console.WriteLine($" File upload failed: {await uploadResponse.Content.ReadAsStringAsync(cancellationToken)}"); |
| 144 | } |
| 145 | } |
| 146 | catch (Exception ex) |
| 147 | { |
| 148 | Console.WriteLine($" File upload error: {ex.Message}"); |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | else if (action == "decline") |
| 153 | { |
| 154 | Console.WriteLine($" File declined!"); |
| 155 | Console.WriteLine($" Context: {JsonSerializer.Serialize(consentContext)}"); |
| 156 | } |
| 157 | |
| 158 | return AdaptiveCardResponse.CreateBuilder() |
| 159 | .WithStatusCode(200) |
| 160 | .Build(); |
| 161 | }); |
| 162 | |
| 163 | webApp.Run(); |
| 164 | |