diff --git a/samples/java_springboot/51.teams-messaging-extensions-action/README.md b/samples/java_springboot/51.teams-messaging-extensions-action/README.md index c7e32a4b07..f30b6e0ca6 100644 --- a/samples/java_springboot/51.teams-messaging-extensions-action/README.md +++ b/samples/java_springboot/51.teams-messaging-extensions-action/README.md @@ -6,8 +6,13 @@ Bot Framework v4 Conversation Bot sample for Teams. There are two basic types of Messaging Extension in Teams: [Search-based](https://docs.microsoft.com/en-us/microsoftteams/platform/messaging-extensions/how-to/search-commands/define-search-command) and [Action-based](https://docs.microsoft.com/en-us/microsoftteams/platform/messaging-extensions/how-to/action-commands/define-action-command). This sample illustrates how to build an Action-based Messaging Extension. +This sample is a Spring Boot app and uses the Azure CLI and azure-webapp Maven plugin to deploy to Azure. + ## Prerequisites +- Java 1.8+ +- Install [Maven](https://maven.apache.org/) +- An account on [Azure](https://azure.microsoft.com) if you want to deploy to Azure. - Microsoft Teams is installed and you have an account - [ngrok](https://ngrok.com/) or equivalent tunnelling solution @@ -16,31 +21,21 @@ build an Action-based Messaging Extension. > Note these instructions are for running the sample on your local machine, the tunnelling solution is required because the Teams service needs to call into the bot. -1) Clone the repository - - ```bash - git clone https://github.com/Microsoft/botbuilder-java.git - ``` - -1) Run ngrok - point to port 3978 +- Run ngrok http -host-header=rewrite 3978 - ```bash - ngrok http -host-header=rewrite 3978 - ``` - -1) Create [Bot Framework registration resource](https://docs.microsoft.com/en-us/azure/bot-service/bot-service-quickstart-registration) in Azure +- Create [Bot Framework registration resource](https://docs.microsoft.com/en-us/azure/bot-service/bot-service-quickstart-registration) in Azure - Use the current `https` URL you were given by running ngrok. Append with the path `/api/messages` used by this sample - Ensure that you've [enabled the Teams Channel](https://docs.microsoft.com/en-us/azure/bot-service/channel-connect-teams?view=azure-bot-service-4.0) - __*If you don't have an Azure account*__ you can use this [Bot Framework registration](https://docs.microsoft.com/en-us/microsoftteams/platform/bots/how-to/create-a-bot-for-teams#register-your-web-service-with-the-bot-framework) -1) Update the `resources/application.properties` configuration for the bot to use the Microsoft App Id and App Password from the Bot Framework registration. (Note the App Password is referred to as the "client secret" in the azure portal and you can always create a new client secret anytime.) +- Update the `resources/application.properties` configuration for the bot to use the Microsoft App Id and App Password from the Bot Framework registration. (Note the App Password is referred to as the "client secret" in the azure portal and you can always create a new client secret anytime.) -1) __*This step is specific to Teams.*__ +- __*This step is specific to Teams.*__ - **Edit** the `manifest.json` contained in the `teamsAppManifest` folder to replace your Microsoft App Id (that was created when you registered your bot earlier) *everywhere* you see the place holder string `<>` (depending on the scenario the Microsoft App Id may occur multiple times in the `manifest.json`) - **Zip** up the contents of the `teamsAppManifest` folder to create a `manifest.zip` - **Upload** the `manifest.zip` to Teams (in the Apps view click "Upload a custom app") -1) From the root of this project folder: +- From the root of this project folder: - Build the sample using `mvn package` - Unless done previously, install the packages in the local cache by using `mvn install` - Run it by using `java -jar .\target\bot-teams-messaging-extensions-action-sample.jar` @@ -58,9 +53,55 @@ or ## 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. +As described on [Deploy your bot](https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-deploy-az-cli), you will perform the first 4 steps to setup the Azure app, then deploy the code using the azure-webapp Maven plugin. + +### 1. Login to Azure +From a command (or PowerShell) prompt in the root of the bot folder, execute: +`az login` + +### 2. Set the subscription +`az account set --subscription ""` + +If you aren't sure which subscription to use for deploying the bot, you can view the list of subscriptions for your account by using `az account list` command. + +### 3. Create an App registration +`az ad app create --display-name "" --password "" --available-to-other-tenants` + +Replace `` and `` with your own values. + +`` is the unique name of your bot. +`` is a minimum 16 character password for your bot. + +Record the `appid` from the returned JSON + +### 4. Create the Azure resources +Replace the values for ``, ``, ``, and `` in the following commands: + +#### To a new Resource Group +``` +az deployment sub create --name "teamsActionDeploy" --location "westus" --template-file ".\deploymentTemplates\template-with-new-rg.json" --parameters appId="" appSecret="" botId="" botSku=S1 newAppServicePlanName="teamsActionPlan" newWebAppName="teamsActionBot" groupLocation="westus" newAppServicePlanLocation="westus" +``` + +#### To an existing Resource Group +``` +az deployment group create --resource-group "" --template-file ".\deploymentTemplates\template-with-preexisting-rg.json" --parameters appId="" appSecret="" botId="" newWebAppName="teamsActionBot" newAppServicePlanName="teamsActionPlan" appServicePlanLocation="westus" --name "teamsActionBot" +``` + +### 5. Update app id and password +In src/main/resources/application.properties update +- `MicrosoftAppPassword` with the botsecret value +- `MicrosoftAppId` with the appid from the first step + +### 6. Deploy the code +- Execute `mvn clean package` +- Execute `mvn azure-webapp:deploy -Dgroupname="" -Dbotname=""` + +After the bot is deployed, you only need to execute #6 if you make changes to the bot. ## Further reading +- [Spring Boot](https://spring.io/projects/spring-boot) +- [Maven Plugin for Azure App Service](https://github.com/microsoft/azure-maven-plugins/tree/develop/azure-webapp-maven-plugin) - [How Microsoft Teams bots work](https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-basics-teams?view=azure-bot-service-4.0&tabs=javascript) +- [Azure for Java cloud developers](https://docs.microsoft.com/en-us/azure/java/?view=azure-java-stable) diff --git a/samples/java_springboot/51.teams-messaging-extensions-action/src/main/java/com/microsoft/bot/sample/teamsaction/TeamsMessagingExtensionsActionBot.java b/samples/java_springboot/51.teams-messaging-extensions-action/src/main/java/com/microsoft/bot/sample/teamsaction/TeamsMessagingExtensionsActionBot.java index 2ed3d9f5fa..2d8e8f12b1 100644 --- a/samples/java_springboot/51.teams-messaging-extensions-action/src/main/java/com/microsoft/bot/sample/teamsaction/TeamsMessagingExtensionsActionBot.java +++ b/samples/java_springboot/51.teams-messaging-extensions-action/src/main/java/com/microsoft/bot/sample/teamsaction/TeamsMessagingExtensionsActionBot.java @@ -7,9 +7,14 @@ import com.microsoft.bot.builder.teams.TeamsActivityHandler; import com.microsoft.bot.schema.CardImage; import com.microsoft.bot.schema.HeroCard; -import com.microsoft.bot.schema.teams.*; - -import java.util.*; +import com.microsoft.bot.schema.teams.MessagingExtensionAction; +import com.microsoft.bot.schema.teams.MessagingExtensionActionResponse; +import com.microsoft.bot.schema.teams.MessagingExtensionAttachment; +import com.microsoft.bot.schema.teams.MessagingExtensionResult; + +import java.util.Arrays; +import java.util.List; +import java.util.Map; import java.util.concurrent.CompletableFuture; /** @@ -42,6 +47,7 @@ private CompletableFuture createCardCommand( TurnContext turnContext, MessagingExtensionAction action ) { + // The user has chosen to create a card by choosing the 'Create Card' context menu command. Map actionData = (Map) action.getData(); HeroCard card = new HeroCard(); @@ -69,19 +75,25 @@ private CompletableFuture shareMessageCommand( TurnContext turnContext, MessagingExtensionAction action ) { + // The user has chosen to share a message by choosing the 'Share Message' context menu command. Map actionData = (Map) action.getData(); HeroCard card = new HeroCard(); card.setTitle( - action.getMessagePayload().getFrom().getUser() != null ? action.getMessagePayload() - .getFrom().getUser().getDisplayName() : ""); + action.getMessagePayload().getFrom() != null && action.getMessagePayload().getFrom().getUser() != null + ? action.getMessagePayload().getFrom().getUser().getDisplayName() : ""); card.setText(action.getMessagePayload().getBody().getContent()); if (action.getMessagePayload().getAttachments() != null && !action.getMessagePayload() .getAttachments().isEmpty()) { - card.setSubtitle("Attachments not included)"); + // This sample does not add the MessagePayload Attachments. This is left as an + // exercise for the user. + card.setSubtitle(String.format("(%d Attachments not included)", + action.getMessagePayload().getAttachments().size())); } + // This Messaging Extension example allows the user to check a box to include an image with the + // shared message. This demonstrates sending custom parameters along with the message payload. boolean includeImage = actionData.get("includeImage") != null ? ( Boolean.valueOf(actionData.get("includeImage")) ) : false; diff --git a/samples/java_springboot/51.teams-messaging-extensions-action/src/main/java/com/microsoft/bot/sample/teamsaction/package-info.java b/samples/java_springboot/51.teams-messaging-extensions-action/src/main/java/com/microsoft/bot/sample/teamsaction/package-info.java new file mode 100644 index 0000000000..adad2856cd --- /dev/null +++ b/samples/java_springboot/51.teams-messaging-extensions-action/src/main/java/com/microsoft/bot/sample/teamsaction/package-info.java @@ -0,0 +1,8 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See License.txt in the project root for +// license information. + +/** + * This package contains the classes for the Teams Messaging Extension Action sample. + */ +package com.microsoft.bot.sample.teamsaction; \ No newline at end of file diff --git a/samples/java_springboot/51.teams-messaging-extensions-action/src/main/webapp/index.html b/samples/java_springboot/51.teams-messaging-extensions-action/src/main/webapp/index.html index d5ba5158e9..a200688b3e 100644 --- a/samples/java_springboot/51.teams-messaging-extensions-action/src/main/webapp/index.html +++ b/samples/java_springboot/51.teams-messaging-extensions-action/src/main/webapp/index.html @@ -4,7 +4,7 @@ - EchoBot + Teams Action Based Messaging Extension