8000 Merge master into 4.5.0b5 (#360) · yosshy/botbuilder-python@f352df5 · GitHub
[go: up one dir, main page]

Skip to content

Commit f352df5

Browse files
authored
Merge master into 4.5.0b5 (microsoft#360)
* Apply conversation reference in TurnContext.update_activity (microsoft#358) * Create test_update_activity_should_apply_conversation_reference * Apply conversation reference in TurnContext.update_activity * Added trace activity helper in turn context (microsoft#359)
1 parent 37c361d commit f352df5

File tree

2 files changed

+77
-6
lines changed

2 files changed

+77
-6
lines changed

libraries/botbuilder-core/botbuilder/core/turn_context.py

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,15 @@
33

44
import re
55
from copy import copy
6+
from datetime import datetime
67
from typing import List, Callable, Union, Dict
7-
from botbuilder.schema import Activity, ConversationReference, Mention, ResourceResponse
8+
from botbuilder.schema import (
9+
Activity,
10+
ActivityTypes,
11+
ConversationReference,
12+
Mention,
13+
ResourceResponse,
14+
)
815

916

1017
class TurnContext:
@@ -173,9 +180,11 @@ async def update_activity(self, activity: Activity):
173180
:param activity:
174181
:return:
175182
"""
183+
reference = TurnContext.get_conversation_reference(self.activity)
184+
176185
return await self._emit(
177186
self._on_update_activity,
178-
activity,
187+
TurnContext.apply_conversation_reference(activity, reference),
179188
self.adapter.update_activity(self, activity),
180189
)
181190

@@ -240,9 +249,23 @@ async def next_handler():
240249
raise error
241250

242251
await emit_next(0)
243-
# This should be changed to `return await logic()`
252+
# logic does not use parentheses because it's a coroutine
244253
return await logic
245254

255+
async def send_trace_activity(
256+
self, name: str, value: object, value_type: str, label: str
257+
) -> ResourceResponse:
258+
trace_activity = Activity(
259+
type=ActivityTypes.trace,
260+
timestamp=datetime.utcnow(),
261+
name=name,
262+
value=value,
263+
value_type=value_type,
264+
label=label,
265+
)
266+
267+
return await self.send_activity(trace_activity)
268+
246269
@staticmethod
247270
def get_conversation_reference(activity: Activity) -> ConversationReference:
248271
"""

libraries/botbuilder-core/tests/test_turn_context.py

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
# Copyright (c) Microsoft Corporation. All rights reserved.
22
# Licensed under the MIT License.
33

4+
from typing import Callable, List
45
import aiounittest
56

67
from botbuilder.schema import (
78
Activity,
9+
ActivityTypes,
810
ChannelAccount,
911
ConversationAccount,
1012
Mention,
1113
ResourceResponse,
1214
)
13-
from botbuilder.core import BotAdapter, TurnContext
15+
from botbuilder.core import BotAdapter, MessageFactory, TurnContext
1416

1517
ACTIVITY = Activity(
1618
id="1234",
@@ -33,18 +35,19 @@ async def send_activities(self, context, activities):
3335
assert activities
3436
for (idx, activity) in enumerate(activities): # pylint: disable=unused-variable
3537
assert isinstance(activity, Activity)
36-
assert activity.type == "message"
38+
assert activity.type == "message" or activity.type == ActivityTypes.trace
3739
responses.append(ResourceResponse(id="5678"))
3840
return responses
3941

4042
async def update_activity(self, context, activity):
4143
assert context is not None
4244
assert activity is not None
45+
return ResourceResponse(id=activity.id)
4346

4447
async def delete_activity(self, context, reference):
4548
assert context is not None
4649
assert reference is not None
47-
assert reference.activity_id == "1234"
50+
assert reference.activity_id == ACTIVITY.id
4851

4952

5053
class TestBotContext(aiounittest.AsyncTestCase):
@@ -225,6 +228,26 @@ async def update_handler(context, activity, next_handler_coroutine):
225228
await context.update_activity(ACTIVITY)
226229
assert called is True
227230

231+
async def test_update_activity_should_apply_conversation_reference(self):
232+
activity_id = "activity ID"
233+
context = TurnContext(SimpleAdapter(), ACTIVITY)
234+
called = False
235+
236+
async def update_handler(context, activity, next_handler_coroutine):
237+
nonlocal called
238+
called = True
239+
assert context is not None
240+
assert activity.id == activity_id
241+
assert activity.conversation.id == ACTIVITY.conversation.id
242+
await next_handler_coroutine()
243+
244+
context.on_update_activity(update_handler)
245+
new_activity = MessageFactory.text("test text")
246+
new_activity.id = activity_id
247+
update_result = await context.update_activity(new_activity)
248+
assert called is True
249+
assert update_result.id == activity_id
250+
228251
def test_get_conversation_reference_should_return_valid_reference(self):
229252
reference = TurnContext.get_conversation_reference(ACTIVITY)
230253

@@ -298,3 +321,28 @@ def test_should_remove_at_mention_from_activity(self):
298321

299322
assert text, " test activity"
300323
assert activity.text, " test activity"
324+
325+
async def test_should_send_a_trace_activity(self):
326+
context = TurnContext(SimpleAdapter(), ACTIVITY)
327+
called = False
328+
329+
# pylint: disable=unused-argument
330+
async def aux_func(
331+
ctx: TurnContext, activities: List[Activity], next: Callable
332+
):
333+
nonlocal called
334+
called = True
335+
assert isinstance(activities, list), "activities not array."
336+
assert len(activities) == 1, "invalid count of activities."
337+
assert activities[0].type == ActivityTypes.trace, "type wrong."
338+
assert activities[0].name == "name-text", "name wrong."
339+
assert activities[0].value == "value-text", "value worng."
340+
assert activities[0].value_type == "valueType-text", "valeuType wrong."
341+
assert activities[0].label == "label-text", "label wrong."
342+
return []
343+
344+
context.on_send_activities(aux_func)
345+
await context.send_trace_activity(
346+
"name-text", "value-text", "valueType-text", "label-text"
347+
)
348+
assert called

0 commit comments

Comments
 (0)
0