async def _instantiate_dialog(self, user: User, peer: Union[User, Bot]): log.info(f'instantiating the dialog') conversation = Conversation(participant1=ConversationPeer(peer=user), participant2=ConversationPeer(peer=peer)) profiles = await run_sync_in_executor(PersonProfile.objects) profiles_count = await run_sync_in_executor(profiles.count) for p in conversation.participants: p.assigned_profile = profiles[random.randrange(profiles_count)] while True: conv_id = random.getrandbits(31) if conv_id not in self._active_dialogs and \ await run_sync_in_executor(lambda: Conversation.objects(conversation_id=conv_id).count()) == 0: break conversation.conversation_id = conv_id self._active_dialogs[conv_id] = conversation for p in conversation.participants: target_gateway = self._gateway_for_peer(p) await target_gateway.start_conversation(conv_id, p.peer, p.assigned_profile) self._reset_inactivity_timer(conv_id)
async def _instantiate_dialog(self, user: User, peer: Union[User, Bot]): log.info(f'instantiating the dialog') conversation = Conversation( participant1=ConversationPeer( peer=user, peer_conversation_guid=uuid4().__str__()), participant2=ConversationPeer( peer=peer, peer_conversation_guid=uuid4().__str__())) tags_set: QuerySet = Settings.objects(name='tags') active_tags = tags_set.first().value if tags_set.count() else [] if active_tags: profiles: QuerySet = await run_sync_in_executor( PersonProfile.objects(tags__in=active_tags)) if profiles.count() == 0: log.warning(f'Not found any profiles with tags: {active_tags}') profiles: QuerySet = await run_sync_in_executor( PersonProfile.objects) else: profiles: QuerySet = await run_sync_in_executor( PersonProfile.objects) first_profile = None linked_profile_uuid = None for p in conversation.participants: if first_profile is None: p.assigned_profile = first_profile = random.choice(profiles) linked_profile_uuid = first_profile.link_uuid else: # profiles assignment order: # other profile from the same linked group || profile with unmatching sentences || same profile second_profile = random.choice( profiles(id__ne=first_profile.id, link_uuid=linked_profile_uuid) or (profiles(persona__ne=first_profile.persona) or [first_profile])) p.assigned_profile = second_profile while True: conv_id = random.getrandbits(31) if conv_id not in self._active_dialogs and \ await run_sync_in_executor(lambda: Conversation.objects(conversation_id=conv_id).count()) == 0: break conversation.conversation_id = conv_id conversation.messages_to_switch_topic = self.dialog_options[ 'n_messages_to_switch_topic'] conversation.reset_topic_switch_counter() self._active_dialogs[conv_id] = conversation topics = [ 'Социальные сети', 'Фитнес', 'Уборка и чистота', 'Одиночество', 'Мода', 'Женская внешность', 'Мужская внешность', 'Деньги, богатство', 'Машины', 'Счастье' ] curr_topic = random.sample(topics, k=1) str_topic = curr_topic[0] msg = conversation.add_message(text='Switched to topic ' + str_topic, sender=peer, system=True) for p in conversation.participants: target_gateway = self._gateway_for_peer(p) p.assigned_profile.topics[0] = str_topic await target_gateway.start_conversation(conv_id, p.peer, p.assigned_profile, p.peer_conversation_guid) self._reset_inactivity_timer(conv_id)