-
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.
- Loading branch information
1 parent
744ad1d
commit cec7838
Showing
17 changed files
with
973 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# basic_bot | ||
|
||
echo_template | ||
|
||
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 | ||
|
||
This sample **requires** prerequisites in order to run. | ||
|
||
### Install Python 3.6 | ||
|
||
## Running the sample | ||
- Run `pip install -r requirements.txt` to install all dependencies | ||
- Run `python app.py` | ||
|
||
|
||
## 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 | ||
- Enter a Bot URL of `http://localhost:3978/api/messages` | ||
|
||
|
||
## 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) | ||
- [Dialogs](https://docs.microsoft.com/azure/bot-service/bot-builder-concept-dialog?view=azure-bot-service-4.0) | ||
- [Gathering Input Using Prompts](https://docs.microsoft.com/azure/bot-service/bot-builder-prompts?view=azure-bot-service-4.0&tabs=csharp) | ||
- [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/azure/cognitive-services/luis/) | ||
- [Channels and Bot Connector Service](https://docs.microsoft.com/azure/bot-service/bot-concepts?view=azure-bot-service-4.0) |
Empty file.
Binary file not shown.
Binary file not shown.
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 | ||
from datetime import datetime | ||
|
||
from aiohttp import web | ||
from aiohttp.web import Request, Response, json_response | ||
from botbuilder.core import ( | ||
BotFrameworkAdapter, | ||
BotFrameworkAdapterSettings, | ||
ConversationState, | ||
MemoryStorage, | ||
TurnContext, | ||
UserState, | ||
) | ||
from botbuilder.core.integration import aiohttp_error_middleware | ||
from botbuilder.schema import Activity, ActivityTypes | ||
|
||
from bot import MyBot | ||
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 | ||
|
||
# where the memory goes | ||
memory = MemoryStorage() | ||
user_state = UserState(memory) | ||
conversation_state = ConversationState(memory) | ||
|
||
# Create the Bot | ||
BOT = MyBot(user_state, conversation_state) | ||
|
||
|
||
# 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=415) | ||
|
||
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=201) | ||
|
||
|
||
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.