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..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 @@ -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,7 @@ 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()); } } @@ -34,7 +36,7 @@ private MessagingExtensionActionResponse CreateCardCommand(ITurnContext(); - + var card = new HeroCard { Title = createCardData.Title, @@ -49,7 +51,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" ] } ] }