ADAPTER = BotFrameworkAdapter(SETTINGS) # 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]: { error }', file=sys.stderr) # Send a message to the user await context.send_activity('Oops. Something went wrong!') # Clear out state await conversation_state.delete(context) ADAPTER.on_turn_error = on_error # Create MemoryStorage, UserState and ConversationState memory = MemoryStorage() user_state = UserState(memory) conversation_state = ConversationState(memory) bot = StateManagementBot(conversation_state, user_state) async def messages(req: web.Request) -> web.Response: body = await req.json() activity = Activity().deserialize(body) auth_header = req.headers[ 'Authorization'] if 'Authorization' in req.headers else ''
# 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 tbs = taiwan_bot_sheet.TaiwanBotSheet() memory = MemoryStorage() conversation_state = ConversationState(memory) bot = FAQBot(tbs, conversation_state) app = FastAPI() @app.get("/healthcheck") def healthcheck(): # for https://cron-job.org/ to keep heroku alive return {"message": "I'm alive and well! Thank you!"} @app.get("/sheet")
"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 = MethodType(on_error, ADAPTER) # Create the main dialog BOT = MyBot() # Listen for incoming requests on /api/messages. @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)
from bots.courses_bot.course_recognizer import CourseRecognizer from bots.courses_bot.data_models.course import Course from bots.courses_bot.dialogs.student_profile_dialog import StudentProfileDialog from bots.echo_bot.bot import EchoBot from config import DefaultConfig CONFIG = DefaultConfig() MEMORY = MemoryStorage() USER_STATE = UserState(MEMORY) CONVERSATION_STATE = ConversationState(MEMORY) SETTINGS = BotFrameworkAdapterSettings(CONFIG.APP_ID, CONFIG.APP_PASSWORD) ADAPTER = BotFrameworkAdapter(SETTINGS) ADAPTER.on_turn_error = Bot.handle_bot_errors # Create Echo Bot ECHO_BOT = EchoBot(ADAPTER) # Load course info COURSE = Course.load_courses(CONFIG) # LUIS Recognizer LUIS_RECOGNIZER = CourseRecognizer(CONFIG) # Create Courses Bot COURSES_BOT = CoursesBot( ADAPTER, USER_STATE, CONVERSATION_STATE, StudentProfileDialog(USER_STATE, COURSE, LUIS_RECOGNIZER), COURSE)