From 18066830feeef5f56f9edbb5e433510103b103a2 Mon Sep 17 00:00:00 2001 From: Wajeed-msft <31851992+Wajeed-msft@users.noreply.github.com> Date: Thu, 7 Jan 2021 23:50:30 +0530 Subject: [PATCH 1/2] Added Just In Time installation flow for messaging extension action --- .../Bots/TeamsMessagingExtensionsActionBot.cs | 74 ++++++++++++++++++- .../README.md | 4 + .../Resources/adaptiveCard.json | 19 +++++ .../Resources/justintimeinstallation.json | 22 ++++++ .../TeamsAppManifest/manifest.json | 20 +++++ 5 files changed, 135 insertions(+), 4 deletions(-) create mode 100644 samples/csharp_dotnetcore/51.teams-messaging-extensions-action/Resources/adaptiveCard.json create mode 100644 samples/csharp_dotnetcore/51.teams-messaging-extensions-action/Resources/justintimeinstallation.json diff --git a/samples/csharp_dotnetcore/51.teams-messaging-extensions-action/Bots/TeamsMessagingExtensionsActionBot.cs b/samples/csharp_dotnetcore/51.teams-messaging-extensions-action/Bots/TeamsMessagingExtensionsActionBot.cs index 00b00e7aba..a1d0613b15 100644 --- a/samples/csharp_dotnetcore/51.teams-messaging-extensions-action/Bots/TeamsMessagingExtensionsActionBot.cs +++ b/samples/csharp_dotnetcore/51.teams-messaging-extensions-action/Bots/TeamsMessagingExtensionsActionBot.cs @@ -3,12 +3,14 @@ using System; using System.Collections.Generic; +using System.IO; using System.Threading; using System.Threading.Tasks; using Microsoft.Bot.Builder; using Microsoft.Bot.Builder.Teams; using Microsoft.Bot.Schema; using Microsoft.Bot.Schema.Teams; +using Newtonsoft.Json; using Newtonsoft.Json.Linq; namespace Microsoft.BotBuilderSamples.Bots @@ -26,7 +28,8 @@ protected override async Task OnTeamsMessaging case "shareMessage": return ShareMessageCommand(turnContext, action); default: - throw new NotImplementedException($"Invalid CommandId: {action.CommandId}"); + return await Task.FromResult(new MessagingExtensionActionResponse()); + // throw new NotImplementedException($"Invalid CommandId: {action.CommandId}"); } } @@ -34,7 +37,7 @@ private MessagingExtensionActionResponse CreateCardCommand(ITurnContext(); - + var card = new HeroCard { Title = createCardData.Title, @@ -49,7 +52,7 @@ private MessagingExtensionActionResponse CreateCardCommand(ITurnContext OnTeamsMessagingExtensionFetchTaskAsync(ITurnContext turnContext, MessagingExtensionAction action, CancellationToken cancellationToken) + { + // we are handling two cases within try/catch block + //if the bot is installed it will create adaptive card attachment and show card with input fields + string memberName; + try + { + // Check if your app is installed by fetching member information. + var member = await TeamsInfo.GetMemberAsync(turnContext, turnContext.Activity.From.Id, cancellationToken); + memberName = member.Name; + } + catch (ErrorResponseException ex) + { + if (ex.Body.Error.Code == "BotNotInConversationRoster") + { + return new MessagingExtensionActionResponse + { + Task = new TaskModuleContinueResponse + { + Value = new TaskModuleTaskInfo + { + Card = GetAdaptiveCardAttachmentFromFile("justintimeinstallation.json"), + Height = 200, + Width = 400, + Title = "Adaptive Card - App Installation", + }, + }, + }; + } + throw; // It's a different error. + } + + return new MessagingExtensionActionResponse + { + Task = new TaskModuleContinueResponse + { + Value = new TaskModuleTaskInfo + { + Card = GetAdaptiveCardAttachmentFromFile("adaptiveCard.json"), + Height = 200, + Width = 400, + Title = $"Welcome {memberName}", + }, + }, + }; + } + + + /// Returns adaptive card attachment which allows Just In Time installation of app. + private static Attachment GetAdaptiveCardAttachmentFromFile(string fileName) + { + //Read the card json and create attachment. + string[] paths = { ".", "Resources", fileName }; + var adaptiveCardJson = File.ReadAllText(Path.Combine(paths)); + + var adaptiveCardAttachment = new Attachment() + { + ContentType = "application/vnd.microsoft.card.adaptive", + Content = JsonConvert.DeserializeObject(adaptiveCardJson), + }; + return adaptiveCardAttachment; + } } } diff --git a/samples/csharp_dotnetcore/51.teams-messaging-extensions-action/README.md b/samples/csharp_dotnetcore/51.teams-messaging-extensions-action/README.md index 280a4d1005..abda4e7c7a 100644 --- a/samples/csharp_dotnetcore/51.teams-messaging-extensions-action/README.md +++ b/samples/csharp_dotnetcore/51.teams-messaging-extensions-action/README.md @@ -58,6 +58,10 @@ or 2) Selecting the **Share Message** command from the Message command list. +or + +3) Selecting the **Fetch Roster** command from the Compose Box command list. You will presented with prompt for Just In Time installation if app is not already added to current team/chat. + ## Deploy the bot to Azure To learn more about deploying a bot to Azure, see [Deploy your bot to Azure](https://aka.ms/azuredeployment) for a complete list of deployment instructions. diff --git a/samples/csharp_dotnetcore/51.teams-messaging-extensions-action/Resources/adaptiveCard.json b/samples/csharp_dotnetcore/51.teams-messaging-extensions-action/Resources/adaptiveCard.json new file mode 100644 index 0000000000..8718e8bf66 --- /dev/null +++ b/samples/csharp_dotnetcore/51.teams-messaging-extensions-action/Resources/adaptiveCard.json @@ -0,0 +1,19 @@ +{ + "$schema": "http://adaptivecards.io/schemas/adaptive-card.json", + "version": "1.0", + "type": "AdaptiveCard", + "body": [ + { + "type": "TextBlock", + "text": "This app is installed in this conversation. You can now use it to do some great stuff!!", + "isSubtle": false, + "wrap": true + } + ], + "actions": [ + { + "type": "Action.Submit", + "title": "Close" + } + ] +} diff --git a/samples/csharp_dotnetcore/51.teams-messaging-extensions-action/Resources/justintimeinstallation.json b/samples/csharp_dotnetcore/51.teams-messaging-extensions-action/Resources/justintimeinstallation.json new file mode 100644 index 0000000000..5557814174 --- /dev/null +++ b/samples/csharp_dotnetcore/51.teams-messaging-extensions-action/Resources/justintimeinstallation.json @@ -0,0 +1,22 @@ +{ + "type": "AdaptiveCard", + "body": [ + { + "type": "TextBlock", + "text": "Looks like you haven't used Action Messaging Extension app in this team/chat. Please click **Continue** to add this app.", + "wrap": true + } + ], + "actions": [ + { + "type": "Action.Submit", + "title": "Continue", + "data": { + "msteams": { + "justInTimeInstall": true + } + } + } + ], + "version": "1.0" +} diff --git a/samples/csharp_dotnetcore/51.teams-messaging-extensions-action/TeamsAppManifest/manifest.json b/samples/csharp_dotnetcore/51.teams-messaging-extensions-action/TeamsAppManifest/manifest.json index 0dcba15628..4904f76132 100644 --- a/samples/csharp_dotnetcore/51.teams-messaging-extensions-action/TeamsAppManifest/manifest.json +++ b/samples/csharp_dotnetcore/51.teams-messaging-extensions-action/TeamsAppManifest/manifest.json @@ -23,6 +23,18 @@ "color": "icon-color.png" }, "accentColor": "#FFFFFF", + "bots": [ + { + "botId": "<>", + "needsChannelSelector": false, + "isNotificationOnly": false, + "scopes": [ + "team", + "personal", + "groupchat" + ] + } + ], "composeExtensions": [ { "botId": "<>", @@ -68,6 +80,14 @@ "inputType": "toggle" } ] + }, + { + "id": "FetchRoster", + "description": "Fetch the conversation roster", + "title": "FetchRoster", + "type": "action", + "fetchTask": true, + "context": [ "compose" ] } ] } From 21ff2d437e65ff58e789c069344b603e287ddcec Mon Sep 17 00:00:00 2001 From: Eric Dahlvang Date: Wed, 13 Jan 2021 15:40:34 -0800 Subject: [PATCH 2/2] Update samples/csharp_dotnetcore/51.teams-messaging-extensions-action/Bots/TeamsMessagingExtensionsActionBot.cs --- .../Bots/TeamsMessagingExtensionsActionBot.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/samples/csharp_dotnetcore/51.teams-messaging-extensions-action/Bots/TeamsMessagingExtensionsActionBot.cs b/samples/csharp_dotnetcore/51.teams-messaging-extensions-action/Bots/TeamsMessagingExtensionsActionBot.cs index a1d0613b15..cb29ca5829 100644 --- a/samples/csharp_dotnetcore/51.teams-messaging-extensions-action/Bots/TeamsMessagingExtensionsActionBot.cs +++ b/samples/csharp_dotnetcore/51.teams-messaging-extensions-action/Bots/TeamsMessagingExtensionsActionBot.cs @@ -29,7 +29,6 @@ protected override async Task OnTeamsMessaging return ShareMessageCommand(turnContext, action); default: return await Task.FromResult(new MessagingExtensionActionResponse()); - // throw new NotImplementedException($"Invalid CommandId: {action.CommandId}"); } }