10000 Added trace activity helper in turn context (#359) · TheCompuGuru/botbuilder-python@9119769 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9119769

Browse files
authored
Added trace activity helper in turn context (microsoft#359)
1 parent d247303 commit 9119769

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

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

Lines changed: 22 additions & 1 deletion
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:
@@ -245,6 +252,20 @@ async def next_handler():
245252
# logic does not use parentheses because it's a coroutine
246253
return await logic
247254

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+
248269
@staticmethod
249270
def get_conversation_reference(activity: Activity) -> ConversationReference:
250271
"""

libraries/botbuilder-core/tests/test_turn_context.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
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,
@@ -33,7 +35,7 @@ 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

@@ -319,3 +321,28 @@ def test_should_remove_at_mention_from_activity(self):
319321

320322
assert text, " test activity"
321323
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