Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ async def send_activities(
)

if not response:
response = ResourceResponse(activity.id or "")
response = ResourceResponse(id=activity.id or "")

responses.append(response)
return responses
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.


class ContentType:
O365_CONNECTOR_CARD = "application/vnd.microsoft.teams.card.o365connector"
FILE_CONSENT_CARD = "application/vnd.microsoft.teams.card.file.consent"
FILE_DOWNLOAD_INFO = "application/vnd.microsoft.teams.file.download.info"
FILE_INFO_CARD = "application/vnd.microsoft.teams.card.file.info"


class Type:
O365_CONNECTOR_CARD_VIEWACTION = "ViewAction"
O365_CONNECTOR_CARD_OPEN_URI = "OpenUri"
O365_CONNECTOR_CARD_HTTP_POST = "HttpPOST"
O365_CONNECTOR_CARD_ACTION_CARD = "ActionCard"
O365_CONNECTOR_CARD_TEXT_INPUT = "TextInput"
O365_CONNECTOR_CARD_DATE_INPUT = "DateInput"
O365_CONNECTOR_CARD_MULTICHOICE_INPUT = "MultichoiceInput"
119 changes: 119 additions & 0 deletions scenarios/file-upload/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
# FileUpload

Bot Framework v4 echo bot sample.

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.

## Prerequisites
- Open Notepad (or another text editor) to save some values as you complete the setup.

- Ngrok setup
1. Download and install [Ngrok](https://ngrok.com/download)
2. In terminal navigate to the directory where Ngrok is installed
3. Run this command: ```ngrok http -host-header=rewrite 3978 ```
4. Copy the https://xxxxxxxx.ngrok.io address and put it into notepad. **NOTE** You want the https address.

- Azure setup
1. Login to the [Azure Portal]((https://portal.azure.com)
2. (optional) create a new resource group if you don't currently have one
3. Go to your resource group
4. Click "Create a new resource"
5. Search for "Bot Channel Registration"
6. Click Create
7. Enter bot name, subscription
8. In the "Messaging endpoint url" enter the ngrok address from earlier.
8a. Finish the url with "/api/messages. It should look like ```https://xxxxxxxxx.ngrok.io/api/messages```
9. Click the "Microsoft App Id and password" box
10. Click on "Create New"
11. Click on "Create App ID in the App Registration Portal"
12. Click "New registration"
13. Enter a name
14. Under "Supported account types" select "Accounts in any organizational directory and personal Microsoft accounts"
15. Click register
16. Copy the application (client) ID and put it in Notepad. Label it "Microsoft App ID"
17. Go to "Certificates & Secrets"
18. Click "+ New client secret"
19. Enter a description
20. Click "Add"
21. Copy the value and put it into Notepad. Label it "Password"
22. (back in the channel registration view) Copy/Paste the Microsoft App ID and Password into their respective fields
23. Click Create
24. Go to "Resource groups" on the left
25. Select the resource group that the bot channel reg was created in
26. Select the bot channel registration
27. Go to Channels
28. Select the "Teams" icon under "Add a featured channel
29. Click Save

- Updating Sample Project Settings
1. Open the project
2. Open config.py
3. Enter the app id under the ```MicrosoftAppId``` and the password under the ```MicrosoftAppPassword```
4. Save the close the file
5. Under the teams_app_manifest folder open the manifest.json file
6. Update the ```botId``` with the Microsoft App ID from before
7. Update the ```id``` with the Microsoft App ID from before
8. Save the close the file

- Uploading the bot to Teams
1. In file explorer navigate to the TeamsAppManifest folder in the project
2. Select the 3 files and zip them
3. Open Teams
4. Click on "Apps"
5. Select "Upload a custom app" on the left at the bottom
6. Select the zip
7. Select for you
8. (optionally) click install if prompted
9. Click open

## To try this sample

- Clone the repository

```bash
git clone https://github.com/Microsoft/botbuilder-python.git
```

- In a terminal, navigate to `samples/python/scenarios/file-upload`

- From a terminal

```bash
pip install -r requirements.txt
python app.py
```

- Interacting with the bot
1. Send a message to your bot in Teams
2. Confirm you are getting a 200 back in Ngrok
3. Click Accept on the card that is shown
4. Confirm you see a 2nd 200 in Ngrok
5. In Teams go to Files -> OneDrive -> Applications

## Testing the bot using Bot Framework Emulator

[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.

- Install the Bot Framework Emulator version 4.3.0 or greater from [here](https://github.com/Microsoft/BotFramework-Emulator/releases)

### Connect to the bot using Bot Framework Emulator

- Launch Bot Framework Emulator
- File -> Open Bot
- Enter a Bot URL of `http://localhost:3978/api/messages`

## 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.

## Further reading

- [Bot Framework Documentation](https://docs.botframework.com)
- [Bot Basics](https://docs.microsoft.com/azure/bot-service/bot-builder-basics?view=azure-bot-service-4.0)
- [Activity processing](https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-concept-activity-processing?view=azure-bot-service-4.0)
- [Azure Bot Service Introduction](https://docs.microsoft.com/azure/bot-service/bot-service-overview-introduction?view=azure-bot-service-4.0)
- [Azure Bot Service Documentation](https://docs.microsoft.com/azure/bot-service/?view=azure-bot-service-4.0)
- [Azure CLI](https://docs.microsoft.com/cli/azure/?view=azure-cli-latest)
- [Azure Portal](https://portal.azure.com)
- [Language Understanding using LUIS](https://docs.microsoft.com/en-us/azure/cognitive-services/luis/)
- [Channels and Bot Connector Service](https://docs.microsoft.com/en-us/azure/bot-service/bot-concepts?view=azure-bot-service-4.0)
91 changes: 91 additions & 0 deletions scenarios/file-upload/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

import asyncio
import sys
import traceback
from datetime import datetime

from flask import Flask, request, Response
from botbuilder.core import (
BotFrameworkAdapterSettings,
TurnContext,
BotFrameworkAdapter,
)
from botbuilder.schema import Activity, ActivityTypes

from bots import TeamsFileBot

# Create the loop and Flask app
LOOP = asyncio.get_event_loop()
APP = Flask(__name__, instance_relative_config=True)
APP.config.from_object("config.DefaultConfig")

# Create adapter.
# See https://aka.ms/about-bot-adapter to learn more about how bots work.
SETTINGS = BotFrameworkAdapterSettings(APP.config["APP_ID"], APP.config["APP_PASSWORD"])
ADAPTER = BotFrameworkAdapter(SETTINGS)


# Catch-all for errors.
async def on_error(context: TurnContext, error: Exception):
# This check writes out errors to console log .vs. app insights.
# NOTE: In production environment, you should consider logging this to Azure
# application insights.
print(f"\n [on_turn_error] unhandled error: {error}", file=sys.stderr)
print(traceback.format_exc())

# Send a message to the user
await context.send_activity("The bot encountered an error or bug.")
await context.send_activity(
"To continue to run this bot, please fix the bot source code."
)
# Send a trace activity if we're talking to the Bot Framework Emulator
if context.activity.channel_id == "emulator":
# Create a trace activity that contains the error object
trace_activity = Activity(
label="TurnError",
name="on_turn_error Trace",
timestamp=datetime.utcnow(),
type=ActivityTypes.trace,
value=f"{error}",
value_type="https://www.botframework.com/schemas/error",
)
# Send a trace activity, which will be displayed in Bot Framework Emulator
await context.send_activity(trace_activity)


ADAPTER.on_turn_error = on_error

# Create the Bot
BOT = TeamsFileBot()

# Listen for incoming requests on /api/messages.s
@APP.route("/api/messages", methods=["POST"])
def messages():
# Main bot message handler.
if "application/json" in request.headers["Content-Type"]:
body = request.json
else:
return Response(status=415)

activity = Activity().deserialize(body)
auth_header = (
request.headers["Authorization"] if "Authorization" in request.headers else ""
)

try:
task = LOOP.create_task(
ADAPTER.process_activity(activity, auth_header, BOT.on_turn)
)
LOOP.run_until_complete(task)
return Response(status=201)
except Exception as exception:
raise exception


if __name__ == "__main__":
try:
APP.run(debug=False, port=APP.config["PORT"]) # nosec debug
except Exception as exception:
raise exception
6 changes: 6 additions & 0 deletions scenarios/file-upload/bots/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

from .teams_file_bot import TeamsFileBot

__all__ = ["TeamsFileBot"]
Loading