forked from OfficeDev/Microsoft-Teams-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move samples from bot builder to Teams Samples Repository: Python (Of…
…ficeDev#481) * Raised PR For Bot-Conversation Python * Raised PR For Bot File Upload * Task Module and Bot Initiate Thread * Added Five more samples * Botbuilder Samples Updated * Message Extension Code Pushed * Updated Read and Sample.json Files * Team Authentication * Main Readme One Sample update Co-authored-by: ChetanSharma-MSFT <[email protected]>
- Loading branch information
1 parent
3053974
commit ed66f90
Showing
289 changed files
with
16,232 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
--- | ||
page_type: sample | ||
description: This sample app demonstrate is how to incorporate basic conversational flow into a Teams application. | ||
products: | ||
- office-teams | ||
- office | ||
- office-365 | ||
languages: | ||
- python | ||
extensions: | ||
contentType: samples | ||
createdDate: "12-12-2019 13:38:25" | ||
urlFragment: officedev-microsoft-teams-samples-bot-conversation-python | ||
|
||
--- | ||
|
||
# Teams Conversation Bot | ||
|
||
Bot Framework v4 Conversation Bot sample for Teams. | ||
|
||
This bot has been created using [Bot Framework](https://dev.botframework.com). This sample shows | ||
how to incorporate basic conversational flow into a Teams application. It also illustrates a few of the Teams specific calls you can make from your bot. | ||
|
||
## Prerequisites | ||
|
||
- Microsoft Teams is installed and you have an account | ||
- [Python SDK](https://www.python.org/downloads/) min version 3.6 | ||
- [ngrok](https://ngrok.com/) or equivalent tunnelling solution | ||
|
||
## Concepts introduced in this sample | ||
![BotConversation](Images/BotConversation.gif) | ||
|
||
The bot initialization message | ||
![Message](Images/1.BotInstallationMessage.PNG) | ||
|
||
The bot will send the welcome card for you to interact with | ||
![WelcomeCard](Images/2.WelcomeCard.PNG) | ||
|
||
The bot will respond to the message and mention the user | ||
![MentionMe](Images/3.MentionMe.PNG) | ||
|
||
The bot initialization message | ||
![MessageAllMembers](Images/4.MessageAllMembers.PNG) | ||
|
||
## To try this sample | ||
|
||
> Note these instructions are for running the sample on your local machine, the tunnelling solution is required because | ||
the Teams service needs to call into the bot. | ||
|
||
1) Clone the repository | ||
|
||
```bash | ||
git clone https://github.com/OfficeDev/Microsoft-Teams-Samples.git | ||
``` | ||
|
||
2) Run ngrok - point to port 3978 | ||
|
||
```bash | ||
ngrok http --host-header=rewrite 3978 | ||
``` | ||
|
||
3) Create [Bot Framework registration resource](https://docs.microsoft.com/en-us/azure/bot-service/bot-service-quickstart-registration) in Azure | ||
- Use the current `https` URL you were given by running ngrok. Append with the path `/api/messages` used by this sample | ||
- 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) | ||
- __*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) | ||
|
||
4) In a terminal, go to `samples\bot-conversation` | ||
|
||
5) Activate your desired virtual environment | ||
|
||
6) Install dependencies by running ```pip install -r requirements.txt``` in the project folder. | ||
|
||
7) Update the `config.py` 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.) | ||
|
||
8) __*This step is specific to Teams.*__ | ||
- **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`) | ||
- **Zip** up the contents of the `teamsAppManifest` folder to create a `manifest.zip` | ||
- **Upload** the `manifest.zip` to Teams (in the Apps view click "Upload a custom app") | ||
9) Run your bot with `python app.py` | ||
## Interacting with the bot | ||
You can interact with this bot by sending it a message, or selecting a command from the command list. The bot will respond to the following strings. | ||
1. **Show Welcome** | ||
- **Result:** The bot will send the welcome card for you to interact with | ||
- **Valid Scopes:** personal, group chat, team chat | ||
2. **MentionMe** | ||
- **Result:** The bot will respond to the message and mention the user | ||
- **Valid Scopes:** personal, group chat, team chat | ||
3. **MessageAllMembers** | ||
- **Result:** The bot will send a 1-on-1 message to each member in the current conversation (aka on the conversation's roster). | ||
- **Valid Scopes:** personal, group chat, team chat | ||
You can select an option from the command list by typing ```@TeamsConversationBot``` into the compose message area and ```What can I do?``` text above the compose area. | ||
## 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 | ||
- [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) | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. | ||
|
||
import sys | ||
import traceback | ||
import uuid | ||
from datetime import datetime | ||
from http import HTTPStatus | ||
|
||
from aiohttp import web | ||
from aiohttp.web import Request, Response, json_response | ||
from botbuilder.core import ( | ||
BotFrameworkAdapterSettings, | ||
TurnContext, | ||
BotFrameworkAdapter, | ||
) | ||
from botbuilder.core.integration import aiohttp_error_middleware | ||
from botbuilder.schema import Activity, ActivityTypes | ||
|
||
from bots import TeamsConversationBot | ||
from config import DefaultConfig | ||
|
||
CONFIG = DefaultConfig() | ||
|
||
# Create adapter. | ||
# See https://aka.ms/about-bot-adapter to learn more about how bots work. | ||
SETTINGS = BotFrameworkAdapterSettings(CONFIG.APP_ID, 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) | ||
traceback.print_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 | ||
|
||
# If the channel is the Emulator, and authentication is not in use, the AppId will be null. | ||
# We generate a random AppId for this case only. This is not required for production, since | ||
# the AppId will have a value. | ||
APP_ID = SETTINGS.app_id if SETTINGS.app_id else uuid.uuid4() | ||
|
||
# Create the Bot | ||
BOT = TeamsConversationBot(CONFIG.APP_ID, CONFIG.APP_PASSWORD) | ||
|
||
|
||
# Listen for incoming requests on /api/messages. | ||
async def messages(req: Request) -> Response: | ||
# Main bot message handler. | ||
if "application/json" in req.headers["Content-Type"]: | ||
body = await req.json() | ||
else: | ||
return Response(status=HTTPStatus.UNSUPPORTED_MEDIA_TYPE) | ||
|
||
activity = Activity().deserialize(body) | ||
auth_header = req.headers["Authorization"] if "Authorization" in req.headers else "" | ||
|
||
response = await ADAPTER.process_activity(activity, auth_header, BOT.on_turn) | ||
if response: | ||
return json_response(data=response.body, status=response.status) | ||
return Response(status=HTTPStatus.OK) | ||
|
||
|
||
APP = web.Application(middlewares=[aiohttp_error_middleware]) | ||
APP.router.add_post("/api/messages", messages) | ||
|
||
if __name__ == "__main__": | ||
try: | ||
web.run_app(APP, host="localhost", port=CONFIG.PORT) | ||
except Exception as error: | ||
raise error |
Oops, something went wrong.