diff --git a/generators/python/app/templates/core/{{cookiecutter.bot_name}}/README.md b/generators/python/app/templates/core/{{cookiecutter.bot_name}}/README.md index 35a5eb2f1d..4581ecc5bd 100644 --- a/generators/python/app/templates/core/{{cookiecutter.bot_name}}/README.md +++ b/generators/python/app/templates/core/{{cookiecutter.bot_name}}/README.md @@ -30,7 +30,6 @@ If you wish to create a LUIS application via the CLI, these steps can be found i - Run `pip install -r requirements.txt` to install all dependencies - Update LuisAppId, LuisAPIKey and LuisAPIHostName in `config.py` with the information retrieved from the [LUIS portal](https://www.luis.ai) - Run `python app.py` -- Alternatively to the last command, you can set the file in an environment variable with `set FLASK_APP=app.py` in windows (`export FLASK_APP=app.py` in mac/linux) and then run `flask run --host=127.0.0.1 --port=3978` ## Testing the bot using Bot Framework Emulator diff --git a/generators/python/app/templates/core/{{cookiecutter.bot_name}}/adapter_with_error_handler.py b/generators/python/app/templates/core/{{cookiecutter.bot_name}}/adapter_with_error_handler.py new file mode 100644 index 0000000000..4e24fcb796 --- /dev/null +++ b/generators/python/app/templates/core/{{cookiecutter.bot_name}}/adapter_with_error_handler.py @@ -0,0 +1,56 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. +import sys +import traceback +from datetime import datetime + +from botbuilder.core import ( + BotFrameworkAdapter, + BotFrameworkAdapterSettings, + ConversationState, + TurnContext, +) +from botbuilder.schema import ActivityTypes, Activity + + +class AdapterWithErrorHandler(BotFrameworkAdapter): + def __init__( + self, + settings: BotFrameworkAdapterSettings, + conversation_state: ConversationState, + ): + super().__init__(settings) + self._conversation_state = conversation_state + + # Catch-all for errors. + async def on_error(context: TurnContext, error: Exception): + # This check writes out errors to console log + # 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) + + # Clear out state + nonlocal self + await self._conversation_state.delete(context) + + self.on_turn_error = on_error diff --git a/generators/python/app/templates/core/{{cookiecutter.bot_name}}/flight_booking_recognizer.py b/generators/python/app/templates/core/{{cookiecutter.bot_name}}/flight_booking_recognizer.py index 7476103c70..f1a2290b40 100644 --- a/generators/python/app/templates/core/{{cookiecutter.bot_name}}/flight_booking_recognizer.py +++ b/generators/python/app/templates/core/{{cookiecutter.bot_name}}/flight_booking_recognizer.py @@ -4,21 +4,23 @@ from botbuilder.ai.luis import LuisApplication, LuisRecognizer from botbuilder.core import Recognizer, RecognizerResult, TurnContext +from config import DefaultConfig + class FlightBookingRecognizer(Recognizer): - def __init__(self, configuration: dict): + def __init__(self, configuration: DefaultConfig): self._recognizer = None luis_is_configured = ( - configuration["LUIS_APP_ID"] - and configuration["LUIS_API_KEY"] - and configuration["LUIS_API_HOST_NAME"] + configuration.LUIS_APP_ID + and configuration.LUIS_API_KEY + and configuration.LUIS_API_HOST_NAME ) if luis_is_configured: luis_application = LuisApplication( - configuration["LUIS_APP_ID"], - configuration["LUIS_API_KEY"], - "https://" + configuration["LUIS_API_HOST_NAME"], + configuration.LUIS_APP_ID, + configuration.LUIS_API_KEY, + "https://" + configuration.LUIS_API_HOST_NAME, ) self._recognizer = LuisRecognizer(luis_application) diff --git a/generators/python/app/templates/echo/{{cookiecutter.bot_name}}/README.md b/generators/python/app/templates/echo/{{cookiecutter.bot_name}}/README.md index 5eeee191fe..cc26d0f4db 100644 --- a/generators/python/app/templates/echo/{{cookiecutter.bot_name}}/README.md +++ b/generators/python/app/templates/echo/{{cookiecutter.bot_name}}/README.md @@ -13,7 +13,6 @@ This sample **requires** prerequisites in order to run. ## Running the sample - Run `pip install -r requirements.txt` to install all dependencies - Run `python app.py` -- Alternatively to the last command, you can set the file in an environment variable with `set FLASK_APP=app.py` in windows (`export FLASK_APP=app.py` in mac/linux) and then run `flask run --host=127.0.0.1 --port=3978` ## Testing the bot using Bot Framework Emulator diff --git a/generators/python/app/templates/echo/{{cookiecutter.bot_name}}/app.py b/generators/python/app/templates/echo/{{cookiecutter.bot_name}}/app.py index f197a12002..9d02057a04 100644 --- a/generators/python/app/templates/echo/{{cookiecutter.bot_name}}/app.py +++ b/generators/python/app/templates/echo/{{cookiecutter.bot_name}}/app.py @@ -15,7 +15,7 @@ from botbuilder.core.integration import aiohttp_error_middleware from botbuilder.schema import Activity, ActivityTypes -from bots import EchoBot +from bot import MyBot from config import DefaultConfig CONFIG = DefaultConfig() @@ -57,7 +57,7 @@ async def on_error(context: TurnContext, error: Exception): ADAPTER.on_turn_error = on_error # Create the Bot -BOT = EchoBot() +BOT = MyBot() # Listen for incoming requests on /api/messages diff --git a/generators/python/app/templates/empty/{{cookiecutter.bot_name}}/README.md b/generators/python/app/templates/empty/{{cookiecutter.bot_name}}/README.md index 5eeee191fe..cc26d0f4db 100644 --- a/generators/python/app/templates/empty/{{cookiecutter.bot_name}}/README.md +++ b/generators/python/app/templates/empty/{{cookiecutter.bot_name}}/README.md @@ -13,7 +13,6 @@ This sample **requires** prerequisites in order to run. ## Running the sample - Run `pip install -r requirements.txt` to install all dependencies - Run `python app.py` -- Alternatively to the last command, you can set the file in an environment variable with `set FLASK_APP=app.py` in windows (`export FLASK_APP=app.py` in mac/linux) and then run `flask run --host=127.0.0.1 --port=3978` ## Testing the bot using Bot Framework Emulator