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_on_update_activity_handler_before_update(self): context = TurnContext(SimpleAdapter(), ACTIVITY) called = False async def update_handler(context, activity, next_handler_coroutine): nonlocal called called = True assert activity is not None assert context is not None assert activity.id == "1234" await next_handler_coroutine() context.on_update_activity(update_handler) await context.update_activity(ACTIVITY) assert called is True
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()
async def test_update_activity_should_apply_conversation_reference(self): activity_id = "activity ID" context = TurnContext(SimpleAdapter(), ACTIVITY) called = False async def update_handler(context, activity, next_handler_coroutine): nonlocal called called = True assert context is not None assert activity.id == activity_id assert activity.conversation.id == ACTIVITY.conversation.id await next_handler_coroutine() context.on_update_activity(update_handler) new_activity = MessageFactory.text("test text") new_activity.id = activity_id update_result = await context.update_activity(new_activity) assert called is True assert update_result.id == activity_id
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 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()