microsoft/teams.net
Publicmirrored fromhttps://github.com/microsoft/teams.netAvailable
core/samples/CompatBot/EchoBot.cs
196lines · 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.Schema; |
| 7 | using Microsoft.Bot.Schema.Teams; |
| 8 | using Microsoft.Teams.Bot.Apps; |
| 9 | using Microsoft.Teams.Bot.Apps.Schema; |
| 10 | using Microsoft.Teams.Bot.Compat; |
| 11 | using Microsoft.Teams.Bot.Core.Schema; |
| 12 | using Newtonsoft.Json.Linq; |
| 13 | |
| 14 | namespace CompatBot; |
| 15 | |
| 16 | public class ConversationData |
| 17 | { |
| 18 | public int MessageCount { get; set; } = 0; |
| 19 | |
| 20 | } |
| 21 | |
| 22 | internal class EchoBot(TeamsBotApplication teamsBotApp, ConversationState conversationState, ILogger<EchoBot> logger) |
| 23 | : TeamsActivityHandler |
| 24 | { |
| 25 | public override async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default) |
| 26 | { |
| 27 | await base.OnTurnAsync(turnContext, cancellationToken); |
| 28 | |
| 29 | await conversationState.SaveChangesAsync(turnContext, false, cancellationToken); |
| 30 | } |
| 31 | protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken) |
| 32 | { |
| 33 | logger.LogInformation("OnMessage"); |
| 34 | IStatePropertyAccessor<ConversationData> conversationStateAccessors = conversationState.CreateProperty<ConversationData>(nameof(ConversationData)); |
| 35 | ConversationData conversationData = await conversationStateAccessors.GetAsync(turnContext, () => new ConversationData(), cancellationToken); |
| 36 | |
| 37 | string replyText = $"Echo from BF Compat [{conversationData.MessageCount++}]: {turnContext.Activity.Text}"; |
| 38 | |
| 39 | var act = MessageFactory.Text(replyText, replyText); |
| 40 | act.Recipient = new ChannelAccount(); |
| 41 | act.Recipient.Properties.Add("isTargeted", true); |
| 42 | await turnContext.SendActivityAsync(act, cancellationToken); |
| 43 | |
| 44 | // await turnContext.SendActivityAsync(MessageFactory.Text($"Send a proactive message `/api/notify/{turnContext.Activity.Conversation.Id}`"), cancellationToken); |
| 45 | |
| 46 | var incomingCoreActivity = ((Activity)turnContext.Activity).FromCompatActivity(); |
| 47 | TeamsActivity tm = TeamsActivity.CreateBuilder() |
| 48 | .WithConversation(new Conversation { Id = incomingCoreActivity.Conversation?.Id! }) |
| 49 | .WithText("Hello TM !") |
| 50 | .WithRecipient(incomingCoreActivity.From, true) |
| 51 | .WithFrom(incomingCoreActivity.Recipient) |
| 52 | //.WithServiceUrl(activity.ServiceUrl!) |
| 53 | .WithServiceUrl("https://pilot1.botapi.skype.com/amer/9a9b49fd-1dc5-4217-88b3-ecf855e91b0e/") |
| 54 | .Build(); |
| 55 | |
| 56 | await teamsBotApp.ConversationClient.SendActivityAsync(tm, cancellationToken: cancellationToken); |
| 57 | |
| 58 | var res = await turnContext.SendActivityAsync( |
| 59 | MessageFactory.Text("I'm going to add and remove reactions to this message."), cancellationToken); |
| 60 | |
| 61 | await Task.Delay(500, cancellationToken); |
| 62 | |
| 63 | await teamsBotApp.ConversationClient.AddReactionAsync( |
| 64 | turnContext.Activity.Conversation.Id, |
| 65 | res.Id, |
| 66 | "laugh", |
| 67 | new Uri("https://pilot1.botapi.skype.com/amer/9a9b49fd-1dc5-4217-88b3-ecf855e91b0e/"), |
| 68 | //incomingCoreActivity.ServiceUrl!, |
| 69 | AgenticIdentity.FromProperties(incomingCoreActivity.Recipient?.Properties), |
| 70 | null, |
| 71 | cancellationToken); |
| 72 | |
| 73 | await Task.Delay(500, cancellationToken); |
| 74 | await teamsBotApp.ConversationClient.AddReactionAsync( |
| 75 | turnContext.Activity.Conversation.Id, |
| 76 | res.Id, |
| 77 | "sad", |
| 78 | incomingCoreActivity.ServiceUrl!, |
| 79 | AgenticIdentity.FromProperties(incomingCoreActivity.Recipient?.Properties), |
| 80 | null, |
| 81 | cancellationToken); |
| 82 | |
| 83 | await Task.Delay(500, cancellationToken); |
| 84 | |
| 85 | await teamsBotApp.ConversationClient.DeleteReactionAsync( |
| 86 | turnContext.Activity.Conversation.Id, |
| 87 | res.Id, |
| 88 | "laugh", |
| 89 | //new Uri("https://pilot1.botapi.skype.com/amer/9a9b49fd-1dc5-4217-88b3-ecf855e91b0e/"), |
| 90 | incomingCoreActivity.ServiceUrl!, |
| 91 | AgenticIdentity.FromProperties(incomingCoreActivity.Recipient?.Properties), |
| 92 | null, |
| 93 | cancellationToken); |
| 94 | |
| 95 | Attachment attachment = new() |
| 96 | { |
| 97 | ContentType = "application/vnd.microsoft.card.adaptive", |
| 98 | Content = Cards.FeedbackCardObj |
| 99 | }; |
| 100 | IMessageActivity attachmentReply = MessageFactory.Attachment(attachment); |
| 101 | await turnContext.SendActivityAsync(attachmentReply, cancellationToken); |
| 102 | |
| 103 | } |
| 104 | |
| 105 | |
| 106 | protected override async Task OnMessageReactionActivityAsync(ITurnContext<IMessageReactionActivity> turnContext, CancellationToken cancellationToken) |
| 107 | { |
| 108 | await turnContext.SendActivityAsync(MessageFactory.Text("Message reaction received."), cancellationToken); |
| 109 | } |
| 110 | |
| 111 | protected override async Task OnInstallationUpdateActivityAsync(ITurnContext<IInstallationUpdateActivity> turnContext, CancellationToken cancellationToken) |
| 112 | { |
| 113 | await turnContext.SendActivityAsync(MessageFactory.Text("Installation update received."), cancellationToken); |
| 114 | await turnContext.SendActivityAsync(MessageFactory.Text($"Send a proactive messages to `/api/notify/{turnContext.Activity.Conversation.Id}`"), cancellationToken); |
| 115 | } |
| 116 | |
| 117 | protected override async Task OnInstallationUpdateAddAsync(ITurnContext<IInstallationUpdateActivity> turnContext, CancellationToken cancellationToken) |
| 118 | { |
| 119 | await turnContext.SendActivityAsync(MessageFactory.Text("Installation update Add received."), cancellationToken); |
| 120 | await turnContext.SendActivityAsync(MessageFactory.Text($"Send a proactive messages to `/api/notify/{turnContext.Activity.Conversation.Id}`"), cancellationToken); |
| 121 | } |
| 122 | |
| 123 | protected override async Task<Microsoft.Bot.Builder.InvokeResponse> OnInvokeActivityAsync(ITurnContext<IInvokeActivity> turnContext, CancellationToken cancellationToken) |
| 124 | { |
| 125 | logger.LogInformation("Invoke Activity received: {Name}", turnContext.Activity.Name); |
| 126 | JObject actionValue = JObject.FromObject(turnContext.Activity.Value); |
| 127 | JObject? action = actionValue["action"] as JObject; |
| 128 | JObject? actionData = action?["data"] as JObject; |
| 129 | string? userInput = actionData?["feedback"]?.ToString(); |
| 130 | //var userInput = actionValue["userInput"]?.ToString(); |
| 131 | |
| 132 | logger.LogInformation("Action: {Action}, User Input: {UserInput}", action, userInput); |
| 133 | |
| 134 | |
| 135 | |
| 136 | Attachment attachment = new() |
| 137 | { |
| 138 | ContentType = "application/vnd.microsoft.card.adaptive", |
| 139 | Content = Cards.ResponseCard(userInput) |
| 140 | }; |
| 141 | |
| 142 | IMessageActivity card = MessageFactory.Attachment(attachment); |
| 143 | await turnContext.SendActivityAsync(card, cancellationToken); |
| 144 | |
| 145 | return new Microsoft.Bot.Builder.InvokeResponse |
| 146 | { |
| 147 | Status = 200, |
| 148 | Body = new { value = "invokes from compat bot" } |
| 149 | }; |
| 150 | } |
| 151 | |
| 152 | protected override async Task OnMembersAddedAsync(IList<ChannelAccount> membersAdded, ITurnContext<IConversationUpdateActivity> turnContext, CancellationToken cancellationToken) |
| 153 | { |
| 154 | await turnContext.SendActivityAsync(MessageFactory.Text("Welcome."), cancellationToken); |
| 155 | await turnContext.SendActivityAsync(MessageFactory.Text($"Send a proactive messages to `/api/notify/{turnContext.Activity.Conversation.Id}`"), cancellationToken); |
| 156 | } |
| 157 | |
| 158 | protected override Task OnMembersRemovedAsync(IList<ChannelAccount> membersRemoved, ITurnContext<IConversationUpdateActivity> turnContext, CancellationToken cancellationToken) |
| 159 | { |
| 160 | return turnContext.SendActivityAsync(MessageFactory.Text("Bye."), cancellationToken); |
| 161 | } |
| 162 | |
| 163 | protected override async Task OnTeamsMeetingStartAsync(MeetingStartEventDetails meeting, ITurnContext<IEventActivity> turnContext, CancellationToken cancellationToken) |
| 164 | { |
| 165 | await turnContext.SendActivityAsync(MessageFactory.Text("Welcome to meeting: "), cancellationToken); |
| 166 | await turnContext.SendActivityAsync(MessageFactory.Text($"{meeting.Title} {meeting.MeetingType}"), cancellationToken); |
| 167 | } |
| 168 | |
| 169 | private static async Task SendUpdateDeleteActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken) |
| 170 | { |
| 171 | ConversationReference cr = turnContext.Activity.GetConversationReference(); |
| 172 | Activity reply = (Activity)Activity.CreateMessageActivity(); |
| 173 | reply.ApplyConversationReference(cr, isIncoming: false); |
| 174 | reply.Text = "This is a proactive message sent using the Conversations API."; |
| 175 | |
| 176 | ResourceResponse[] res = await turnContext.Adapter.SendActivitiesAsync(turnContext, [reply], cancellationToken); |
| 177 | |
| 178 | await Task.Delay(2000, cancellationToken); |
| 179 | |
| 180 | Activity updatedActivity = (Activity)Activity.CreateMessageActivity(); |
| 181 | updatedActivity.ApplyConversationReference(cr, isIncoming: false); |
| 182 | updatedActivity.Id = res[0].Id; |
| 183 | updatedActivity.Text = "This message has been updated."; |
| 184 | |
| 185 | await turnContext.Adapter.UpdateActivityAsync(turnContext, updatedActivity, cancellationToken); |
| 186 | |
| 187 | await Task.Delay(2000, cancellationToken); |
| 188 | |
| 189 | ConversationReference deleteReference = cr; |
| 190 | deleteReference.ActivityId = res[0].Id; |
| 191 | await turnContext.Adapter.DeleteActivityAsync(turnContext, deleteReference, cancellationToken); |
| 192 | |
| 193 | await turnContext.SendActivityAsync(MessageFactory.Text("Proactive message sent and deleted."), cancellationToken); |
| 194 | } |
| 195 | |
| 196 | } |
| 197 | |