8000 Merge pull request #501 from microsoft/jj/echo-aiohttp · TheCompuGuru/botbuilder-python@853053b · GitHub
[go: up one dir, main page]

Skip to content
< 8000 header class="HeaderMktg header-logged-out js-details-container js-header Details f4 py-3" role="banner" data-is-top="true" data-color-mode=light data-light-theme=light data-dark-theme=dark>

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 853053b

Browse files
authored
Merge pull request microsoft#501 from microsoft/jj/echo-aiohttp
JJ/echo aiohttp
2 parents 5880917 + 67916da commit 853053b

File tree

3 files changed

+29
-20
lines changed

3 files changed

+29
-20
lines changed

samples/02.echo-bot/app.py

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
11
# Copyright (c) Microsoft Corporation. All rights reserved.
22
# Licensed under the MIT License.
33

4-
import asyncio
54
import sys
65
from datetime import datetime
76

8-
from flask import Flask, request, Response
7+
from aiohttp import web
8+
from aiohttp.web import Request, Response
99
from botbuilder.core import BotFrameworkAdapterSettings, TurnContext, BotFrameworkAdapter
1010
from botbuilder.schema import Activity, ActivityTypes
1111

1212
from bots import EchoBot
13+
from config import DefaultConfig
1314

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()
1816

1917
# Create adapter.
2018
# 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)
2220
ADAPTER = BotFrameworkAdapter(SETTINGS)
2321

2422

@@ -52,31 +50,41 @@ async def on_error(context: TurnContext, error: Exception):
5250
BOT = EchoBot()
5351

5452
# Listen for incoming requests on /api/messages
55-
@app.route("/api/messages", methods=["POST"])
56-
def messages():
53+
async def messages(req: Request) -> Response:
5754
# 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()
6057
else:
6158
return Response(status=415)
6259

6360
activity = Activity().deserialize(body)
6461
auth_header = (
65-
request.headers["Authorization"] if "Authorization" in request.headers else ""
62+
req.headers["Authorization"] if "Authorization" in req.headers else ""
6663
)
6764

6865
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)
7367
return Response(status=201)
7468
except Exception as exception:
7569
raise exception
7670

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
7784

85+
#this part is needed if you start your bot with 'py app.py' instead of the deployed command.
7886
if __name__ == "__main__":
7987
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

samples/02.echo-bot/deploymentTemplates/template-with-preexisting-rg.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@
189189
"use32BitWorkerProcess": true,
190190
"webSocketsEnabled": false,
191191
"alwaysOn": false,
192-
"appCommandLine": "",
192+
"appCommandLine": "python -m aiohttp.web -H 0.0.0.0 -P 8000 app:init_func",
193193
"managedPipelineMode": "Integrated",
194194
"virtualApplications": [
195195
{

samples/02.echo-bot/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
botbuilder-core>=4.4.0b1
22
flask>=1.0.3
3+
aiohttp>=3.6.2

0 commit comments

Comments
 (0)
0