|
| 1 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +# Licensed under the MIT License. |
| 3 | + |
| 4 | +import asyncio |
| 5 | +import sys |
| 6 | +from datetime import datetime |
| 7 | + |
| 8 | +from flask import Flask, request, Response |
| 9 | +from botbuilder.core import BotFrameworkAdapterSettings, TurnContext, BotFrameworkAdapter |
| 10 | +from botbuilder.schema import Activity, ActivityTypes |
| 11 | + |
| 12 | +from bots import QnABot |
| 13 | + |
| 14 | +# Create the loop and Flask app |
| 15 | +LOOP = asyncio.get_event_loop() |
| 16 | +app = Flask(__name__, instance_relative_config=True) |
| 17 | +app.config.from_object("config.DefaultConfig") |
| 18 | + |
| 19 | +# Create adapter. |
| 20 | +# See https://aka.ms/about-bot-adapter to learn more about how bots work. |
| 21 | +SETTINGS = BotFrameworkAdapterSettings(app.config["APP_ID"], app.config["APP_PASSWORD"]) |
| 22 | +ADAPTER = BotFrameworkAdapter(SETTINGS) |
| 23 | + |
| 24 | + |
| 25 | +# Catch-all for errors. |
| 26 | +async def on_error(context: TurnContext, error: Exception): |
| 27 | + # This check writes out errors to console log .vs. app insights. |
| 28 | + # NOTE: In production environment, you should consider logging this to Azure |
| 29 | + # application insights. |
| 30 | + print(f"\n [on_turn_error] unhandled error: {error}", file=sys.stderr) |
| 31 | + |
| 32 | + # Send a message to the user |
| 33 | + await context.send_activity("The bot encountered an error or bug.") |
| 34 | + await context.send_activity("To continue to run this bot, please fix the bot source code.") |
| 35 | + # Send a trace activity if we're talking to the Bot Framework Emulator |
| 36 | + if context.activity.channel_id == 'emulator': |
| 37 | + # Create a trace activity that contains the error object |
| 38 | + trace_activity = Activity( |
| 39 | + label="TurnError", |
| 40 | + name="on_turn_error Trace", |
| 41 | + timestamp=datetime.utcnow(), |
| 42 | + type=ActivityTypes.trace, |
| 43 | + value=f"{error}", |
| 44 | + value_type="https://www.botframework.com/schemas/error" |
| 45 | + ) |
| 46 | + # Send a trace activity, which will be displayed in Bot Framework Emulator |
| 47 | + await context.send_activity(trace_activity) |
| 48 | + |
| 49 | +ADAPTER.on_turn_error = on_error |
| 50 | + |
| 51 | +# Create the Bot |
| 52 | +BOT = QnABot(app.config) |
| 53 | + |
| 54 | +# Listen for incoming requests on /api/messages |
| 55 | +@app.route("/api/messages", methods=["POST"]) |
| 56 | +def messages(): |
| 57 | + # Main bot message handler. |
| 58 | + if "application/json" in request.headers["Content-Type"]: |
| 59 | + body = request.json |
| 60 | + else: |
| 61 | + return Response(status=415) |
| 62 | + |
| 63 | + activity = Activity().deserialize(body) |
| 64 | + auth_header = ( |
| 65 | + request.headers["Authorization"] if "Authorization" in request.headers else "" |
| 66 | + ) |
| 67 | + |
| 68 | + try: |
| 69 | + task = LOOP.create_task( |
| 70 | + ADAPTER.process_activity(activity, auth_header, BOT.on_turn) |
| 71 | + ) |
| 72 | + LOOP.run_until_complete(task) |
| 73 | + return Response(status=201) |
| 74 | + except Exception as exception: |
| 75 | + raise exception |
| 76 | + |
| 77 | + |
| 78 | +if __name__ == "__main__": |
| 79 | + try: |
| 80 | + app.run(debug=False, port=app.config["PORT"]) # nosec debug |
| 81 | + except Exception as exception: |
| 82 | + raise exception |
0 commit comments