From 5bea08e229c010a48636b04534e068d830c08944 Mon Sep 17 00:00:00 2001 From: tracyboehrer Date: Thu, 14 May 2020 08:25:24 -0500 Subject: [PATCH] Correctly returning InvokeResponse in Skills samples --- .../echo-skill-bot/app.py | 16 +++++++--------- .../simple-root-bot/app.py | 14 ++++++++------ .../81.skills-skilldialog/dialog-root-bot/app.py | 13 +++++++------ .../dialog-skill-bot/app.py | 13 ++++++------- 4 files changed, 28 insertions(+), 28 deletions(-) diff --git a/samples/python/80.skills-simple-bot-to-bot/echo-skill-bot/app.py b/samples/python/80.skills-simple-bot-to-bot/echo-skill-bot/app.py index 21739dea20..3e18fbdad8 100644 --- a/samples/python/80.skills-simple-bot-to-bot/echo-skill-bot/app.py +++ b/samples/python/80.skills-simple-bot-to-bot/echo-skill-bot/app.py @@ -1,12 +1,11 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. -import sys -import traceback -from datetime import datetime +from http import HTTPStatus from aiohttp import web from aiohttp.web import Request, Response +from aiohttp.web_response import json_response from botbuilder.core import BotFrameworkAdapterSettings from botbuilder.schema import Activity from botframework.connector.auth import AuthenticationConfiguration @@ -40,16 +39,15 @@ async def messages(req: Request) -> Response: if "application/json" in req.headers["Content-Type"]: body = await req.json() else: - return Response(status=415) + return Response(status=HTTPStatus.UNSUPPORTED_MEDIA_TYPE) activity = Activity().deserialize(body) auth_header = req.headers["Authorization"] if "Authorization" in req.headers else "" - try: - await ADAPTER.process_activity(activity, auth_header, BOT.on_turn) - return Response(status=201) - except Exception as exception: - raise exception + invoke_response = await ADAPTER.process_activity(activity, auth_header, BOT.on_turn) + if invoke_response: + return json_response(data=invoke_response.body, status=invoke_response.status) + return Response(status=HTTPStatus.OK) APP = web.Application() diff --git a/samples/python/80.skills-simple-bot-to-bot/simple-root-bot/app.py b/samples/python/80.skills-simple-bot-to-bot/simple-root-bot/app.py index b2510cde57..812983a787 100644 --- a/samples/python/80.skills-simple-bot-to-bot/simple-root-bot/app.py +++ b/samples/python/80.skills-simple-bot-to-bot/simple-root-bot/app.py @@ -1,8 +1,10 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. +from http import HTTPStatus from aiohttp import web from aiohttp.web import Request, Response +from aiohttp.web_response import json_response from botbuilder.core import ( BotFrameworkAdapterSettings, ConversationState, @@ -59,22 +61,22 @@ ADAPTER, BOT, ID_FACTORY, CREDENTIAL_PROVIDER, AuthenticationConfiguration() ) + # Listen for incoming requests on /api/messages async def messages(req: Request) -> Response: # Main bot message handler. if "application/json" in req.headers["Content-Type"]: body = await req.json() else: - return Response(status=415) + return Response(status=HTTPStatus.UNSUPPORTED_MEDIA_TYPE) activity = Activity().deserialize(body) auth_header = req.headers["Authorization"] if "Authorization" in req.headers else "" - try: - await ADAPTER.process_activity(activity, auth_header, BOT.on_turn) - return Response(status=201) - except Exception as exception: - raise exception + invoke_response = await ADAPTER.process_activity(activity, auth_header, BOT.on_turn) + if invoke_response: + return json_response(data=invoke_response.body, status=invoke_response.status) + return Response(status=HTTPStatus.OK) APP = web.Application(middlewares=[aiohttp_error_middleware]) diff --git a/samples/python/81.skills-skilldialog/dialog-root-bot/app.py b/samples/python/81.skills-skilldialog/dialog-root-bot/app.py index 1fced377a9..68feb84d5b 100644 --- a/samples/python/81.skills-skilldialog/dialog-root-bot/app.py +++ b/samples/python/81.skills-skilldialog/dialog-root-bot/app.py @@ -1,8 +1,10 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. +from http import HTTPStatus from aiohttp import web from aiohttp.web import Request, Response +from aiohttp.web_response import json_response from botbuilder.core import ( BotFrameworkAdapterSettings, ConversationState, @@ -66,16 +68,15 @@ async def messages(req: Request) -> Response: if "application/json" in req.headers["Content-Type"]: body = await req.json() else: - return Response(status=415) + return Response(status=HTTPStatus.UNSUPPORTED_MEDIA_TYPE) activity = Activity().deserialize(body) auth_header = req.headers["Authorization"] if "Authorization" in req.headers else "" - try: - await ADAPTER.process_activity(activity, auth_header, BOT.on_turn) - return Response(status=201) - except Exception as exception: - raise exception + invoke_response = await ADAPTER.process_activity(activity, auth_header, BOT.on_turn) + if invoke_response: + return json_response(data=invoke_response.body, status=invoke_response.status) + return Response(status=HTTPStatus.OK) APP = web.Application(middlewares=[aiohttp_error_middleware]) diff --git a/samples/python/81.skills-skilldialog/dialog-skill-bot/app.py b/samples/python/81.skills-skilldialog/dialog-skill-bot/app.py index b96d33e68e..3a5a8c9672 100644 --- a/samples/python/81.skills-skilldialog/dialog-skill-bot/app.py +++ b/samples/python/81.skills-skilldialog/dialog-skill-bot/app.py @@ -1,5 +1,6 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. +from http import HTTPStatus from aiohttp import web from aiohttp.web import Request, Response, json_response @@ -48,17 +49,15 @@ async def messages(req: Request) -> Response: if "application/json" in req.headers["Content-Type"]: body = await req.json() else: - return Response(status=415) + return Response(status=HTTPStatus.UNSUPPORTED_MEDIA_TYPE) activity = Activity().deserialize(body) auth_header = req.headers["Authorization"] if "Authorization" in req.headers else "" - response = await ADAPTER.process_activity(activity, auth_header, BOT.on_turn) - if response: - return json_response(data=response.body, status=response.status) - # TODO: The body argument on the response below is a temporal workaround - # for more information go to https://github.com/microsoft/botbuilder-python/issues/878 - return Response(status=201, body='{"foo":"bar"}'.encode("utf-8")) + invoke_response = await ADAPTER.process_activity(activity, auth_header, BOT.on_turn) + if invoke_response: + return json_response(data=invoke_response.body, status=invoke_response.status) + return Response(status=HTTPStatus.OK) APP = web.Application(middlewares=[aiohttp_error_middleware])