1
- # Copyright (c) Microsoft Corporation. All rights reserved.
2
- # Licensed under the MIT License.
3
-
4
- import asyncio
1
+ import json
5
2
import sys
6
3
from datetime import datetime
7
- from types import MethodType
8
4
9
- from flask import Flask , request , Response
5
+ from aiohttp import web
6
+ from aiohttp .web import Request , Response , json_response
7
+
10
8
from botbuilder .core import (
11
9
BotFrameworkAdapterSettings ,
12
10
TurnContext ,
13
11
BotFrameworkAdapter ,
14
12
)
15
- from botbuilder .schema import Activity , ActivityTypes
16
13
14
+ from botbuilder .schema import Activity , ActivityTypes
17
15
from bots import LinkUnfurlingBot
16
+ from config import DefaultConfig
18
17
19
- # Create the loop and Flask app
20
- LOOP = asyncio .get_event_loop ()
21
- APP = Flask (__name__ , instance_relative_config = True )
22
- APP .config .from_object ("config.DefaultConfig" )
18
+ CONFIG = DefaultConfig ()
23
19
24
20
# Create adapter.
25
21
# 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" ] )
22
+ SETTINGS = BotFrameworkAdapterSettings (CONFIG . APP_ID , CONFIG . APP_PASSWORD )
27
23
ADAPTER = BotFrameworkAdapter (SETTINGS )
28
24
29
-
30
25
# Catch-all for errors.
31
- async def on_error ( # pylint: disable=unused-argument
32
- self , context : TurnContext , error : Exception
33
- ):
26
+ async def on_error (context : TurnContext , error : Exception ):
34
27
# This check writes out errors to console log .vs. app insights.
35
28
# NOTE: In production environment, you should consider logging this to Azure
36
29
# application insights.
@@ -52,41 +45,39 @@ async def on_error( # pylint: disable=unused-argument
52
45
value = f"{ error } " ,
53
46
value_type = "https://www.botframework.com/schemas/error" ,
54
47
)
48
+
55
49
# Send a trace activity, which will be displayed in Bot Framework Emulator
56
50
await context .send_activity (trace_activity )
57
51
58
-
59
- ADAPTER .on_turn_error = MethodType (on_error , ADAPTER )
52
+ ADAPTER .on_turn_error = on_error
60
53
61
54
# Create the Bot
62
55
BOT = LinkUnfurlingBot ()
63
56
64
- # Listen for incoming requests on /api/messages.s
65
- @APP .route ("/api/messages" , methods = ["POST" ])
66
- def messages ():
57
+ # Listen for incoming requests on /api/messages
58
+ async def messages (req : Request ) -> Response :
67
59
# Main bot message handler.
68
- if "application/json" in request .headers ["Content-Type" ]:
69
- body = request .json
60
+ if "application/json" in req .headers ["Content-Type" ]:
61
+ body = await req .json ()
70
62
else :
71
63
return Response (status = 415 )
72
64
73
65
activity = Activity ().deserialize (body )
74
- auth_header = (
75
- request .headers ["Authorization" ] if "Authorization" in request .headers else ""
76
- )
66
+ auth_header = req .headers ["Authorization" ] if "Authorization" in req .headers else ""
77
67
78
68
try :
79
- task = LOOP .create_task (
80
- ADAPTER .process_activity (activity , auth_header , BOT .on_turn )
81
- )
82
- LOOP .run_until_complete (task )
69
+ response = await ADAPTER .process_activity (activity , auth_header , BOT .on_turn )
70
+ if response :
71
+ return json_response (data = response .body , status = response .status )
83
72
return Response (status = 201 )
84
73
except Exception as exception :
85
74
raise exception
86
75
76
+ APP = web .Application ()
77
+ APP .router .add_post ("/api/messages" , messages )
87
78
88
79
if __name__ == "__main__" :
89
80
try :
90
- APP . run ( debug = False , port = APP . config [ " PORT" ]) # nosec debug
91
- except Exception as exception :
92
- raise exception
81
+ web . run_app ( APP , host = "localhost" , port = CONFIG . PORT )
82
+ except Exception as error :
83
+ raise error
0 commit comments