|
5 | 5 | from datetime import datetime
|
6 | 6 | from types import MethodType
|
7 | 7 |
|
8 |
| -from flask import Flask, request, Response |
| 8 | +from aiohttp import web |
9 | 9 | from botbuilder.core import (
|
10 | 10 | BotFrameworkAdapterSettings,
|
11 | 11 | TurnContext,
|
|
15 | 15 | from botbuilder.schema import Activity, ActivityTypes
|
16 | 16 | from activity_log import ActivityLog
|
17 | 17 | from bots import MessageReactionBot
|
18 |
| -from threading_helper import run_coroutine |
| 18 | +# from threading_helper import run_coroutine |
| 19 | +from config import DefaultConfig |
19 | 20 |
|
20 | 21 | # Create the Flask app
|
21 |
| -APP = Flask(__name__, instance_relative_config=True) |
22 |
| -APP.config.from_object("config.DefaultConfig") |
| 22 | +CONFIG = DefaultConfig() |
| 23 | +PORT = CONFIG.PORT |
23 | 24 |
|
24 | 25 | # Create adapter.
|
25 | 26 | # See https://aka.ms/about-bot-adapter to learn more about how bots work.
|
26 |
| -SETTINGS = BotFrameworkAdapterSettings(APP.config["APP_ID"], APP.config["APP_PASSWORD"]) |
| 27 | +SETTINGS = BotFrameworkAdapterSettings(CONFIG.APP_ID, CONFIG.APP_PASSWORD) |
27 | 28 | ADAPTER = BotFrameworkAdapter(SETTINGS)
|
28 | 29 |
|
29 | 30 |
|
@@ -64,31 +65,29 @@ async def on_error( # pylint: disable=unused-argument
|
64 | 65 | BOT = MessageReactionBot(ACTIVITY_LOG)
|
65 | 66 |
|
66 | 67 | # Listen for incoming requests on /api/messages.s
|
67 |
| -@APP.route("/api/messages", methods=["POST"]) |
68 |
| -def messages(): |
| 68 | +async def messages(req: web.Request) -> web.Response: |
69 | 69 | # Main bot message handler.
|
70 |
| - if "application/json" in request.headers["Content-Type"]: |
71 |
| - body = request.json |
| 70 | + if "application/json" in req.headers["Content-Type"]: |
| 71 | + body = await req.json() |
72 | 72 | else:
|
73 |
| - return Response(status=415) |
| 73 | + return web.Response(status=415) |
74 | 74 |
|
75 | 75 | activity = Activity().deserialize(body)
|
76 | 76 | auth_header = (
|
77 |
| - request.headers["Authorization"] if "Authorization" in request.headers else "" |
| 77 | + req.headers["Authorization"] if "Authorization" in req.headers else "" |
78 | 78 | )
|
79 | 79 |
|
80 | 80 | try:
|
81 |
| - print("about to create task") |
82 |
| - print("about to run until complete") |
83 |
| - run_coroutine(ADAPTER.process_activity(activity, auth_header, BOT.on_turn)) |
84 |
| - print("is now complete") |
85 |
| - return Response(status=201) |
| 81 | + await ADAPTER.process_activity(activity, auth_header, BOT.on_turn) |
| 82 | + return web.Response(status=201) |
86 | 83 | except Exception as exception:
|
87 | 84 | raise exception
|
88 | 85 |
|
89 | 86 |
|
90 |
| -if __name__ == "__main__": |
91 |
| - try: |
92 |
| - APP.run(debug=False, port=APP.config["PORT"]) # nosec debug |
93 |
| - except Exception as exception: |
94 |
| - raise exception |
| 87 | +APP = web.Application() |
| 88 | +APP.router.add_post("/api/messages", messages) |
| 89 | + |
| 90 | +try: |
| 91 | + web.run_app(APP, host="localhost", port=PORT) |
| 92 | +except Exception as error: |
| 93 | + raise error |
0 commit comments