Skip to content
This repository was archived by the owner on Dec 4, 2023. It is now read-only.

Commit b16bdec

Browse files
authored
Added sample 15.handling-attachments (#1047)
* Added sample 15.handling-attachments * Corrected package name in 15.handling-attachments
1 parent 2e577c2 commit b16bdec

File tree

16 files changed

+1740
-0
lines changed

16 files changed

+1740
-0
lines changed

libraries/bot-connector/src/main/java/com/microsoft/bot/connector/Attachments.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,21 @@ public interface Attachments {
3535
* @throws IllegalArgumentException thrown if parameters fail the validation
3636
*/
3737
CompletableFuture<InputStream> getAttachment(String attachmentId, String viewId);
38+
39+
/**
40+
* Get the URI of an attachment view.
41+
* @param attachmentId id of the attachment.
42+
* @param viewId default is "original".
43+
* @return URI of the attachment.
44+
*/
45+
String getAttachmentUri(String attachmentId, String viewId);
46+
47+
/**
48+
* Get the URI of an attachment view.
49+
* @param attachmentId id of the attachment.
50+
* @return URI of the attachment.
51+
*/
52+
default String getAttachmentUri(String attachmentId) {
53+
return getAttachmentUri(attachmentId, "original");
54+
}
3855
}

libraries/bot-connector/src/main/java/com/microsoft/bot/connector/rest/RestAttachments.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
package com.microsoft.bot.connector.rest;
88

99
import com.microsoft.bot.connector.Async;
10+
import java.net.URLEncoder;
11+
import org.apache.commons.lang3.StringUtils;
1012
import retrofit2.Retrofit;
1113
import com.microsoft.bot.connector.Attachments;
1214
import com.google.common.reflect.TypeToken;
@@ -165,4 +167,30 @@ private ServiceResponse<InputStream> getAttachmentDelegate(
165167
.registerError(ErrorResponseException.class)
166168
.build(response);
167169
}
170+
171+
/**
172+
* Get the URI of an attachment view.
173+
* @param attachmentId id of the attachment.
174+
* @param viewId default is "original".
175+
* @return URI of the attachment.
176+
*/
177+
@Override
178+
public String getAttachmentUri(String attachmentId, String viewId) {
179+
if (StringUtils.isEmpty(attachmentId)) {
180+
throw new IllegalArgumentException("Must provide an attachmentId");
181+
}
182+
183+
if (StringUtils.isEmpty(viewId)) {
184+
viewId = "original";
185+
}
186+
187+
String baseUrl = client.baseUrl();
188+
String uri = baseUrl
189+
+ (baseUrl.endsWith("/") ? "" : "/")
190+
+ "v3/attachments/{attachmentId}/views/{viewId}";
191+
uri = uri.replace("{attachmentId}", URLEncoder.encode(attachmentId));
192+
uri = uri.replace("{viewId}", URLEncoder.encode(viewId));
193+
194+
return uri;
195+
}
168196
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) Microsoft Corporation. All rights reserved.
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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

Comments
 (0)