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

Commit 9ab46f5

Browse files
authored
Merge pull request #412 from microsoft/trboehre/teams-fileupload
56.teams-file-upload
2 parents f1747bb + ad07179 commit 9ab46f5

File tree

19 files changed

+1657
-0
lines changed

19 files changed

+1657
-0
lines changed
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: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
2+
# Teams File Upload Bot
3+
4+
Bot Framework v4 file upload bot sample for Teams.
5+
6+
This bot has been created using [Bot Framework](https://dev.botframework.com), it shows how to
7+
upload files to Teams from a bot and how to receive a file sent to a bot as an attachment.
8+
9+
## Prerequisites
10+
11+
- Microsoft Teams is installed and you have an account
12+
- [ngrok](https://ngrok.com/) or equivalent tunnelling solution
13+
14+
## To try this sample
15+
16+
> Note these instructions are for running the sample on your local machine, the tunnelling solution is required because
17+
the Teams service needs to call into the bot.
18+
19+
1) Clone the repository
20+
21+
```bash
22+
git clone https://github.com/Microsoft/botbuilder-java.git
23+
```
24+
25+
1) Run ngrok - point to port 8080
26+
27+
```bash
28+
ngrok http -host-header=rewrite 8080
29+
```
30+
31+
1) Create [Bot Framework registration resource](https://docs.microsoft.com/en-us/azure/bot-service/bot-service-quickstart-registration) in Azure
32+
- Use the current `https` URL you were given by running ngrok. Append with the path `/api/messages` used by this sample
33+
- 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)
34+
- __*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)
35+
36+
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.)
37+
38+
1) __*This step is specific to Teams.*__
39+
- **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 `<<YOUR-MICROSOFT-APP-ID>>` (depending on the scenario the Microsoft App Id may occur multiple times in the `manifest.json`)
40+
- **Zip** up the contents of the `teamsAppManifest` folder to create a `manifest.zip`
41+
- **Upload** the `manifest.zip` to Teams (in the Apps view click "Upload a custom app")
42+
43+
1) From the root of this project folder:
44+
- Build the sample using `mvn package`
45+
- Unless done previously, install the packages in the local cache by using `mvn install`
46+
- Run it by using `java -jar .\target\bot-teams-file-upload-sample.jar`
47+
48+
49+
## Interacting with the bot in Teams
50+
51+
> Note this `manifest.json` specified that the bot will be installed in "personal" scope which is why you immediately entered a one on one chat conversation with the bot. Please refer to Teams documentation for more details.
52+
53+
Sending a message to the bot will cause it to respond with a card that will prompt you to upload a file. The file that's being uploaded is the `teams-logo.png` in the `Files` directory in this sample. The `Accept` and `Decline` events illustrated in this sample are specific to Teams. You can message the bot again to receive another prompt.
54+
55+
You can also send a file to the bot as an attachment in the message compose section in Teams. This will be delivered to the bot as a Message Activity and the code in this sample fetches and saves the file.
56+
57+
## Deploy the bot to Azure
58+
59+
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.
60+
61+
## Further reading
62+
63+
- [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)
64+
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
3+
"contentVersion": "1.0.0.0",
4+
"parameters": {
5+
"groupLocation": {
6+
"value": ""
7+
},
8+
"groupName": {
9+
"value": ""
10+
},
11+
"appId": {
12+
"value": ""
13+
},
14+
"appSecret": {
15+
"value": ""
16+
},
17+
"botId": {
18+
"value": ""
19+
},
20+
"botSku": {
21+
"value": ""
22+
},
23+
"newAppServicePlanName": {
24+
"value": ""
25+
},
26+
"newAppServicePlanSku": {
27+
"value": {
28+
"name": "S1",
29+
"tier": "Standard",
30+
"size": "S1",
31+
"family": "S",
32+
"capacity": 1
33+
}
34+
},
35+
"newAppServicePlanLocation": {
36+
"value": ""
37+
},
38+
"newWebAppName": {
39+
"value": ""
40+
}
41+
}
42+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
3+
"contentVersion": "1.0.0.0",
4+
"parameters": {
5+
"appId": {
6+
"value": ""
7+
},
8+
"appSecret": {
9+
"value": ""
10+
},
11+
"botId": {
12+
"value": ""
13+
},
14+
"botSku": {
15+
"value": ""
16+
},
17+
"newAppServicePlanName": {
18+
"value": ""
19+
},
20+
"newAppServicePlanSku": {
21+
"value": {
22+
"name": "S1",
23+
"tier": "Standard",
24+
"size": "S1",
25+
"family": "S",
26+
"capacity": 1
27+
}
28+
},
29+
"appServicePlanLocation": {
30+
"value": ""
31+
},
32+
"existingAppServicePlan": {
33+
"value": ""
34+
},
35+
"newWebAppName": {
36+
"value": ""
37+
}
38+
}
39+
}
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
{
2+
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
3+
"contentVersion": "1.0.0.0",
4+
"parameters": {
5+
"groupLocation": {
6+
"defaultValue": "",
7+
"type": "string",
8+
"metadata": {
9+
"description": "Specifies the location of the Resource Group."
10+
}
11+
},
12+
"groupName": {
13+
"type": "string",
14+
"metadata": {
15+
"description": "Specifies the name of the Resource Group."
16+
}
17+
},
18+
"appId": {
19+
"type": "string",
20+
"metadata": {
21+
"description": "Active Directory App ID, set as MicrosoftAppId in the Web App's Application Settings."
22+
}
23+
},
24+
"appSecret": {
25+
"type": "string",
26+
"metadata": {
27+
"description": "Active Directory App Password, set as MicrosoftAppPassword in the Web App's Application Settings."
28+
}
29+
},
30+
"botId": {
31+
"type": "string",
32+
"metadata": {
33+
"description": "The globally unique and immutable bot ID. Also used to configure the displayName of the bot, which is mutable."
34+
}
35+
},
36+
"botSku": {
37+
"defaultValue": "F0",
38+
"type": "string",
39+
"metadata": {
40+
"description": "The pricing tier of the Bot Service Registration. Acceptable values are F0 and S1."
41+
}
42+
},
43+
"newAppServicePlanName": {
44+
"defaultValue": "",
45+
"type": "string",
46+
"metadata": {
47+
"description": "The name of the App Service Plan."
48+
}
49+
},
50+
"newAppServicePlanSku": {
51+
"type": "object",
52+
"defaultValue": {
53+
"name": "P1v2",
54+
"tier": "PremiumV2",
55+
"size": "P1v2",
56+
"family": "Pv2",
57+
"capacity": 1
58+
},
59+
"metadata": {
60+
"description": "The SKU of the App Service Plan. Defaults to Standard values."
61+
}
62+
},
63+
"newAppServicePlanLocation": {
64+
"defaultValue": "",
65+
"type": "string",
66+
"metadata": {
67+
"description": "The location of the App Service Plan. Defaults to \"westus\"."
68+
}
69+
},
70+
"newWebAppName": {
71+
"type": "string",
72+
"defaultValue": "",
73+
"metadata": {
74+
"description": "The globally unique name of the Web App. Defaults to the value passed in for \"botId\"."
75+
}
76+
}
77+
},
78+
"variables": {
79+
"resourcesLocation": "[deployment().location]",
80+
"effectiveGroupLocation": "[if(empty(parameters('groupLocation')), variables('resourcesLocation'), parameters('groupLocation'))]",
81+
"effectivePlanLocation": "[if(empty(parameters('newAppServicePlanLocation')), variables('resourcesLocation'), parameters('newAppServicePlanLocation'))]",
82+
"appServicePlanName": "[if(empty(parameters('newAppServicePlanName')), concat(parameters('botId'), 'ServicePlan'), parameters('newAppServicePlanName'))]",
83+
"webAppName": "[if(empty(parameters('newWebAppName')), parameters('botId'), parameters('newWebAppName'))]",
84+
"siteHost": "[concat(variables('webAppName'), '.azurewebsites.net')]",
85+
"botEndpoint": "[concat('https://', variables('siteHost'), '/api/messages')]"
86+
},
87+
"resources": [
88+
{
89+
"name": "[parameters('groupName')]",
90+
"type": "Microsoft.Resources/resourceGroups",
91+
"apiVersion": "2018-05-01",
92+
"location": "[variables('effectiveGroupLocation')]",
93+
"properties": {
94+
}
95+
},
96+
{
97+
"type": "Microsoft.Resources/deployments",
98+
"apiVersion": "2018-05-01",
99+
"name": "storageDeployment",
100+
"resourceGroup": "[parameters('groupName')]",
101+
"dependsOn": [
102+
"[resourceId('Microsoft.Resources/resourceGroups/', parameters('groupName'))]"
103+
],
104+
"properties": {
105+
"mode": "Incremental",
106+
"template": {
107+
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
108+
"contentVersion": "1.0.0.0",
109+
"parameters": {},
110+
"variables": {},
111+
"resources": [
112+
{
113+
"comments": "Create a new App Service Plan",
114+
"type": "Microsoft.Web/serverfarms",
115+
"name": "[variables('appServicePlanName')]",
116+
"apiVersion": "2018-02-01",
117+
"location": "[variables('effectivePlanLocation')]",
118+
"sku": "[parameters('newAppServicePlanSku')]",
119+
"kind": "linux",
120+
"properties": {
121+
"name": "[variables('appServicePlanName')]",
122+
"reserved":true
123+
}
124+
},
125+
{
126+
"comments": "Create a Web App using the new App Service Plan",
127+
"type": "Microsoft.Web/sites",
128+
"apiVersion": "2015-08-01",
129+
"location": "[variables('resourcesLocation')]",
130+
"kind": "app",
131+
"dependsOn": [
132+
"[resourceId('Microsoft.Web/serverfarms/', variables('appServicePlanName'))]"
133+
],
134+
"name": "[variables('webAppName')]",
135+
"properties": {
136+
"name": "[variables('webAppName')]",
137+
"serverFarmId": "[variables('appServicePlanName')]",
138+
"siteConfig": {
139+
"appSettings": [
140+
{
141+
"name": "JAVA_OPTS",
142+
"value": "-Dserver.port=80"
143+
},
144+
{
145+
"name": "MicrosoftAppId",
146+
"value": "[parameters('appId')]"
147+
},
148+
{
149+
"name": "MicrosoftAppPassword",
150+
"value": "[parameters('appSecret')]"
151+
}
152+
],
153+
"cors": {
154+
"allowedOrigins": [
155+
"https://botservice.hosting.portal.azure.net",
156+
"https://hosting.onecloud.azure-test.net/"
157+
]
158+
}
159+
}
160+
}
161+
},
162+
{
163+
"apiVersion": "2017-12-01",
164+
"type": "Microsoft.BotService/botServices",
165+
"name": "[parameters('botId')]",
166+
"location": "global",
167+
"kind": "bot",
168+
"sku": {
169+
"name": "[parameters('botSku')]"
170+
},
171+
"properties": {
172+
"name": "[parameters('botId')]",
173+
"displayName": "[parameters('botId')]",
174+
"endpoint": "[variables('botEndpoint')]",
175+
"msaAppId": "[parameters('appId')]",
176+
"developerAppInsightsApplicationId": null,
177+
"developerAppInsightKey": null,
178+
"publishingCredentials": null,
179+
"storageResourceId": null
180+
},
181+
"dependsOn": [
182+
"[resourceId('Microsoft.Web/sites/', variables('webAppName'))]"
183+
]
184+
}
185+
],
186+
"outputs": {}
187+
}
188+
}
189+
}
190+
]
191+
}

0 commit comments

Comments
 (0)