1
1
# Copyright (c) Microsoft Corporation. All rights reserved.
2
2
# Licensed under the MIT License.
3
3
4
- import asyncio
5
4
import sys
6
5
from datetime import datetime
7
6
8
- from flask import Flask , request , Response
7
+ from aiohttp import web
8
+ from aiohttp .web import Request , Response
9
9
from botbuilder .core import BotFrameworkAdapterSettings , TurnContext , BotFrameworkAdapter
10
10
from botbuilder .schema import Activity , ActivityTypes
11
11
12
12
from bots import EchoBot
13
+ from config import DefaultConfig
13
14
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" )
15
+ CONFIG = DefaultConfig ()
18
16
19
17
# Create adapter.
20
18
# 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" ] )
19
+ SETTINGS = BotFrameworkAdapterSettings (CONFIG . APP_ID , CONFIG . APP_PASSWORD )
22
20
ADAPTER = BotFrameworkAdapter (SETTINGS )
23
21
24
22
@@ -52,31 +50,41 @@ async def on_error(context: TurnContext, error: Exception):
52
50
BOT = EchoBot ()
53
51
54
52
# Listen for incoming requests on /api/messages
55
- @app .route ("/api/messages" , methods = ["POST" ])
56
- def messages ():
53
+ async def messages (req : Request ) -> Response :
57
54
# Main bot message handler.
58
- if "application/json" in request .headers ["Content-Type" ]:
59
- body = request .json
55
+ if "application/json" in req .headers ["Content-Type" ]:
56
+ body = await req .json ()
60
57
else :
61
58
return Response (status = 415 )
62
59
63
60
activity = Activity ().deserialize (body )
64
61
auth_header = (
65
- request .headers ["Authorization" ] if "Authorization" in request .headers else ""
62
+ req .headers ["Authorization" ] if "Authorization" in req .headers else ""
66
63
)
67
64
68
65
try :
69
- task = LOOP .create_task (
70
- ADAPTER .process_activity (activity , auth_header , BOT .on_turn )
71
- )
72
- LOOP .run_until_complete (task )
66
+ await ADAPTER .process_activity (activity , auth_header , BOT .on_turn )
73
67
return Response (status = 201 )
74
68
except Exception as exception :
75
69
raise exception
76
70
71
+ def app ():
72
+ APP = web .Application ()
73
+ APP .router .add_post ("/api/messages" , messages )
74
+ return APP
75
+
76
+ #this is the code needed for the deployment template startup command
77
+ def init_func (argv ):
78
+ try :
79
+ APP = app ()
80
+ except Exception as error :
81
+ raise error
82
+
83
+ return APP
77
84
85
+ #this part is needed if you start your bot with 'py app.py' instead of the deployed command.
78
86
if __name__ == "__main__" :
79
87
try :
80
- app . run ( debug = False , port = app . config [ " PORT" ]) # nosec debug
81
- except Exception as exception :
82
- raise exception
88
+ web . run_app ( app (), host = "localhost" , port = CONFIG . PORT )
89
+ except Exception as error :
90
+ raise error
0 commit comments