microsoft/teams.net
Publicmirrored fromhttps://github.com/microsoft/teams.netAvailable
core/samples/CompatBot/EchoBot.cs
168lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using Microsoft.Bot.Builder; |
| 5 | using Microsoft.Bot.Builder.Teams; |
| 6 | using Microsoft.Bot.Connector; |
| 7 | using Microsoft.Teams.Bot.Core; |
| 8 | using Microsoft.Teams.Bot.Compat; |
| 9 | using Microsoft.Teams.Bot.Core.Schema; |
| 10 | using Microsoft.Bot.Schema; |
| 11 | using Microsoft.Bot.Schema.Teams; |
| 12 | using Newtonsoft.Json.Linq; |
| 13 | using Microsoft.Teams.Bot.Apps; |
| 14 | using Microsoft.Teams.Bot.Apps.Schema; |
| 15 | |
| 16 | namespace CompatBot; |
| 17 | |
| 18 | public class ConversationData |
| 19 | { |
| 20 | public int MessageCount { get; set; } = 0; |
| 21 | |
| 22 | } |
| 23 | |
| 24 | internal class EchoBot(TeamsBotApplication teamsBotApp, ConversationState conversationState, ILogger<EchoBot> logger) |
| 25 | : TeamsActivityHandler |
| 26 | { |
| 27 | public override async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default) |
| 28 | { |
| 29 | await base.OnTurnAsync(turnContext, cancellationToken); |
| 30 | |
| 31 | await conversationState.SaveChangesAsync(turnContext, false, cancellationToken); |
| 32 | } |
| 33 | protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken) |
| 34 | { |
| 35 | logger.LogInformation("OnMessage"); |
| 36 | IStatePropertyAccessor<ConversationData> conversationStateAccessors = conversationState.CreateProperty<ConversationData>(nameof(ConversationData)); |
| 37 | ConversationData conversationData = await conversationStateAccessors.GetAsync(turnContext, () => new ConversationData(), cancellationToken); |
| 38 | |
| 39 | string replyText = $"Echo from BF Compat [{conversationData.MessageCount++}]: {turnContext.Activity.Text}"; |
| 40 | await turnContext.SendActivityAsync(MessageFactory.Text(replyText, replyText), cancellationToken); |
| 41 | await turnContext.SendActivityAsync(MessageFactory.Text($"Send a proactive message `/api/notify/{turnContext.Activity.Conversation.Id}`"), cancellationToken); |
| 42 | |
| 43 | var activity = ((Activity)turnContext.Activity).FromCompatActivity(); |
| 44 | TeamsActivity tm = TeamsActivity.CreateBuilder() |
| 45 | .WithConversation(new Conversation { Id = activity.Conversation.Id }) |
| 46 | .WithText("Hello TM !") |
| 47 | .WithRecipient(activity.From, true) |
| 48 | .WithFrom(activity.Recipient) |
| 49 | .WithServiceUrl(activity.ServiceUrl!) |
| 50 | .Build(); |
| 51 | |
| 52 | await teamsBotApp.ConversationClient.SendActivityAsync(tm, cancellationToken: cancellationToken); |
| 53 | |
| 54 | // TeamsAPXClient provides Teams-specific operations like: |
| 55 | // - FetchTeamDetailsAsync, FetchChannelListAsync |
| 56 | // - FetchMeetingInfoAsync, FetchParticipantAsync, SendMeetingNotificationAsync |
| 57 | // - Batch messaging: SendMessageToListOfUsersAsync, SendMessageToAllUsersInTenantAsync, etc. |
| 58 | |
| 59 | await SendUpdateDeleteActivityAsync(turnContext, teamsBotApp.ConversationClient, cancellationToken); |
| 60 | |
| 61 | var attachment = new Attachment |
| 62 | { |
| 63 | ContentType = "application/vnd.microsoft.card.adaptive", |
| 64 | Content = Cards.FeedbackCardObj |
| 65 | }; |
| 66 | var attachmentReply = MessageFactory.Attachment(attachment); |
| 67 | await turnContext.SendActivityAsync(attachmentReply, cancellationToken); |
| 68 | |
| 69 | } |
| 70 | |
| 71 | |
| 72 | protected override async Task OnMessageReactionActivityAsync(ITurnContext<IMessageReactionActivity> turnContext, CancellationToken cancellationToken) |
| 73 | { |
| 74 | await turnContext.SendActivityAsync(MessageFactory.Text("Message reaction received."), cancellationToken); |
| 75 | } |
| 76 | |
| 77 | protected override async Task OnInstallationUpdateActivityAsync(ITurnContext<IInstallationUpdateActivity> turnContext, CancellationToken cancellationToken) |
| 78 | { |
| 79 | await turnContext.SendActivityAsync(MessageFactory.Text("Installation update received."), cancellationToken); |
| 80 | await turnContext.SendActivityAsync(MessageFactory.Text($"Send a proactive messages to `/api/notify/{turnContext.Activity.Conversation.Id}`"), cancellationToken); |
| 81 | } |
| 82 | |
| 83 | protected override async Task OnInstallationUpdateAddAsync(ITurnContext<IInstallationUpdateActivity> turnContext, CancellationToken cancellationToken) |
| 84 | { |
| 85 | await turnContext.SendActivityAsync(MessageFactory.Text("Installation update Add received."), cancellationToken); |
| 86 | await turnContext.SendActivityAsync(MessageFactory.Text($"Send a proactive messages to `/api/notify/{turnContext.Activity.Conversation.Id}`"), cancellationToken); |
| 87 | } |
| 88 | |
| 89 | protected override async Task<InvokeResponse> OnInvokeActivityAsync(ITurnContext<IInvokeActivity> turnContext, CancellationToken cancellationToken) |
| 90 | { |
| 91 | logger.LogInformation("Invoke Activity received: {Name}", turnContext.Activity.Name); |
| 92 | var actionValue = JObject.FromObject(turnContext.Activity.Value); |
| 93 | var action = actionValue["action"] as JObject; |
| 94 | var actionData = action?["data"] as JObject; |
| 95 | var userInput = actionData?["feedback"]?.ToString(); |
| 96 | //var userInput = actionValue["userInput"]?.ToString(); |
| 97 | |
| 98 | logger.LogInformation("Action: {Action}, User Input: {UserInput}", action, userInput); |
| 99 | |
| 100 | |
| 101 | |
| 102 | var attachment = new Attachment |
| 103 | { |
| 104 | ContentType = "application/vnd.microsoft.card.adaptive", |
| 105 | Content = Cards.ResponseCard(userInput) |
| 106 | }; |
| 107 | |
| 108 | var card = MessageFactory.Attachment(attachment); |
| 109 | await turnContext.SendActivityAsync(card, cancellationToken); |
| 110 | |
| 111 | return new InvokeResponse |
| 112 | { |
| 113 | Status = 200, |
| 114 | Body = new { value = "invokes from compat bot" } |
| 115 | }; |
| 116 | } |
| 117 | |
| 118 | protected override async Task OnMembersAddedAsync(IList<ChannelAccount> membersAdded, ITurnContext<IConversationUpdateActivity> turnContext, CancellationToken cancellationToken) |
| 119 | { |
| 120 | await turnContext.SendActivityAsync(MessageFactory.Text("Welcome."), cancellationToken); |
| 121 | await turnContext.SendActivityAsync(MessageFactory.Text($"Send a proactive messages to `/api/notify/{turnContext.Activity.Conversation.Id}`"), cancellationToken); |
| 122 | } |
| 123 | |
| 124 | protected override Task OnMembersRemovedAsync(IList<ChannelAccount> membersRemoved, ITurnContext<IConversationUpdateActivity> turnContext, CancellationToken cancellationToken) |
| 125 | { |
| 126 | return turnContext.SendActivityAsync(MessageFactory.Text("Bye."), cancellationToken); |
| 127 | } |
| 128 | |
| 129 | protected override async Task OnTeamsMeetingStartAsync(MeetingStartEventDetails meeting, ITurnContext<IEventActivity> turnContext, CancellationToken cancellationToken) |
| 130 | { |
| 131 | await turnContext.SendActivityAsync(MessageFactory.Text("Welcome to meeting: "), cancellationToken); |
| 132 | await turnContext.SendActivityAsync(MessageFactory.Text($"{meeting.Title} {meeting.MeetingType}"), cancellationToken); |
| 133 | } |
| 134 | |
| 135 | private static async Task SendUpdateDeleteActivityAsync(ITurnContext<IMessageActivity> turnContext, ConversationClient conversationClient, CancellationToken cancellationToken) |
| 136 | { |
| 137 | var cr = turnContext.Activity.GetConversationReference(); |
| 138 | Activity reply = (Activity)Activity.CreateMessageActivity(); |
| 139 | reply.ApplyConversationReference(cr, isIncoming: false); |
| 140 | reply.Text = "This is a proactive message sent using the Conversations API."; |
| 141 | |
| 142 | CoreActivity ca = reply.FromCompatActivity(); |
| 143 | |
| 144 | var res = await conversationClient.SendActivityAsync(ca, null, cancellationToken); |
| 145 | |
| 146 | await Task.Delay(2000, cancellationToken); |
| 147 | |
| 148 | await conversationClient.UpdateActivityAsync( |
| 149 | cr.Conversation.Id, |
| 150 | res.Id!, |
| 151 | TeamsActivity.CreateBuilder() |
| 152 | .WithId(res.Id ?? "") |
| 153 | .WithServiceUrl(new Uri(turnContext.Activity.ServiceUrl)) |
| 154 | .WithType(ActivityType.Message) |
| 155 | .WithText("This message has been updated.") |
| 156 | .WithFrom(ca.From) |
| 157 | .Build(), |
| 158 | null, |
| 159 | cancellationToken); |
| 160 | |
| 161 | await Task.Delay(2000, cancellationToken); |
| 162 | |
| 163 | await conversationClient.DeleteActivityAsync(cr.Conversation.Id, res.Id!, new Uri(turnContext.Activity.ServiceUrl), AgenticIdentity.FromProperties(ca.From.Properties), null, cancellationToken); |
| 164 | |
| 165 | await turnContext.SendActivityAsync(MessageFactory.Text("Proactive message sent and deleted."), cancellationToken); |
| 166 | } |
| 167 | |
| 168 | } |
| 169 | |