|
| 1 | +# Handling Attachments |
| 2 | + |
| 3 | +Bot Framework v4 handling attachments bot sample |
| 4 | + |
| 5 | +This bot has been created using [Bot Framework](https://dev.botframework.com), it shows how to send outgoing attachments and how to save attachments to disk. |
| 6 | + |
| 7 | +> **NOTE: A specific example for Microsoft Teams, demonstrating how to |
| 8 | +upload files to Teams from a bot and how to receive a file sent to a bot as an attachment, can be found [here](../56.teams-file-upload)** |
| 9 | + |
| 10 | +## Prerequisites |
| 11 | + |
| 12 | +- Java 1.8+ |
| 13 | +- Install [Maven](https://maven.apache.org/) |
| 14 | +- An account on [Azure](https://azure.microsoft.com) if you want to deploy to Azure. |
| 15 | + |
| 16 | +## To try this sample locally |
| 17 | +- From the root of this project folder: |
| 18 | + - Build the sample using `mvn package` |
| 19 | + - Run it by using `java -jar .\target\bot-attachments-sample.jar` |
| 20 | + |
| 21 | +- Test the bot using Bot Framework Emulator |
| 22 | + |
| 23 | + [Bot Framework Emulator](https://github.com/microsoft/botframework-emulator) is a desktop application that allows bot developers to test and debug their bots on localhost or running remotely through a tunnel. |
| 24 | + |
| 25 | + - Install the Bot Framework Emulator version 4.3.0 or greater from [here](https://github.com/Microsoft/BotFramework-Emulator/releases) |
| 26 | + |
| 27 | + - Connect to the bot using Bot Framework Emulator |
| 28 | + |
| 29 | + - Launch Bot Framework Emulator |
| 30 | + - File -> Open Bot |
| 31 | + - Enter a Bot URL of `http://localhost:3978/api/messages` |
| 32 | + |
| 33 | +## Interacting with the bot |
| 34 | + |
| 35 | +A message exchange between user and bot may contain cards and media attachments, such as images, video, audio, and files. |
| 36 | +The types of attachments that may be sent and received varies by channel. Additionally, a bot may also receive file attachments. |
| 37 | + |
| 38 | +## Deploy the bot to Azure |
| 39 | + |
| 40 | +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. |
| 41 | + |
| 42 | +### 1. Login to Azure |
| 43 | +From a command (or PowerShell) prompt in the root of the bot folder, execute: |
| 44 | +`az login` |
| 45 | + |
| 46 | +### 2. Set the subscription |
| 47 | +`az account set --subscription "<azure-subscription>"` |
| 48 | + |
| 49 | +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. |
| 50 | + |
| 51 | +### 3. Create an App registration |
| 52 | +`az ad app create --display-name "<botname>" --password "<appsecret>" --available-to-other-tenants` |
| 53 | + |
| 54 | +Replace `<botname>` and `<appsecret>` with your own values. |
| 55 | + |
| 56 | +`<botname>` is the unique name of your bot. |
| 57 | +`<appsecret>` is a minimum 16 character password for your bot. |
| 58 | + |
| 59 | +Record the `appid` from the returned JSON |
| 60 | + |
| 61 | +### 4. Create the Azure resources |
| 62 | +Replace the values for `<appid>`, `<appsecret>`, `<botname>`, and `<groupname>` in the following commands: |
| 63 | + |
| 64 | +#### To a new Resource Group |
| 65 | +`az deployment sub create --name "echoBotDeploy" --location "westus" --template-file ".\deploymentTemplates\template-with-new-rg.json" --parameters appId="<appid>" appSecret="<appsecret>" botId="<botname>" botSku=S1 newAppServicePlanName="echoBotPlan" newWebAppName="echoBot" groupLocation="westus" newAppServicePlanLocation="westus"` |
| 66 | + |
| 67 | +#### To an existing Resource Group |
| 68 | +`az deployment group create --resource-group "<groupname>" --template-file ".\deploymentTemplates\template-with-preexisting-rg.json" --parameters appId="<appid>" appSecret="<appsecret>" botId="<botname>" newWebAppName="echoBot" newAppServicePlanName="echoBotPlan" appServicePlanLocation="westus" --name "echoBot"` |
| 69 | + |
| 70 | +### 5. Update app id and password |
| 71 | +In src/main/resources/application.properties update |
| 72 | + - `MicrosoftAppPassword` with the botsecret value |
| 73 | + - `MicrosoftAppId` with the appid from the first step |
| 74 | + |
| 75 | +### 6. Deploy the code |
| 76 | +- Execute `mvn clean package` |
| 77 | +- Execute `mvn azure-webapp:deploy -Dgroupname="<groupname>" -Dbotname="<botname>"` |
| 78 | + |
| 79 | +If the deployment is successful, you will be able to test it via "Test in Web Chat" from the Azure Portal using the "Bot Channel Registration" for the bot. |
| 80 | + |
| 81 | +After the bot is deployed, you only need to execute #6 if you make changes to the bot. |
| 82 | + |
| 83 | + |
| 84 | +## Further reading |
| 85 | + |
| 86 | +- [Maven Plugin for Azure App Service](https://docs.microsoft.com/en-us/java/api/overview/azure/maven/azure-webapp-maven-plugin/readme?view=azure-java-stable) |
| 87 | +- [Spring Boot](https://spring.io/projects/spring-boot) |
| 88 | +- [Azure for Java cloud developers](https://docs.microsoft.com/en-us/azure/java/?view=azure-java-stable) |
| 89 | +- [Bot Framework Documentation](https://docs.botframework.com) |
| 90 | +- [Bot Basics](https://docs.microsoft.com/azure/bot-service/bot-builder-basics?view=azure-bot-service-4.0) |
| 91 | +- [Attachments](https://docs.microsoft.com/en-us/azure/bot-service/nodejs/bot-builder-nodejs-send-receive-attachments?view=azure-bot-service-4.0) |
| 92 | +- [Activity processing](https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-concept-activity-processing?view=azure-bot-service-4.0) |
| 93 | +- [Azure Bot Service Introduction](https://docs.microsoft.com/azure/bot-service/bot-service-overview-introduction?view=azure-bot-service-4.0) |
| 94 | +- [Azure Bot Service Documentation](https://docs.microsoft.com/azure/bot-service/?view=azure-bot-service-4.0) |
| 95 | +- [Azure CLI](https://docs.microsoft.com/cli/azure/?view=azure-cli-latest) |
| 96 | +- [Azure Portal](https://portal.azure.com) |
| 97 | +- [Channels and Bot Connector Service](https://docs.microsoft.com/en-us/azure/bot-service/bot-concepts?view=azure-bot-service-4.0) |
0 commit comments