8000 Merge pull request #502 from microsoft/eric/invokeResponseSerialization · guptarohan41/botbuilder-python@c013b52 · GitHub
[go: up one dir, main page]

Skip to content

Commit c013b52

Browse files
authored
Merge pull request microsoft#502 from microsoft/eric/invokeResponseSerialization
[Teams] Invoke Response serialization
2 parents 177f34f + 4011bd9 commit c013b52

File tree

5 files changed

+42
-6
lines changed

5 files changed

+42
-6
lines changed

libraries/botbuilder-core/botbuilder/core/bot_framework_adapter.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
from .bot_adapter import BotAdapter
3939
from .turn_context import TurnContext
4040
from .user_token_provider import UserTokenProvider
41+
from .invoke_response import InvokeResponse
4142
from .conversation_reference_extension import get_continuation_activity
4243

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

266-
return await self.run_pipeline(context, logic)
267+
await self.run_pipeline(context, logic)
268+
269+
if activity.type == ActivityTypes.invoke:
270+
invoke_response = context.turn_state.get(
271+
BotFrameworkAdapter._INVOKE_RESPONSE_KEY # pylint: disable=protected-access
272+
)
273+
if invoke_response is None:
274+
return InvokeResponse(status=501)
275+
return invoke_response.value
276+
277+
return None
267278

268279
async def authenticate_request(
269280
self, request: Activity, auth_header: str
@@ -283,7 +294,7 @@ async def authenticate_request(
283294
)
284295

285296
if not claims.is_authenticated:
286-
raise Exception("Unauthorized Access. Request is not authorized")
297+
raise PermissionError("Unauthorized Access. Request is not authorized")
287298

288299
return claims
289300

libraries/botbuilder-core/botbuilder/core/teams/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from .teams_activity_handler import TeamsActivityHandler
99
from .teams_info import TeamsInfo
10-
from .teams_helper import deserializer_helper
10+
from .teams_helper import deserializer_helper, serializer_helper
1111
from .teams_activity_extensions import (
1212
teams_get_channel_id,
1313
teams_get_team_info,
@@ -21,4 +21,5 @@
2121
"teams_get_channel_id",
2222
"teams_get_team_info",
2323
"teams_notify_user",
24+
"serializer_helper",
2425
]

libraries/botbuilder-core/botbuilder/core/teams/teams_activity_handler.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
from botbuilder.schema import Activity, ActivityTypes, ChannelAccount
66
from botbuilder.core import ActivityHandler, InvokeResponse, BotFrameworkAdapter
77
from botbuilder.core.turn_context import TurnContext
8+
from botbuilder.core.teams.teams_helper import deserializer_helper, serializer_helper
89
from botbuilder.core.teams.teams_info import TeamsInfo
9-
from botbuilder.core.teams.teams_helper import deserializer_helper
1010
from botbuilder.schema.teams import (
1111
AppBasedLinkQuery,
1212
TeamInfo,
@@ -446,7 +446,7 @@ async def on_teams_channel_renamed_activity( # pylint: disable=unused-argument
446446

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

451451

452452
class _InvokeResponseException(Exception):

libraries/botbuilder-core/botbuilder/core/teams/teams_helper.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from typing import Type
33
from enum import Enum
44

5-
from msrest.serialization import Model, Deserializer
5+
from msrest.serialization import Model, Deserializer, Serializer
66

77
import botbuilder.schema as schema
88
import botbuilder.schema.teams as teams_schema
@@ -22,3 +22,26 @@ def deserializer_helper(msrest_cls: Type[Model], dict_to_deserialize: dict) -> M
2222
dependencies_dict = {dependency.__name__: dependency for dependency in dependencies}
2323
deserializer = Deserializer(dependencies_dict)
2424
return deserializer(msrest_cls.__name__, dict_to_deserialize)
25+
26+
27+
# TODO consolidate these two methods
28+
29+
30+
def serializer_helper(object_to_serialize: Model) -> dict:
31+
if object_to_serialize is None:
32+
return None
33+
34+
dependencies = [
35+
schema_cls
36+
for key, schema_cls in getmembers(schema)
37+
if isinstance(schema_cls, type) and issubclass(schema_cls, (Model, Enum))
38+
]
39+
dependencies += [
40+
schema_cls
41+
for key, schema_cls in getmembers(teams_schema)
42+
if isinstance(schema_cls, type) and issubclass(schema_cls, (Model, Enum))
43+
]
44+
dependencies_dict = {dependency.__name__: dependency for dependency in dependencies}
45+
serializer = Serializer(dependencies_dict)
46+
# pylint: disable=protected-access
47+
return serializer._serialize(object_to_serialize)

libraries/botbuilder-schema/botbuilder/schema/teams/_models.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1538,6 +1538,7 @@ class TeamsChannelAccount(ChannelAccount):
15381538
"surname": {"key": "surname", "type": "str"},
15391539
"email": {"key": "email", "type": "str"},
15401540
"userPrincipalName": {"key": "userPrincipalName", "type": "str"},
1541+
"aad_object_id": {"key": "objectId", "type": "str"},
15411542
}
15421543

15431544
def __init__(self, **kwargs):

0 commit comments

Comments
 (0)
0