def logic(interaction, message): user_id = message['user_id'] text = message['text'] users_list = list(user.users.find({'type': 'agent'})) #message['user_id'] if len(users_list) == 0: chat_api.reply("No hay agentes dados de alta", message) return True agent_uuid = text.split(' ')[0].lower() agent_data_uuid = user.find(uuid = agent_uuid) if len(agent_data_uuid) == 0: r_text = 'No se encontro ningún agente con dicho identificador' chat_api.reply(r_text, message) return True agent_data = agent_data_uuid[0] user_context = conversation.context(user_id) redirect_user_id = user_context.get('redirect_user') if redirect_user_id is None: r_text = 'No tienes ninguna conversación seleccionada' chat_api.reply(r_text, message) return True user_data = user.get(redirect_user_id) current_p_results = pending_conversations.find(user_id = redirect_user_id, closed = False) current_p = current_p_results[0].get('id') if len(current_p_results)>0 else None if current_p is None: pending_conversations.create(redirect_user_id, [agent_uuid]) current_p_results = pending_conversations.find(user_id = redirect_user_id, closed = False) current_p = current_p_results[0].get('id') if len(current_p_results)>0 else None else: pending_conversations.switch_owner(current_p, user_id, agent_uuid) conversation.update_context(user_id, 'redirect_user', None) conversation.update_context(user_id, 'redirect_name', None) conversation.update_context(user_id, 'redirect_phone', None) conversation.update_context(user_id, 'conversational_level', 'user') conversation.update_context(user_id, 'current_pending_conversation', None) r_text = f"{agent_uuid} ha recibido al usuario {user_data['uuid']}" chat_api.reply(r_text, message) a_text = f"Has recibido al usuario {user_data['uuid']} de {user_id}" chat_api.reply(a_text, {'user_id': agent_uuid, 'text': a_text}) return True
def logic(interaction, message): user_id = message['user_id'] # We can also use the command number detected before redirect_user_number = message['text'].split(' ')[0].lower() users_uuid = user.find(uuid=redirect_user_number) try: user_found = users_uuid[0] except: user_found = None if user_found is not None: conversation.update_context(user_id, 'redirect_user', user_found['id']) conversation.update_context(user_id, 'redirect_name', user_found['name']) conversation.update_context(user_id, 'redirect_phone', user_found['phone']) conversation.update_context(user_id, 'conversational_level', 'user') if user_found.get('owner') is None: user.update(user_found['id'], {'owner': user_id}) current_p_results = pending_conversations.find( user_id=user_found['id'], closed=False) current_p = current_p_results[0].get( 'id') if len(current_p_results) > 0 else None if current_p is None: pending_conversations.create(user_found['id'], owners=[user_id]) current_p_results = pending_conversations.find( user_id=user_found['id'], closed=False) current_p = current_p_results[0].get( 'id') if len(current_p_results) > 0 else None conversation.update_context(user_id, 'current_pending_conversation', current_p) pending_conversations.add_owner(current_p, user_id) pending_conversations.remove_new_messages(current_p) s_msg = "Haz selecionado al " + user_found['uuid'] + " " + user_found[ 'name'] chat_api.reply(s_msg, message) s_msg = "Con la conversacion pendiente " + str(current_p) chat_api.reply(s_msg, message) else: list_users.logic() return True
def logic(interaction, message): user_id = message['user_id'] user_context = conversation.context(user_id) last_reply_time = user_context.get('last_reply_time') is_closing = user_context.get('is_closing') pending_convos = pending_conversations.find(user_id=user_id, closed=False) current_p_convo_id = pending_convos[0].get( 'id') if len(pending_convos) > 0 else None if current_p_convo_id is not None and is_closing == True: text = message.get('text').lower().split(' ')[0] current_p_data = pending_conversations.get(current_p_convo_id) current_user = user.get(user_id) user_name = current_user.get('name') if 'no' in text: for owner in current_p_data['owners']: agent_context = conversation.context(owner) message = {'user_id': owner} c_msg = f'El usuario {user_name} no ha confirmado el cierre de su conversación' chat_api.reply(c_msg, message) elif 'si' in text: pending_conversations.close(current_p_convo_id) for owner in current_p_data['owners']: agent_context = conversation.context(owner) redirect_user = agent_context.get('redirect_user') agent_p_convo = agent_context.get( 'current_pending_conversation') message = {'user_id': owner} c_msg = f'La conversación pendiente con {user_name}({current_p_convo_id}) se ha cerrado' chat_api.reply(c_msg, message) if redirect_user == user_id and current_p_convo_id == agent_p_convo: conversation.update_context(owner, 'redirect_user', None) conversation.update_context(owner, 'redirect_name', None) conversation.update_context(owner, 'redirect_phone', None) conversation.update_context(owner, 'conversational_level', 'user') conversation.update_context( owner, 'current_pending_conversation', None) conversation.update_context(user_id, 'is_closing', False) return True if last_reply_time is None: conversation.update_context(user_id, 'last_reply_time', datetime.now()) chat_api.reply(interaction['text'], message) else: if datetime.now() > last_reply_time + timedelta(hours=24): conversation.update_context(user_id, 'last_reply_time', datetime.now()) chat_api.reply(interaction['text'], message) else: None # Do nothing if 24 before last reply did not happen return True
def set_pending_conversation(message, user_id): u_p_conversations = pending_conversations.find(user_id, closed=False) if u_p_conversations is None or len(u_p_conversations) == 0: pending_conversation = pending_conversations.create(user_id) else: pending_conversation = u_p_conversations[0] pending_conversations.received_new_messages(user_id) pending_conversations.alert_admins_pending(pending_conversation) return True
def logic(interaction, message): chat_api.reply("Estas son las conversaciones pendientes:", message) user_id = message['user_id'] pending_conversations.clean_pending_index() p_list = list(pending_conversations.find( owner=user_id, closed=False)) #message['user_id'] if len(p_list) == 0: chat_api.reply("No hay casos pendientes a resolver", message) return True for p in p_list: star_messages = '' if p['new_messages'] == True: star_messages = '* ' user_p = user.get(p['user_id']) #user_name = user_info['name'] #user_phone = user_info['phone'] text_message = f"👤{p['id']} {star_messages}= {user_p['name']}" chat_api.reply(text_message, message) return True
def logic(interaction, message): chat_api.reply("Selecciona un usuario@:", message) user_id = message['user_id'] user.clean_user_index() users_list = list(user.users.find({'type': 'user'})) #message['user_id'] if len(users_list) == 0: chat_api.reply("No hay usuari@s dados de alta", message) return True for user_info in users_list: if 'name' not in user_info: continue user_id = user_info['id'] user_uuid = user_info['uuid'] user_name = user_info['name'] user_phone = user_info['phone'] user_pending = pending_conversations.find(user_id = user_id, closed=False, new_messages=False) if len(user_pending)>0: string_pending = '* ' else: string_pending = '' text_message = f"👤{user_uuid} {string_pending}= {user_name} ({user_phone})" chat_api.reply(text_message, message) return True
def logic(interaction, message): chat_api.reply("Estos son l@s agentes disponibles:", message) user_id = message['user_id'] user.clean_agent_index() users_list = list(user.users.find({'type': 'agent'})) #message['user_id'] if len(users_list) == 0: chat_api.reply("No hay agentes dados de alta", message) return True #Double check if uuids are correctly setup for user_info in users_list: if 'name' not in user_info: continue user_id = user_info['id'] user_uuid = user_info['uuid'] user_name = user_info['name'] user_phone = user_info['phone'] user_pending = pending_conversations.find(user_id = user_id, closed=False, new_messages=False) if len(user_pending)>0: string_pending = '* ' else: string_pending = '' text_message = f"👤{user_uuid} {string_pending}= {user_name} ({user_phone})" chat_api.reply(text_message, message) return True