Commit fb512245 by Oleksandr Barabash

PA message handler added

parent 18efe630
...@@ -252,11 +252,12 @@ async def v1_pa_message(request: Request) -> Response: ...@@ -252,11 +252,12 @@ async def v1_pa_message(request: Request) -> Response:
try: try:
body = json_loads(await request.text()) body = json_loads(await request.text())
pa_message = PAMessage.get_schema().load(body) pa_message = PAMessage.get_schema().load(body)
notification_id = await BOT.send_message(pa_message.conversation_id, response = await BOT.send_message(pa_message.conversation_id,
pa_message.tenant_id, pa_message.tenant_id,
pa_message.text, pa_message.text,
pa_message.card) pa_message.card)
Log.d(TAG, f"v1_pa_message::notification: '{notification_id}'") Log.d(TAG, f"v1_pa_message::notification: '{response}'")
return make_response(200, "OK")
except Exception: except Exception:
Log.e(TAG, "v1_pa_message::error sending message", Log.e(TAG, "v1_pa_message::error sending message",
exc_info=sys.exc_info()) exc_info=sys.exc_info())
......
...@@ -5,6 +5,7 @@ from asyncio import Future ...@@ -5,6 +5,7 @@ from asyncio import Future
from typing import Optional, Dict from typing import Optional, Dict
from urllib.parse import urlparse, parse_qsl, urlencode from urllib.parse import urlparse, parse_qsl, urlencode
import aiohttp
from aiohttp.web_app import Application from aiohttp.web_app import Application
from botbuilder.core import (TurnContext, CardFactory, BotFrameworkAdapter, from botbuilder.core import (TurnContext, CardFactory, BotFrameworkAdapter,
BotFrameworkAdapterSettings) BotFrameworkAdapterSettings)
...@@ -236,49 +237,75 @@ class TeamsMessagingExtensionsActionPreviewBot(TeamsActivityHandler): ...@@ -236,49 +237,75 @@ class TeamsMessagingExtensionsActionPreviewBot(TeamsActivityHandler):
await turn_context.send_activity(i18n.t("unknown_request")) await turn_context.send_activity(i18n.t("unknown_request"))
async def on_message_activity(self, turn_context: TurnContext) -> None: async def on_message_activity(self, turn_context: TurnContext) -> None:
""" on message activity """
i18n = get_i18n(turn_context) i18n = get_i18n(turn_context)
if turn_context.activity.conversation.tenant_id != AppConfig.TENANT_ID: if turn_context.activity.conversation.tenant_id != AppConfig.TENANT_ID:
await turn_context.send_activity(i18n.t("tenant_forbidden")) await turn_context.send_activity(i18n.t("tenant_forbidden"))
return return
# try to save conversation reference,
# who knows maybe we didn't get the on_conversation_update!
await self.cosmos_client.create_conversation_reference(turn_context) await self.cosmos_client.create_conversation_reference(turn_context)
if turn_context.activity.value is not None: if turn_context.activity.value is not None:
return await self.handle_submit_action(turn_context) # TODO(s1z): translate this string
return await turn_context.send_activity("Sorry, not supported! :(")
message = turn_context.activity.text.strip().lower()
cmd_help = i18n.t("cmd_help") # send request to PA
cmd_portal = i18n.t("cmd_portal") async with aiohttp.ClientSession() as session:
# TODO(s1z): string bot's @mention if needed.
if message == cmd_help.lower(): message = turn_context.activity.text.strip().lower()
tenant_id = turn_context.activity.conversation.tenant_id tenant_id = turn_context.activity.conversation.tenant_id
conversation_id = turn_context.activity.conversation.id conversation_id = turn_context.activity.conversation.id
response = await turn_context.send_activity( data = dict(conversationId=conversation_id, tenantId=tenant_id,
i18n.t("response_help", text=message)
cmd_portal=cmd_portal, async with session.post(AppConfig.PA_URL, data=data) as resp:
cmd_help=cmd_help, Log.e(TAG, f"on_message_activity::response.status:"
tenant_id=tenant_id, f"{resp.status}")
conversation_id=conversation_id) rest_text = await resp.text()
) Log.e(TAG, f"on_message_activity::response.text: {rest_text}")
Log.d(TAG, "on_message_activity::help_resp: {}".format(response)) return
return
if message == cmd_portal.lower():
card = CardHelper.load_assets_card("default_card")
attachments = [CardFactory.adaptive_card(card)]
message = Activity(type=ActivityTypes.message,
attachments=attachments)
await turn_context.send_activity(message)
return
await turn_context.send_activity(i18n.t("response_unknown_cmd", # async def on_message_activity(self, turn_context: TurnContext) -> None:
cmd_help=cmd_help)) # """ on message activity """
#
# i18n = get_i18n(turn_context)
#
# if turn_context.activity.conversation.tenant_id != AppConfig.TENANT_ID:
# await turn_context.send_activity(i18n.t("tenant_forbidden"))
# return
#
# # try to save conversation reference,
# # who knows maybe we didn't get the on_conversation_update!
# await self.cosmos_client.create_conversation_reference(turn_context)
#
# if turn_context.activity.value is not None:
# return await self.handle_submit_action(turn_context)
#
# message = turn_context.activity.text.strip().lower()
#
# cmd_help = i18n.t("cmd_help")
# cmd_portal = i18n.t("cmd_portal")
#
# if message == cmd_help.lower():
# tenant_id = turn_context.activity.conversation.tenant_id
# conversation_id = turn_context.activity.conversation.id
# response = await turn_context.send_activity(
# i18n.t("response_help",
# cmd_portal=cmd_portal,
# cmd_help=cmd_help,
# tenant_id=tenant_id,
# conversation_id=conversation_id)
# )
# Log.d(TAG, "on_message_activity::help_resp: {}".format(response))
# return
#
# if message == cmd_portal.lower():
# card = CardHelper.load_assets_card("default_card")
# attachments = [CardFactory.adaptive_card(card)]
# message = Activity(type=ActivityTypes.message,
# attachments=attachments)
# await turn_context.send_activity(message)
# return
#
# await turn_context.send_activity(i18n.t("response_unknown_cmd",
# cmd_help=cmd_help))
async def on_mx_task_unsupported(self, turn_context: TurnContext) \ async def on_mx_task_unsupported(self, turn_context: TurnContext) \
-> TaskModuleResponse: -> TaskModuleResponse:
......
...@@ -71,6 +71,14 @@ class AppConfig: ...@@ -71,6 +71,14 @@ class AppConfig:
WEB_APP_NAME = os.environ.get("WEB_APP_NAME", "wa-name") WEB_APP_NAME = os.environ.get("WEB_APP_NAME", "wa-name")
APP_ID = os.environ.get("MS_APP_ID", "app-id") APP_ID = os.environ.get("MS_APP_ID", "app-id")
APP_PASSWORD = os.environ.get("MS_APP_PASSWORD", "app-password") APP_PASSWORD = os.environ.get("MS_APP_PASSWORD", "app-password")
PA_URL = os.environ.get(
"PA_URL", ("https://prod-192.westeurope.logic.azure.com:443"
"/workflows/22a28a2927454e6eb935484d84097132"
"/triggers/manual/paths/"
"invoke?api-version=2016-06-01&"
"sp=%2Ftriggers%2Fmanual%2Frun&sv=1.0"
"&sig=7cNmhQciqmG-CGzOWdS6gTXocXN7fn-900hU04riygs")
)
class CosmosDBConfig: class CosmosDBConfig:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment