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. LOGGER.error( msg=f"An unhandled error has occurred: '{error.__class__.__name__}: {str(error)}'" ) # Send a message to the user await context.send_activity(messages.SOMETHING_WENT_WRONG) # 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)
async def _create_delivery(self, step_context): recipient: ChannelAccount = step_context.context.activity.recipient delivery: Delivery = step_context.values[Keys.DELIVERY_DIALOG_STATE.value] data = await self.storage.read([recipient.id]) # get or initialize this member's state member_state = data.get(recipient.id, {}) if not member_state: member_state = { recipient.id: {} } delivery_list: DeliveryList = member_state.get(Keys.DELIVERY_LIST_STATE.value) if delivery_list: delivery_list.deliveries.append(delivery) delivery_list.turn_number = delivery_list.turn_number + 1 else: delivery_list = DeliveryList() delivery_list.deliveries.append(delivery) delivery_list.turn_number = 1 member_state[recipient.id][Keys.DELIVERY_LIST_STATE.value] = delivery_list try: await self.storage.write(member_state) LOGGER.debug(msg=f"Delivery persisted.") except Exception as e: LOGGER.error(msg=f"An error='{e}' has occurred while trying to schedule a delivery") await step_context.context.send_activity(messages.SOMETHING_WENT_WRONG)
async def messages(req: Request) -> Response: # Main bot message handler. if JSON_CONTENT_TYPE in req.headers[CONTENT_TYPE_HEADER]: body = await req.json() else: return Response(status=415) activity = Activity().deserialize(body) auth_header = req.headers[ AUTHORIZATION_HEADER] if AUTHORIZATION_HEADER in req.headers else "" try: await ERROR_ADAPTER.process_activity(activity, auth_header, BOT.on_turn) return Response(status=201) except Exception as exception: LOGGER.error(msg=f"An unexpected exception={exception} has occurred") raise exception
async def execute_luis_query( luis_recognizer: Recognizer, turn_context: TurnContext) -> (Intent, object): """ Returns an object with pre-formatted LUIS results for the bot's dialogs to consume. """ result = None intent = None try: LOGGER.debug(msg="Executing LUIS query") recognizer_result = await luis_recognizer.recognize(turn_context) intent = get_intent(recognizer_result=recognizer_result) LOGGER.debug(msg="LUIS query execution succeeded") except Exception as exception: LOGGER.error( msg=f"Executing LUIS query failed with an error={exception}") return intent, result
def __init__( self, conversation_state: ConversationState, dialog: Dialog, user_state: UserState, ): if conversation_state is None: error = "Missing parameter. conversation_state is required" LOGGER.error(msg=error) raise Exception(f"[DeliveryBot]: {error}") if user_state is None: error = "Missing parameter. user_state is required" LOGGER.error(msg=error) raise Exception(f"[DeliveryBot]: {error}") if dialog is None: error = "Missing parameter. dialog is required" LOGGER.error(msg=error) raise Exception(f"[DeliveryBot]: {error}") self.conversation_state = conversation_state self.dialog = dialog self.user_state = user_state self.user_state_accessor = self.user_state.create_property( DELIVERIES_HISTORY)
return Response(status=415) activity = Activity().deserialize(body) auth_header = req.headers[ AUTHORIZATION_HEADER] if AUTHORIZATION_HEADER in req.headers else "" try: await ERROR_ADAPTER.process_activity(activity, auth_header, BOT.on_turn) return Response(status=201) except Exception as exception: LOGGER.error(msg=f"An unexpected exception={exception} has occurred") raise exception APP = web.Application() APP.router.add_post("/api/messages", messages) if __name__ == "__main__": try: LOGGER.info( msg= f"Starting application at host='{CONFIG.HOST}' on port={CONFIG.PORT}" ) web.run_app(APP, host=CONFIG.HOST, port=CONFIG.PORT) except Exception as error: LOGGER.error( msg=f"Application initialization failed with an error={error}") raise error