8000 Merge pull request #465 from microsoft/trboehre/Teams-FileUpload · guptarohan41/botbuilder-python@59ff0f2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 59ff0f2

Browse files
authored
Merge pull request microsoft#465 from microsoft/trboehre/Teams-FileUpload
Added TeamsFileBot Scenario
2 parents 1ed9de5 + c0bc2bc commit 59ff0f2

File tree

12 files changed

+475
-1
lines changed

12 files changed

+475
-1
lines changed

libraries/botbuilder-core/botbuilder/core/bot_framework_adapter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ async def send_activities(
399399
)
400400

401401
if not response:
402-
response = ResourceResponse(activity.id or "")
402+
response = ResourceResponse(id=activity.id or "")
403403

404404
responses.append(response)
405405
return responses
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
4+
5+
class ContentType:
6+
O365_CONNECTOR_CARD = "application/vnd.microsoft.teams.card.o365connector"
7+
FILE_CONSENT_CARD = "application/vnd.microsoft.teams.card.file.consent"
8+
FILE_DOWNLOAD_INFO = "application/vnd.microsoft.teams.file.download.info"
9+
FILE_INFO_CARD = "application/vnd.microsoft.teams.card.file.info"
10+
11+
12+
class Type:
13+
O365_CONNECTOR_CARD_VIEWACTION = "ViewAction"
14+
O365_CONNECTOR_CARD_OPEN_URI = "OpenUri"
15+
O365_CONNECTOR_CARD_HTTP_POST = "HttpPOST"
16+
O365_CONNECTOR_CARD_ACTION_CARD = "ActionCard"
17+
O365_CONNECTOR_CARD_TEXT_INPUT = "TextInput"
18+
O365_CONNECTOR_CARD_DATE_INPUT = "DateInput"
19+
O365_CONNECTOR_CARD_MULTICHOICE_INPUT = "MultichoiceInput"

scenarios/file-upload/README.md

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
# FileUpload
2+
3+
Bot Framework v4 echo bot sample.
4+
5+
This bot has been created using [Bot Framework](https://dev.botframework.com), it shows how to create a simple bot that accepts input from the user and echoes it back.
6+
7+
## Prerequisites
8+
- Open Notepad (or another text editor) to save some values as you complete the setup.
9+
10+
- Ngrok setup
11+
1. Download and install [Ngrok](https://ngrok.com/download)
12+
2. In terminal navigate to the directory where Ngrok is installed
13+
3. Run this command: ```ngrok http -host-header=rewrite 3978 ```
14+
4. Copy the https://xxxxxxxx.ngrok.io address and put it into notepad. **NOTE** You want the https address.
15+
16+
- Azure setup
17+
1. Login to the [Azure Portal]((https://portal.azure.com)
18+
2. (optional) create a new resource group if you don't currently have one
19+
3. Go to your resource group
20+
4. Click "Create a new resource"
21+
5. Search for "Bot Channel Registration"
22+
6. Click Create
23+
7. Enter bot name, subscription
24+
8. In the "Messaging endpoint url" enter the ngrok address from earlier.
25+
8a. Finish the url with "/api/messages. It should look like ```https://xxxxxxxxx.ngrok.io/api/messages```
26+
9. Click the "Microsoft App Id and password" box
27+
10. Click on "Create New"
28+
11. Click on "Create App ID in the App Registration Portal"
29+
12. Click "New registration"
30+
13. Enter a name
31+
14. Under "Supported account types" select "Accounts in any organizational directory and personal Microsoft accounts"
32+
15. Click register
33+
16. Copy the application (client) ID and put it in Notepad. Label it "Microsoft App ID"
34+
17. Go to "Certificates & Secrets"
35+
18. Click "+ New client secret"
36+
19. Enter a description
37+
20. Click "Add"
38+
21. Copy the value and put it into Notepad. Label it "Password"
39+
22. (back in the channel registration view) Copy/Paste the Microsoft App ID and Password into their respective fields
40+
23. Click Create
41+
24. Go to "Resource groups" on the left
42+
25. Select the resource group that the bot channel reg was created in
43+
26. Select the bot channel registration
44+
27. Go to Channels
45+
28. Select the "Teams" icon under "Add a featured channel
46+
29. Click Save
47+
48+
- Updating Sample Project Settings
49+
1. Open the project
50+
2. Open config.py
51+
3. Enter the app id under the ```MicrosoftAppId``` and the password under the ```MicrosoftAppPassword```
52+
4. Save the close the file
53+
5. Under the teams_app_manifest folder open the manifest.json file
54+
6. Update the ```botId``` with the Microsoft App ID from before
55+
7. Update the ```id``` with the Microsoft App ID from before
56+
8. Save the close the file
57+
58+
- Uploading the bot to Teams
59+
1. In file explorer navigate to the TeamsAppManifest folder in the project
60+
2. Select the 3 files and zip them
61+
3. Open Teams
62+
4. Click on "Apps"
63+
5. Select "Upload a custom app" on the left at the bottom
64+
6. Select the zip
65+
7. Select for you
66+
8. (optionally) click install if prompted
67+
9. Click open
68+
69+
## To try this sample
70+
71+
- Clone the repository
72+
73+
```bash
74+
git clone https://github.com/Microsoft/botbuilder-python.git
75+
```
76+
77+
- In a terminal, navigate to `samples/python/scenarios/file-upload`
78+
79+
- From a terminal
80+
81+
```bash
82+
pip install -r requirements.txt
83+
python app.py
84+
```
85+
86+
- Interacting with the bot
87+
1. Send a message to your bot in Teams
88+
2. Confirm you are getting a 200 back in Ngrok
89+
3. Click Accept on the card that is shown
90+
4. Confirm you see a 2nd 200 in Ngrok
91+
5. In Teams go to Files -> OneDrive -> Applications
92+
93+
## Testing the bot using Bot Framework Emulator
94+
95+
[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.
96+
97+
- Install the Bot Framework Emulator version 4.3.0 or greater from [here](https://github.com/Microsoft/BotFramework-Emulator/releases)
98+
99+
### Connect to the bot using Bot Framework Emulator
100+
101+
- Launch Bot Framework Emulator
102+
- File -> Open Bot
103+
- Enter a Bot URL of `http://localhost:3978/api/messages`
104+
105+
## Deploy the bot to Azure
106+
107+
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.
108+
109+
## Further reading
110+
111+
- [Bot Framework Documentation](https://docs.botframework.com)
112+
- [Bot Basics](https://docs.microsoft.com/azure/bot-service/bot-builder-basics?view=azure-bot-service-4.0)
113+
- [Activity processing](https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-concept-activity-processing?view=azure-bot-service-4.0)
114+
- [Azure Bot Service Introduction](https://docs.microsoft.com/azure/bot-service/bot-service-overview-introduction?view=azure-bot-service-4.0)
115+
- [Azure Bot Service Documentation](https://docs.microsoft.com/azure/bot-service/?view=azure-bot-service-4.0)
116+
- [Azure CLI](https://docs.microsoft.com/cli/azure/?view=azure-cli-latest)
117+
- [Azure Portal](https://portal.azure.com)
118+
- [Language Understanding using LUIS](https://docs.microsoft.com/en-us/azure/cognitive-services/luis/)
119+
- [Channels and Bot Connector Service](https://docs.microsoft.com/en-us/azure/bot-service/bot-concepts?view=azure-bot-service-4.0)

scenarios/file-upload/app.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
4+
import asyncio
5+
import sys
6+
import traceback
7+
from datetime import datetime
8+
9+
from flask import Flask, request, Response
10+
from botbuilder.core import (
11+
BotFrameworkAdapterSettings,
12+
TurnContext,
13+
BotFrameworkAdapter,
14+
)
15+
from botbuilder.schema import Activity, ActivityTypes
16+
17+
from bots import TeamsFileBot
18+
19+
# Create the loop and Flask app
20+
LOOP = asyncio.get_event_loop()
21+
APP = Flask(__name__, instance_relative_config=True)
22+
APP.config.from_object("config.DefaultConfig")
23+
24+
# Create adapter.
25+
# See https://aka.ms/about-bot-adapter to learn more about how bots work.
26+
SETTINGS = BotFrameworkAdapterSettings(APP.config["APP_ID"], APP.config["APP_PASSWORD"])
27+
ADAPTER = BotFrameworkAdapter(SETTINGS)
28+
29+
30+
# Catch-all for errors.
31+
async def on_error(context: TurnContext, error: Exception):
32+
# This check writes out errors to console log .vs. app insights.
33+
# NOTE: In production environment, you should consider logging this to Azure
34+
# application insights.
35+
print(f"\n [on_turn_error] unhandled error: {error}", file=sys.stderr)
36+
print(traceback.format_exc())
37+
38+
# Send a message to the user
39+
await context.send_activity("The bot encountered an error or bug.")
40+
await context.send_activity(
41+
"To continue to run this bot, please fix the bot source code."
42+
)
43+
# Send a trace activity if we're talking to the Bot Framework Emulator
44+
if context.activity.channel_id == "emulator":
45+
# Create a trace activity that contains the error object
46+
trace_activity = Activity(
47+
label="TurnError",
48+
name="on_turn_error Trace",
49+
timestamp=datetime.utcnow(),
50+
type=ActivityTypes.trace,
51+
value=f"{error}",
52+
value_type="https://www.botframework.com/schemas/error",
53+
)
54+
# Send a trace activity, which will be displayed in Bot Framework Emulator
55+
await context.send_activity(trace_activity)
56+
57+
58+
ADAPTER.on_turn_error = on_error
59+
60+
# Create the Bot
61+
BOT = TeamsFileBot()
62+
63+
# Listen for incoming requests on /api/messages.s
64+
@APP.route("/api/messages", methods=["POST"])
65+
def messages():
66+
# Main bot message handler.
67+
if "application/json" in request.headers["Content-Type"]:
68+
body = request.json
69+
else:
70+
return Response(status=415)
71+
72+
activity = Activity().deserialize(body)
73+
auth_header = (
74+
request.headers["Authorization"] if "Authorization" in request.headers else ""
75+
)
76+
77+
try:
78+
task = LOOP.create_task(
79+
ADAPTER.process_activity(activity, auth_header, BOT.on_turn)
80+
)
81+
LOOP.run_until_complete(task)
82+
return Response(status=201)
83+
except Exception as exception:
84+
raise exception
85+
86+
87+
if __name__ == "__main__":
88+
try:
89+
APP.run(debug=False, port=APP.config["PORT"]) # nosec debug
90+
except Exception as exception:
91+
raise exception
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
4+
from .teams_file_bot import TeamsFileBot
5+
6+
__all__ = ["TeamsFileBot"]

0 commit comments

Comments
 (0)
0