Skip to content

Commit

Permalink
Merge pull request #2085 from microsoft/trboehre-python-generator-fixes
Browse files Browse the repository at this point in the history
Python generator fixes
  • Loading branch information
tracyboehrer authored Dec 20, 2019
2 parents f7f3f27 + db3a172 commit 8c1e8ed
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8c1e8ed

Please sign in to comment.