microsoft/teams.net
Publicmirrored from https://github.com/microsoft/teams.netAvailable
core/samples/CompatBot/EchoBot.cs
157lines · 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 Microsoft.Teams.Bot.Apps; |
| 13 | using Microsoft.Teams.Bot.Apps.Schema; |
| 14 | using Newtonsoft.Json.Linq; |
| 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 | // TeamsAPXClient provides Teams-specific operations like: |
| 44 | // - FetchTeamDetailsAsync, FetchChannelListAsync |
| 45 | // - FetchMeetingInfoAsync, FetchParticipantAsync, SendMeetingNotificationAsync |
| 46 | // - Batch messaging: SendMessageToListOfUsersAsync, SendMessageToAllUsersInTenantAsync, etc. |
| 47 | |
| 48 | await SendUpdateDeleteActivityAsync(turnContext, teamsBotApp.ConversationClient, cancellationToken); |
| 49 | |
| 50 | var attachment = new Attachment |
| 51 | { |
| 52 | ContentType = "application/vnd.microsoft.card.adaptive", |
| 53 | Content = Cards.FeedbackCardObj |
| 54 | }; |
| 55 | var attachmentReply = MessageFactory.Attachment(attachment); |
| 56 | await turnContext.SendActivityAsync(attachmentReply, cancellationToken); |
| 57 | |
| 58 | } |
| 59 | |
| 60 | |
| 61 | protected override async Task OnMessageReactionActivityAsync(ITurnContext<IMessageReactionActivity> turnContext, CancellationToken cancellationToken) |
| 62 | { |
| 63 | await turnContext.SendActivityAsync(MessageFactory.Text("Message reaction received."), cancellationToken); |
| 64 | } |
| 65 | |
| 66 | protected override async Task OnInstallationUpdateActivityAsync(ITurnContext<IInstallationUpdateActivity> turnContext, CancellationToken cancellationToken) |
| 67 | { |
| 68 | await turnContext.SendActivityAsync(MessageFactory.Text("Installation update received."), cancellationToken); |
| 69 | await turnContext.SendActivityAsync(MessageFactory.Text($"Send a proactive messages to `/api/notify/{turnContext.Activity.Conversation.Id}`"), cancellationToken); |
| 70 | } |
| 71 | |
| 72 | protected override async Task OnInstallationUpdateAddAsync(ITurnContext<IInstallationUpdateActivity> turnContext, CancellationToken cancellationToken) |
| 73 | { |
| 74 | await turnContext.SendActivityAsync(MessageFactory.Text("Installation update Add received."), cancellationToken); |
| 75 | await turnContext.SendActivityAsync(MessageFactory.Text($"Send a proactive messages to `/api/notify/{turnContext.Activity.Conversation.Id}`"), cancellationToken); |
| 76 | } |
| 77 | |
| 78 | protected override async Task<InvokeResponse> OnInvokeActivityAsync(ITurnContext<IInvokeActivity> turnContext, CancellationToken cancellationToken) |
| 79 | { |
| 80 | logger.LogInformation("Invoke Activity received: {Name}", turnContext.Activity.Name); |
| 81 | var actionValue = JObject.FromObject(turnContext.Activity.Value); |
| 82 | var action = actionValue["action"] as JObject; |
| 83 | var actionData = action?["data"] as JObject; |
| 84 | var userInput = actionData?["feedback"]?.ToString(); |
| 85 | //var userInput = actionValue["userInput"]?.ToString(); |
| 86 | |
| 87 | logger.LogInformation("Action: {Action}, User Input: {UserInput}", action, userInput); |
| 88 | |
| 89 | |
| 90 | |
| 91 | var attachment = new Attachment |
| 92 | { |
| 93 | ContentType = "application/vnd.microsoft.card.adaptive", |
| 94 | Content = Cards.ResponseCard(userInput) |
| 95 | }; |
| 96 | |
| 97 | var card = MessageFactory.Attachment(attachment); |
| 98 | await turnContext.SendActivityAsync(card, cancellationToken); |
| 99 | |
| 100 | return new InvokeResponse |
| 101 | { |
| 102 | Status = 200, |
| 103 | Body = new { value = "invokes from compat bot" } |
| 104 | }; |
| 105 | } |
| 106 | |
| 107 | protected override async Task OnMembersAddedAsync(IList<ChannelAccount> membersAdded, ITurnContext<IConversationUpdateActivity> turnContext, CancellationToken cancellationToken) |
| 108 | { |
| 109 | await turnContext.SendActivityAsync(MessageFactory.Text("Welcome."), cancellationToken); |
| 110 | await turnContext.SendActivityAsync(MessageFactory.Text($"Send a proactive messages to `/api/notify/{turnContext.Activity.Conversation.Id}`"), cancellationToken); |
| 111 | } |
| 112 | |
| 113 | protected override Task OnMembersRemovedAsync(IList<ChannelAccount> membersRemoved, ITurnContext<IConversationUpdateActivity> turnContext, CancellationToken cancellationToken) |
| 114 | { |
| 115 | return turnContext.SendActivityAsync(MessageFactory.Text("Bye."), cancellationToken); |
| 116 | } |
| 117 | |
| 118 | protected override async Task OnTeamsMeetingStartAsync(MeetingStartEventDetails meeting, ITurnContext<IEventActivity> turnContext, CancellationToken cancellationToken) |
| 119 | { |
| 120 | await turnContext.SendActivityAsync(MessageFactory.Text("Welcome to meeting: "), cancellationToken); |
| 121 | await turnContext.SendActivityAsync(MessageFactory.Text($"{meeting.Title} {meeting.MeetingType}"), cancellationToken); |
| 122 | } |
| 123 | |
| 124 | private static async Task SendUpdateDeleteActivityAsync(ITurnContext<IMessageActivity> turnContext, ConversationClient conversationClient, CancellationToken cancellationToken) |
| 125 | { |
| 126 | var cr = turnContext.Activity.GetConversationReference(); |
| 127 | Activity reply = (Activity)Activity.CreateMessageActivity(); |
| 128 | reply.ApplyConversationReference(cr, isIncoming: false); |
| 129 | reply.Text = "This is a proactive message sent using the Conversations API."; |
| 130 | |
| 131 | CoreActivity ca = reply.FromCompatActivity(); |
| 132 | |
| 133 | var res = await conversationClient.SendActivityAsync(ca, null, cancellationToken); |
| 134 | |
| 135 | await Task.Delay(2000, cancellationToken); |
| 136 | |
| 137 | await conversationClient.UpdateActivityAsync( |
| 138 | cr.Conversation.Id, |
| 139 | res.Id!, |
| 140 | TeamsActivity.CreateBuilder() |
| 141 | .WithId(res.Id ?? "") |
| 142 | .WithServiceUrl(new Uri(turnContext.Activity.ServiceUrl)) |
| 143 | .WithType(ActivityType.Message) |
| 144 | .WithText("This message has been updated.") |
| 145 | .WithFrom(ca.From) |
| 146 | .Build(), |
| 147 | null, |
| 148 | cancellationToken); |
| 149 | |
| 150 | await Task.Delay(2000, cancellationToken); |
| 151 | |
| 152 | await conversationClient.DeleteActivityAsync(cr.Conversation.Id, res.Id!, new Uri(turnContext.Activity.ServiceUrl), AgenticIdentity.FromProperties(ca.From.Properties), null, cancellationToken); |
| 153 | |
| 154 | await turnContext.SendActivityAsync(MessageFactory.Text("Proactive message sent and deleted."), cancellationToken); |
| 155 | } |
| 156 | |
| 157 | } |
| 158 | |