10000 updating app.py · guptarohan41/botbuilder-python@4174cdb · GitHub
[go: up one dir, main page]

Skip to content

Commit 4174cdb

Browse files
committed
updating app.py
1 parent 1015d99 commit 4174cdb

File tree

1 file changed

+24
-33
lines changed

1 file changed

+24
-33
lines changed

scenarios/link-unfurling/app.py

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,29 @@
1-
# Copyright (c) Microsoft Corporation. All rights reserved.
2-
# Licensed under the MIT License.
3-
4-
import asyncio
1+
import json
52
import sys
63
from datetime import datetime
7-
from types import MethodType
84

9-
from flask import Flask, request, Response
5+
from aiohttp import web
6+
from aiohttp.web import Request, Response, json_response
7+
108
from botbuilder.core import (
119
BotFrameworkAdapterSettings,
1210
TurnContext,
1311
BotFrameworkAdapter,
1412
)
15-
from botbuilder.schema import Activity, ActivityTypes
1613

14+
from botbuilder.schema import Activity, ActivityTypes
1715
from bots import LinkUnfurlingBot
16+
from config import DefaultConfig
1817

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

2420
# Create adapter.
2521
# 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)
2723
ADAPTER = BotFrameworkAdapter(SETTINGS)
2824

29-
3025
# 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):
3427
# This check writes out errors to console log .vs. app insights.
3528
# NOTE: In production environment, you should consider logging this to Azure
3629
# application insights.
@@ -52,41 +45,39 @@ async def on_error( # pylint: disable=unused-argument
5245
value=f"{error}",
5346
value_type="https://www.botframework.com/schemas/error",
5447
)
48+
5549
# Send a trace activity, which will be displayed in Bot Framework Emulator
5650
await context.send_activity(trace_activity)
5751

58-
59-
ADAPTER.on_turn_error = MethodType(on_error, ADAPTER)
52+
ADAPTER.on_turn_error = on_error
6053

6154
# Create the Bot
6255
BOT = LinkUnfurlingBot()
6356

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:
6759
# 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()
7062
else:
7163
return Response(status=415)
7264

7365
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 ""
7767

7868
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)
8372
return Response(status=201)
8473
except Exception as exception:
8574
raise exception
8675

76+
APP = web.Application()
77+
APP.router.add_post("/api/messages", messages)
8778

8879
if __name__ == "__main__":
8980
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

Comments
 (0)
0