8000 Merge pull request #1399 from microsoft/axsuarez/avoid-typing-activit… · microsoft/botbuilder-python@b9678d5 · GitHub
[go: up one dir, main page]

Skip to content

Commit b9678d5

Browse files
authored
Merge pull request #1399 from microsoft/axsuarez/avoid-typing-activity-skill
Avoid typing activity if is skill
2 parents 26df914 + de1aba5 commit b9678d5

File tree

3 files changed

+54
-8
lines changed

3 files changed

+54
-8
lines changed

libraries/botbuilder-core/botbuilder/core/adapters/test_adapter.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ async def process_activity(
153153
self._conversation_lock.release()
154154

155155
activity.timestamp = activity.timestamp or datetime.utcnow()
156-
await self.run_pipeline(TurnContext(self, activity), logic)
156+
await self.run_pipeline(self.create_turn_context(activity), logic)
157157

158158
async def send_activities(
159159
self, context, activities: List[Activity]
@@ -227,7 +227,7 @@ async def create_conversation(
227227
members_removed=[],
228228
conversation=ConversationAccount(id=str(uuid.uuid4())),
229229
)
230-
context = TurnContext(self, update)
230+
context = self.create_turn_context(update)
231231
return await callback(context)
232232

233233
async def receive_activity(self, activity):
@@ -252,7 +252,7 @@ async def receive_activity(self, activity):
252252
request.id = str(self._next_id)
253253

254254
# Create context object and run middleware.
255-
context = TurnContext(self, request)
255+
context = self.create_turn_context(request)
256256
return await self.run_pipeline(context, self.logic)
257257

258258
def get_next_activity(self) -> Activity:
@@ -534,6 +534,9 @@ async def exchange_token_from_credentials(
534534

535535
return None
536536

537+
def create_turn_context(self, activity: Activity) -> TurnContext:
538+
return TurnContext(self, activity)
539+
537540

538541
class TestFlow:
539542
__test__ = False

libraries/botbuilder-core/botbuilder/core/show_typing_middleware.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
from typing import Awaitable, Callable
55

66
from botbuilder.schema import Activity, ActivityTypes
7+
from botframework.connector.auth import ClaimsIdentity, SkillValidation
78

9+
from .bot_adapter import BotAdapter
810
from .middleware_set import Middleware
911
from .turn_context import TurnContext
1012

@@ -82,9 +84,12 @@ async def aux():
8284
def stop_interval():
8385
timer.set_clear_timer()
8486

85-
# if it's a message, start sending typing activities until the
86-
# bot logic is done.
87-
if context.activity.type == ActivityTypes.message:
87+
# Start a timer to periodically send the typing activity
88+
# (bots running as skills should not send typing activity)
89+
if (
90+
context.activity.type == ActivityTypes.message
91+
and not ShowTypingMiddleware._is_skill_bot(context)
92+
):
8893
start_interval(context, self._delay, self._period)
8994

9095
# call the bot logic
@@ -93,3 +98,10 @@ def stop_interval():
9398
stop_interval()
9499

95100
return result
101+
102+
@staticmethod
103+
def _is_skill_bot(context: TurnContext) -> bool:
104+
claims_identity = context.turn_state.get(BotAdapter.BOT_IDENTITY_KEY)
105+
return isinstance(
106+
claim 8000 s_identity, ClaimsIdentity
107+
) and SkillValidation.is_skill_claim(claims_identity.claims)

libraries/botbuilder-core/tests/test_show_typing_middleware.py

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,31 @@
11
# Copyright (c) Microsoft Corporation. All rights reserved.
22
# Licensed under the MIT License.
33
import asyncio
4+
from uuid import uuid4
45
import aiounittest
56

6-
from botbuilder.core import ShowTypingMiddleware
7+
from botbuilder.core import ShowTypingMiddleware, TurnContext
78
from botbuilder.core.adapters import TestAdapter
8-
from botbuilder.schema import ActivityTypes
9+
from botbuilder.schema import Activity, ActivityTypes
10+
from botframework.connector.auth import AuthenticationConstants, ClaimsIdentity
11+
12+
13+
class SkillTestAdapter(TestAdapter):
14+
def create_turn_context(self, activity: Activity) -> TurnContext:
15+
turn_context = super().create_turn_context(activity)
16+
17+
claims_identity = ClaimsIdentity(
18+
claims={
19+
AuthenticationConstants.VERSION_CLAIM: "2.0",
20+
AuthenticationConstants.AUDIENCE_CLAIM: str(uuid4()),
21+
AuthenticationConstants.AUTHORIZED_PARTY: str(uuid4()),
22+
},
23+
is_authenticated=True,
24+
)
25+
26+
turn_context.turn_state[self.BOT_IDENTITY_KEY] = claims_identity
27+
28+
return turn_context
929

1030

1131
class TestShowTypingMiddleware(aiounittest.AsyncTestCase):
@@ -65,3 +85,14 @@ def assert_is_message(activity, description): # pylint: disable=unused-argument
6585

6686
step1 = await adapter.send("foo")
6787
await step1.assert_reply(assert_is_message)
88+
89+
async def test_not_send_not_send_typing_indicator_when_bot_running_as_skill(self):
90+
async def aux(context):
91+
await asyncio.sleep(1)
92+
await context.send_activity(f"echo:{context.activity.text}")
93+
94+
skill_adapter = SkillTestAdapter(aux)
95+
skill_adapter.use(ShowTypingMiddleware(0.001, 1))
96+
97+
step1 = await skill_adapter.send("foo")
98+
await step1.assert_reply("echo:foo")

0 commit comments

Comments
 (0)
0