Ejemplo n.º 1
0
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
Ejemplo n.º 2
0
def logic(interaction, message):
    user_id = message.get('user_id')
    try:
        ls_type = message['text'].split(' ')[1]
    except:
        ls_type = None

    if ls_type == 'u':
        list_users.logic(interaction, message)
    elif ls_type == 'c':
        list_cases.logic(interaction, message)
    elif ls_type == 'a':
        list_agents.logic(interaction, message)
    elif ls_type == 'p':
        list_pending_conversations.logic(interaction, message)
    else:
        user_id = message['user_id']
        user_context = conversation.context(user_id)
        current_id = user_context.get('redirect_user')

        if current_id is None:
            s_msg = "No esta seleccionado ningún usuario"
            chat_api.reply(s_msg, message)
            return True

        convo = conversation.get_printable_conversation(current_id)
        s_msg = "Los mensajes con el usuario actual es:"
        chat_api.reply(s_msg, message)
        chat_api.reply(convo, message)

    return True
Ejemplo n.º 3
0
def logic(interaction, message):
    user_id = message['user_id']
    conversation.update_context(user_id, 'syntax_error_u_plus', False)
    user_context = conversation.context(user_id)
    new_user_info = message.get('text')

    new_user_info = new_user_info.replace('+u ', '')
    name_phone_split = new_user_info.split('(')

    # Flow, check syntax
    if '(' not in new_user_info:
        conversation.update_context(user_id, 'syntax_error_u_plus', True)
        return True

    name = name_phone_split[0]
    phone = name_phone_split[1]
    phone = user.clean_phone(phone)

    print(phone)

    country = get_country_name_and_flag(phone)

    conversation.update_context(user_id, 'new_user_info_name', name)
    conversation.update_context(user_id, 'new_user_info_phone', phone)
    conversation.update_context(user_id, 'new_user_info_country', country)

    return True
Ejemplo n.º 4
0
def logic(interaction, message):
    # We can also use the command number detected before
    user_id = message['user_id']
    user_data = user.get(user_id)
    agent_name = user_data.get('name')
    user_context = conversation.context(user_id)
    current_p_conversation_id = user_context.get(
        'current_pending_conversation')

    if current_p_conversation_id is None:
        list_pending_conversations.logic(interaction, message)
        return None

    p_convo_data = pending_conversations.get(current_p_conversation_id)
    client_id = p_convo_data.get('user_id')
    client_data = user.get(client_id)
    client_name = client_data.get('name')

    conversation.update_context(client_id, 'is_closing', True)

    s_msg = f"Se ha invitado confirmar al usuari@ {client_name}({current_p_conversation_id}) a confirmar el cierre de su caso."
    chat_api.reply(s_msg, message)

    message_client = {'user_id': client_id}
    c_msg = f"{agent_name} ha informado que ya tu caso ya no requiere seguimiento ¿Puedes confirmar? Responde si o no."
    chat_api.reply(c_msg, message_client)

    return True
Ejemplo n.º 5
0
def logic(interaction, message):
    user_id = message['user_id']
    user_context = context(message['user_id'])
    # TODO(ricalanis): Change to broadcast
    agent_phones = [key for key in user.agents().keys()]
    last_message = get_last_message(user_id)
    user_new_message = user.get(user_context['id'])

    if user_new_message.get('owner') is not None:
        message_redirect = f"Mensaje de {user_context['name']}({user_context['phone']}):{last_message}"
        message = {'user_id': user.get('owner')}
        chat_api.reply(message_redirect, message)
        return True

    for phone in agent_phones:
        agent_user_id = user.phone_to_id(phone)
        if user.get(agent_user_id) is None: continue
        message = {'user_id': user.phone_to_id(phone)}
        agent_name = user.agents()[phone]['name']

        text_alert = f"Acaba de llegar un mensaje de: {user_context['name']}({user_context['phone']})"
        chat_api.reply(text_alert, message)

        text_alert = f"*Ultimo mensaje: {last_message}*"
        chat_api.reply(text_alert, message)

        update_context(agent_user_id, 'last_message_user', user_context['id'])
        update_context(agent_user_id, 'last_message_name',
                       user_context['name'])
        update_context(agent_user_id, 'last_message_phone',
                       user_context['phone'])

        text_alert = f"Escribe 'p' para poner un timer de 5 min a esta conversación y entrar en la conversación con {user_context['name']}({user_context['phone']}). Por lo contrario sigue escribiendo."
        chat_api.reply(text_alert, message)
    return True
Ejemplo n.º 6
0
def get_next_interaction(interaction, message):
    user_id = message['user_id']
    context_variable = interaction['context_variable']
    if_value = interaction['if_value']
    context_value_current = conversation.context(user_id).get(context_variable)
    if context_value_current == if_value:
        return interaction['true_next_interaction']
    return interaction['false_next_interaction']
Ejemplo n.º 7
0
def remove_user_from_agent_redirect(user_id, agent_id):
    user_context = conversation.context(agent_id)
    redirect_user = user_context.get('redirect_user')
    if redirect_user == user_id:
        conversation.update_context(agent_id, 'redirect_user', None)
        conversation.update_context(agent_id, 'redirect_name', None)
        conversation.update_context(agent_id, 'redirect_phone', None)
        conversation.update_context(agent_id, 'conversational_level', 'user')
        conversation.update_context(agent_id, 'current_pending_conversation', None)
Ejemplo n.º 8
0
def get_next_interaction(interaction, message):
    user_id = message['user_id']
    user_context = conversation.context(user_id)
    no_redirect_user = user_context.get('no_redirect_user')
    if no_redirect_user == True:
      next_interaction = interaction['no_redirect_user']
      conversation.update_context(user_id, 'no_redirect_user', False)
    else:
      next_interaction = interaction['next_interaction']
    return next_interaction
Ejemplo n.º 9
0
def get_next_interaction(interaction, message):
    user_id = message['user_id']
    user_context = conversation.context(user_id)
    did_not_follow_syntax = user_context.get('syntax_error_u_plus')
    if did_not_follow_syntax == True:
        next_interaction = interaction['syntax_error']
        conversation.update_context(user_id, 'syntax_error_u_plus', False)
    else:
        next_interaction = interaction['next_interaction']
    return next_interaction
Ejemplo n.º 10
0
def logic(interaction, message):
    context = conversation.context(message['user_id'])
    interaction_text= interaction['text']
    for key in context:
        key_store = '${' + key + '}'
        if key_store in interaction_text:
            interaction_text = interaction_text.replace(key_store, context[key])

    chat_api.reply(interaction_text, message)
    return True
Ejemplo n.º 11
0
def move_conversation(message):
    # Past interaction actions
    user_id = message.get('user_id')

    # Get where this user was left
    if conversation.is_finished(user_id): return True
    print(user_id)
    last_message = conversation.find_last_message(user_id)
    # This fixes referencing a conversation with a user we dont have, but good to see why this would happen
    #if last_message is None:
    #    conversation.create(message)
    last_interaction_name = last_message.get('interaction_name')
    print(last_interaction_name)
    last_interaction = dialog.get_interaction(last_interaction_name, user_id)

    # Save context if needed
    if 'save_answer_context' in last_interaction:
        save_answer_context(last_interaction, message, user_id)

    if last_interaction.get('save_intent') == 'true':
        utterance_intent = luis_ai.get_label(message['text'])
        conversation.update_context(user_id, 'intent', utterance_intent)

    if attend_new_message(message) == True and user.get_user_type(
            user_id) == 'agent':
        next_interaction_name = 'attend_new_message'
    elif 'card' in message and user.get_user_type(user_id) == 'agent':
        set_new_user_pre_register(user_id, message)
        next_interaction_name = 'register_user'
    else:
        next_interaction_name = interaction.get_next_interaction_name(
            last_interaction, message)
    print(next_interaction_name)

    next_interaction = dialog.get_interaction(next_interaction_name, user_id)
    interaction.run_interaction(next_interaction, message)
    conversation.update(next_interaction, next_interaction_name, message)

    if next_interaction.get('requires_user_response') == 'false':
        if attend_new_message(message) != 'p':
            message['text'] = 'done'
        if 'card' in message: del message['card']
        move_conversation(message)
    if next_interaction.get('finishes_conversation') == 'true':
        conversation.set_finished(user_id)
    if 'save_field_context' in next_interaction:
        save_field_context(next_interaction, message, user_id)
    if next_interaction.get('create_thread') == 'true':
        thread_label = conversation.context(user_id).get('intent')
        first_message_id = conversation.get_canonical_user_message(user_id, 0)
        last_message_id = conversation.get_canonical_user_message(user_id, -1)
        thread.create(user_id, first_message_id, None, None, 'arbi')
    return True
Ejemplo n.º 12
0
def logic(interaction, message):
    user_id = message['user_id']
    user_context = conversation.context(user_id)
    redirect_user = user_context.get('redirect_user')
    conversation.update_context(user_id, 'no_redirect_user', False)

    if redirect_user is None:
        conversation.update_context(user_id, 'no_redirect_user', True)
        list_users.logic(interaction, message)
        return True

    list_agents.logic(interaction, message)
    return True
def logic(interaction, message):
    # We can also use the command number detected before
    user_id = message['user_id']
    current_p = message['text'].split(' ')[0].lower()

    current_p_object = pending_conversations.get(current_p)

    if current_p_object is None:
        list_pending_conversations.logic(interaction, message)
        return True

    redirect_user_number = current_p_object['user_id']
    user_found = user.get(redirect_user_number)

    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})

        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 " + current_p
        chat_api.reply(s_msg, message)

    else:
        already_selected_p = conversation.context(
            'current_pending_conversation')
        if already_selected_p is None:
            list_pending_conversations.logic(interaction, message)
        else:
            already_p_object = pending_conversations.get(already_selected_p)
            already_user = user.get(already_p_object['user_id'])
            already_user_name = already_user.get('name')
            s_msg = "Sigues hablando con " + already_user_name
            chat_api.reply(s_msg, message)

    return True
Ejemplo n.º 14
0
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
Ejemplo n.º 15
0
def logic(interaction, message):

    user_id = message.get('user_id')
    user_context = conversation.context(user_id)
    current_user_id = user_context.get('redirect_user')

    if current_user_id is not None:
      current_user_name = user_context['redirect_name']
      current_user_phone = user_context['redirect_phone']
      notification.create(user_id, current_user_id, 0, settings = {'minutes': 5})
      reply_text = 'Acabas de poner un timer de 5 min con en tu (conversación) con {current_user_name} ({current_user_phone})'
      chat_api.reply(reply_text, message)

    new_message_user = user_context.get('last_message_user')
    user_new_message = user.get(new_message_user)

    if new_message_user is None:
      chat_api.reply('Falle en encontrar el usuario para hablar', message)
      return True

    new_message_name = user_context['last_message_name']
    new_message_phone = user_context['last_message_phone']

    conversation.update_context(user_id, 'redirect_user', new_message_user)
    conversation.update_context(user_id, 'redirect_name', new_message_name)
    conversation.update_context(user_id, 'redirect_phone', new_message_phone)
    conversation.update_context(user_id, 'conversational_level', 'user')

    # Code shared with switch
    if user_found.get('owner') is None:
        user.update(redirect_user_id, { 'owner': user_id } )

    chat_api.reply('La conversación con este usuario es:', message)
    user_messages = conversation.get_printable_conversation(redirect_user_id)
    chat_api.reply(user_messages, message)
    return True
Ejemplo n.º 16
0
def get_next_interaction(interaction, message):
    user_id = message['user_id']
    conv_level = conversation.context(user_id).get('conversational_level')
    if conv_level == 'user': return interaction['user_interaction']
    return interaction['general_interaction']
Ejemplo n.º 17
0
def logic(interaction, message):
    user_id = message['user_id']
    user_context = conversation.context(user_id)
    name = user_context.get('new_user_info_name')
    phone = user_context.get('new_user_info_phone')
    country = user_context.get('new_user_info_country')
    conversation.update_context(user_id, 'new_user_info_name', None)
    conversation.update_context(user_id, 'new_user_info_phone', None)
    conversation.update_context(user_id, 'new_user_info_country', None)

    id_create_user = user.phone_to_id(phone)

    users_by_name = user.find(name=name)
    user_same_name = users_by_name[0] if len(users_by_name) > 0 else None

    users_by_phone = user.find(phone=phone)
    user_same_phone = users_by_phone[0] if len(users_by_phone) > 0 else None

    no_change = False
    if user.get(
            id_create_user
    ) is None and user_same_phone is None and user_same_name is None:

        user_data = {
            'name': name,
            'phone': phone,
            'country': country,
            'owner': user_id,
        }

        user.create(id_create_user, user_data, 'outbound')

        message_start = {
            'user_id': id_create_user,
            'text': '***Inicio Conversacion Outbound'
        }
        pending_conversations.create(id_create_user, [user_id])
        conversation.update_context(id_create_user, 'last_reply',
                                    datetime.now())

        user_register = f"Haz registrado a un nuevo usuario. {name} ({phone})"
        chat_api.reply(user_register, message)

    elif user_same_phone is not None:
        if user_same_phone['name'] != name:
            user.update(user_same_phone['id'], {'name': name})
            name_change = f"Haz cambiado el nombre del usuario con nombre de {name}"
            chat_api.reply(name_change, message)
        else:
            no_change = True

    elif user_same_name is not None:
        if user_same_name['phone'] != phone:
            user.update(user_same_name['id'], {'phone': phone})
            phone_change = f"Haz cambiado el telefono del usuario con nombre de {name}"
            chat_api.reply(phone_change, message)
        else:
            no_change = True

    if no_change == True:
        chat_api.reply('El usuario no sufrio cambios', message)

    return True
Ejemplo n.º 18
0
def logic(interaction, message):
    user_id = message.get('user_id')
    context = conversation.context(user_id)
    sheets.insert_row(context)
    return True