async def on_turn(self, context: TurnContext, logic: Callable[[TurnContext], Awaitable]): message = f"{self._label} {context.activity.type} {context.activity.text}" print(message) # Register outgoing handler context.on_send_activities(self._outgoing_handler) await logic()
async def logic(context: TurnContext): if test_case != SkillFlowTestCase.root_bot_only: # Create a skill ClaimsIdentity and put it in turn_state so isSkillClaim() returns True. claims_identity = ClaimsIdentity({}, False) claims_identity.claims[ "ver" ] = "2.0" # AuthenticationConstants.VersionClaim claims_identity.claims[ "aud" ] = ( SimpleComponentDialog.skill_bot_id ) # AuthenticationConstants.AudienceClaim claims_identity.claims[ "azp" ] = ( SimpleComponentDialog.parent_bot_id ) # AuthenticationConstants.AuthorizedParty context.turn_state[BotAdapter.BOT_IDENTITY_KEY] = claims_identity if test_case == SkillFlowTestCase.root_bot_consuming_skill: # Simulate the SkillConversationReference with a channel OAuthScope stored in turn_state. # This emulates a response coming to a root bot through SkillHandler. context.turn_state[ SkillHandler.SKILL_CONVERSATION_REFERENCE_KEY ] = SkillConversationReference( None, AuthenticationConstants.TO_CHANNEL_FROM_BOT_OAUTH_SCOPE ) if test_case == SkillFlowTestCase.middle_skill: # Simulate the SkillConversationReference with a parent Bot ID stored in turn_state. # This emulates a response coming to a skill from another skill through SkillHandler. context.turn_state[ SkillHandler.SKILL_CONVERSATION_REFERENCE_KEY ] = SkillConversationReference( None, SimpleComponentDialog.parent_bot_id ) async def aux( turn_context: TurnContext, # pylint: disable=unused-argument activities: List[Activity], next: Callable, ): for activity in activities: if activity.type == ActivityTypes.end_of_conversation: SimpleComponentDialog.eoc_sent = activity break return await next() # Interceptor to capture the EoC activity if it was sent so we can assert it in the tests. context.on_send_activities(aux) SimpleComponentDialog.dm_turn_result = await dialog_manager.on_turn(context)
def test_copy_to_should_copy_all_references(self): # pylint: disable=protected-access old_adapter = SimpleAdapter() old_activity = Activity(id="2", type="message", text="test copy") old_context = TurnContext(old_adapter, old_activity) old_context.responded = True async def send_activities_handler(context, activities, next_handler): assert context is not None assert activities is not None assert next_handler is not None await next_handler async def delete_activity_handler(context, reference, next_handler): assert context is not None assert reference is not None assert next_handler is not None await next_handler async def update_activity_handler(context, activity, next_handler): assert context is not None assert activity is not None assert next_handler is not None await next_handler old_context.on_send_activities(send_activities_handler) old_context.on_delete_activity(delete_activity_handler) old_context.on_update_activity(update_activity_handler) adapter = SimpleAdapter() new_context = TurnContext(adapter, ACTIVITY) assert not new_context._on_send_activities # pylint: disable=protected-access assert not new_context._on_update_activity # pylint: disable=protected-access assert not new_context._on_delete_activity # pylint: disable=protected-access old_context.copy_to(new_context) assert new_context.adapter == old_adapter assert new_context.activity == old_activity assert new_context.responded is True assert ( len(new_context._on_send_activities) == 1 ) # pylint: disable=protected-access assert ( len(new_context._on_update_activity) == 1 ) # pylint: disable=protected-access assert ( len(new_context._on_delete_activity) == 1 ) # pylint: disable=protected-access
async def test_should_call_send_on_activities_handler_before_send(self): context = TurnContext(SimpleAdapter(), ACTIVITY) called = False async def send_handler(context, activities, next_handler_coroutine): nonlocal called called = True assert activities is not None assert context is not None assert not activities[0].id await next_handler_coroutine() context.on_send_activities(send_handler) await context.send_activity(ACTIVITY) assert called is True
async def on_turn( self, context: TurnContext, logic: Callable[[TurnContext], Awaitable] ): # Note: skill responses will show as ContinueConversation events; we don't log those. # We only log incoming messages from users. if ( context.activity.type != ActivityTypes.event and context.activity.name != "ContinueConversation" ): message = f"{self._label} {context.activity.type} {context.activity.text}" print(message) # Register outgoing handler. context.on_send_activities(self._outgoing_handler) # Continue processing messages. await logic()
async def on_turn(self, context: TurnContext, logic: Callable[[TurnContext], Awaitable]): """ Processes an incoming activity. :param context: :param logic: :return: """ translate = await self._should_translate(context) if translate and context.activity.type == ActivityTypes.message: context.activity.text = await self.translator.translate( context.activity.text, TranslationSettings.default_language.value) async def aux_on_send(ctx: TurnContext, activities: List[Activity], next_send: Callable): user_language = await self.language_preference_accessor.get( ctx, TranslationSettings.default_language.value) should_translate = (user_language != TranslationSettings.default_language.value) # Translate messages sent to the user to user language if should_translate: for activity in activities: await self._translate_message_activity( activity, user_language) return await next_send() async def aux_on_update(ctx: TurnContext, activity: Activity, next_update: Callable): user_language = await self.language_preference_accessor.get( ctx, TranslationSettings.default_language.value) should_translate = (user_language != TranslationSettings.default_language.value) # Translate messages sent to the user to user language if should_translate and activity.type == ActivityTypes.message: await self._translate_message_activity(activity, user_language) return await next_update() context.on_send_activities(aux_on_send) context.on_update_activity(aux_on_update) await logic()
def test_copy_to_should_copy_all_references(self): old_adapter = SimpleAdapter() old_activity = Activity(id='2', type='message', text='test copy') old_context = TurnContext(old_adapter, old_activity) old_context.responded = True async def send_activities_handler(context, activities, next_handler): assert context is not None assert activities is not None assert next_handler is not None await next_handler async def delete_activity_handler(context, reference, next_handler): assert context is not None assert reference is not None assert next_handler is not None await next_handler async def update_activity_handler(context, activity, next_handler): assert context is not None assert activity is not None assert next_handler is not None await next_handler old_context.on_send_activities(send_activities_handler) old_context.on_delete_activity(delete_activity_handler) old_context.on_update_activity(update_activity_handler) adapter = SimpleAdapter() new_context = TurnContext(adapter, ACTIVITY) assert len(new_context._on_send_activities) == 0 assert len(new_context._on_update_activity) == 0 assert len(new_context._on_delete_activity) == 0 old_context.copy_to(new_context) assert new_context.adapter == old_adapter assert new_context.activity == old_activity assert new_context.responded is True assert len(new_context._on_send_activities) == 1 assert len(new_context._on_update_activity) == 1 assert len(new_context._on_delete_activity) == 1
async def logic(context: TurnContext): if test_case != FlowTestCase.root_bot_only: claims_identity = ClaimsIdentity( { AuthenticationConstants.VERSION_CLAIM: "2.0", AuthenticationConstants.AUDIENCE_CLAIM: self.skill_bot_id, AuthenticationConstants.AUTHORIZED_PARTY: self.parent_bot_id, }, True, ) context.turn_state[ BotAdapter.BOT_IDENTITY_KEY] = claims_identity if test_case == FlowTestCase.root_bot_consuming_skill: context.turn_state[ SkillHandler. SKILL_CONVERSATION_REFERENCE_KEY] = SkillConversationReference( None, AuthenticationConstants. TO_CHANNEL_FROM_BOT_OAUTH_SCOPE) if test_case == FlowTestCase.middle_skill: context.turn_state[ SkillHandler. SKILL_CONVERSATION_REFERENCE_KEY] = SkillConversationReference( None, self.parent_bot_id) async def capture_eoc(inner_context: TurnContext, activities: List[Activity], next): # pylint: disable=unused-argument for activity in activities: if activity.type == ActivityTypes.end_of_conversation: self.eoc_sent = activity break return await next() context.on_send_activities(capture_eoc) await DialogExtensions.run_dialog( dialog, context, convo_state.create_property("DialogState"))
async def test_should_send_a_trace_activity(self): context = TurnContext(SimpleAdapter(), ACTIVITY) called = False # pylint: disable=unused-argument async def aux_func(ctx: TurnContext, activities: List[Activity], next: Callable): nonlocal called called = True assert isinstance(activities, list), "activities not array." assert len(activities) == 1, "invalid count of activities." assert activities[0].type == ActivityTypes.trace, "type wrong." assert activities[0].name == "name-text", "name wrong." assert activities[0].value == "value-text", "value worng." assert activities[ 0].value_type == "valueType-text", "valeuType wrong." assert activities[0].label == "label-text", "label wrong." return [] context.on_send_activities(aux_func) await context.send_trace_activity("name-text", "value-text", "valueType-text", "label-text") assert called
async def on_turn( self, turn_context: TurnContext, logic: Callable[[TurnContext], Awaitable] ): if turn_context.activity.type == ActivityTypes.message: turn_context.activity.text = await self.translator.translate( turn_context.activity.text, TranslatorSettings.bot_language.value ) async def aux_on_send( turn_context: TurnContext, activities: List[Activity], next_send: Callable ): user_language = await self.language_preference_accessor.get( turn_context, TranslatorSettings.user_language.value ) for activity in activities: await self._translate_message_activity(activity, user_language) return await next_send() async def aux_on_update( turn_context: TurnContext, activity: Activity, next_update: Callable ): user_language = await self.language_preference_accessor.get( turn_context, TranslatorSettings.user_language.value ) if activity.type == ActivityTypes.message: await self._translate_message_activity(activity, user_language) return await next_update() turn_context.on_send_activities(aux_on_send) turn_context.on_update_activity(aux_on_update) await logic()
async def on_turn(self, context: TurnContext, logic: Callable[[TurnContext], Awaitable]): context.turn_state[self._stopwatch_state_key] = self._time_func() await self._log_incoming_activity(context, context.activity) context.on_send_activities(self._send_activities_handler) await logic()