Skip to content

[Teams] Invoke Response serialization #502

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Dec 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions libraries/botbuilder-core/botbuilder/core/bot_framework_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
from .bot_adapter import BotAdapter
from .turn_context import TurnContext
from .user_token_provider import UserTokenProvider
from .invoke_response import InvokeResponse
from .conversation_reference_extension import get_continuation_activity

USER_AGENT = f"Microsoft-BotFramework/3.1 (BotBuilder Python/{__version__})"
Expand Down Expand Up @@ -263,7 +264,17 @@ async def process_activity(self, req, auth_header: str, logic: Callable):
teams_channel_data["tenant"]["id"]
)

return await self.run_pipeline(context, logic)
await self.run_pipeline(context, logic)

if activity.type == ActivityTypes.invoke:
invoke_response = context.turn_state.get(
BotFrameworkAdapter._INVOKE_RESPONSE_KEY # pylint: disable=protected-access
)
if invoke_response is None:
return InvokeResponse(status=501)
return invoke_response.value

return None

async def authenticate_request(
self, request: Activity, auth_header: str
Expand All @@ -283,7 +294,7 @@ async def authenticate_request(
)

if not claims.is_authenticated:
raise Exception("Unauthorized Access. Request is not authorized")
raise PermissionError("Unauthorized Access. Request is not authorized")

return claims

Expand Down
3 changes: 2 additions & 1 deletion libraries/botbuilder-core/botbuilder/core/teams/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from .teams_activity_handler import TeamsActivityHandler
from .teams_info import TeamsInfo
from .teams_helper import deserializer_helper
from .teams_helper import deserializer_helper, serializer_helper
from .teams_activity_extensions import (
teams_get_channel_id,
teams_get_team_info,
Expand All @@ -21,4 +21,5 @@
"teams_get_channel_id",
"teams_get_team_info",
"teams_notify_user",
"serializer_helper",
]
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
from botbuilder.schema import Activity, ActivityTypes, ChannelAccount
from botbuilder.core import ActivityHandler, InvokeResponse, BotFrameworkAdapter
from botbuilder.core.turn_context import TurnContext
from botbuilder.core.teams.teams_helper import deserializer_helper, serializer_helper
from botbuilder.core.teams.teams_info import TeamsInfo
from botbuilder.core.teams.teams_helper import deserializer_helper
from botbuilder.schema.teams import (
AppBasedLinkQuery,
TeamInfo,
Expand Down Expand Up @@ -446,7 +446,7 @@ async def on_teams_channel_renamed_activity( # pylint: disable=unused-argument

@staticmethod
def _create_invoke_response(body: object = None) -> InvokeResponse:
return InvokeResponse(status=int(HTTPStatus.OK), body=body)
return InvokeResponse(status=int(HTTPStatus.OK), body=serializer_helper(body))


class _InvokeResponseException(Exception):
Expand Down
25 changes: 24 additions & 1 deletion libraries/botbuilder-core/botbuilder/core/teams/teams_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from typing import Type
from enum import Enum

from msrest.serialization import Model, Deserializer
from msrest.serialization import Model, Deserializer, Serializer

import botbuilder.schema as schema
import botbuilder.schema.teams as teams_schema
Expand All @@ -22,3 +22,26 @@ def deserializer_helper(msrest_cls: Type[Model], dict_to_deserialize: dict) -> M
dependencies_dict = {dependency.__name__: dependency for dependency in dependencies}
deserializer = Deserializer(dependencies_dict)
return deserializer(msrest_cls.__name__, dict_to_deserialize)


# TODO consolidate these two methods


def serializer_helper(object_to_serialize: Model) -> dict:
if object_to_serialize is None:
return None

dependencies = [
schema_cls
for key, schema_cls in getmembers(schema)
if isinstance(schema_cls, type) and issubclass(schema_cls, (Model, Enum))
]
dependencies += [
schema_cls
for key, schema_cls in getmembers(teams_schema)
if isinstance(schema_cls, type) and issubclass(schema_cls, (Model, Enum))
]
dependencies_dict = {dependency.__name__: dependency for dependency in dependencies}
serializer = Serializer(dependencies_dict)
# pylint: disable=protected-access
return serializer._serialize(object_to_serialize)
Original file line number Diff line number Diff line change
Expand Up @@ -1538,6 +1538,7 @@ class TeamsChannelAccount(ChannelAccount):
"surname": {"key": "surname", "type": "str"},
"email": {"key": "email", "type": "str"},
"userPrincipalName": {"key": "userPrincipalName", "type": "str"},
"aad_object_id": {"key": "objectId", "type": "str"},
}

def __init__(self, **kwargs):
Expand Down