Ejemplo n.º 1
0
def build_message(user_id, text, body=None):
    message = {
        'created_at': datetime.now(),
        'user_id': user_id,
        'text': text,
    }
    if body and 'BEGIN:VCARD' in body:
        print('******* Card Found, printing contents: *********')
        print(body)
        print('*******')
        card_info = body
        card_separated = card_info.split('\n')
        raw_phone = None
        if 'waid' in card_info:
            raw_phone = card_info.split('waid=')[1].split(':')[0]
        elif '+' in card_info:
            raw_phone = card_info.split('+')[1].split('\n')[0]
        else:
            raw_phone = card_separated[3].split(':')[-1]
        user_phone = user.clean_phone(raw_phone)
        message['card'] = {
            'name': card_separated[2].replace('FN:', ''),
            'phone': user_phone,
            'country': geo.get_country_name_and_flag(user_phone)
        }
    return message
Ejemplo n.º 2
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.º 3
0
def update(user_id, user_data):
    # user_data = {field:value, field2:value2 ...z

    if 'phone' in user_data:
        user_data['country'] = country = get_country_name_and_flag(user_data['phone'])

    users.find_one_and_update(
        {"id": user_id},
        {"$set": user_data}
    )
    return True
Ejemplo n.º 4
0
def respond(data):
    print(data)
    text = data.get('body')
    user_id = data.get('author')
    body = data.get('body')
    print(data)
    if os.getenv('ARBITRUR_PHONE') in user_id: return True
    message = build_message(user_id, text, body)
    log_handler.insert_message(message)
    print(message)

    if message['text'] == 'KABOOM!':
        hard_reset(message)
        return True

    RECIEVER_ID = user.phone_to_id(os.getenv('ARBITRUR_PHONE'))

    new_user_phone = data.get('chatId').replace('@c.us', '')
    user_data = {
        'name': data.get('senderName'),
        'phone': new_user_phone,
        'country': geo.get_country_name_and_flag(new_user_phone)
    }
    if user.get(user_id) is None: user.create(user_id, user_data)
    # Demote or promote user if there is a change in the agent list
    user.demote_to_user_if_needed(user_id, user_data)
    user.promote_to_agent_if_needed(user_id, user_data)

    # If we are already handling a message and we are not done, ignore.
    conversation.update_canonical_conversation(user_id, RECIEVER_ID, text,
                                               'user')
    if user.is_bot_answering(user_id) == True: return True
    user.set_user_answering(user_id)
    print('Moving Convo')
    core.recieve_message(message)
    user.remove_user_answering(user_id)
    return True
Ejemplo n.º 5
0
def create(user_id, user_data = {}, user_source = 'inbound'):
    user_type = get_user_type(user_id)

    if user_type == 'agent':
        name, uuid, phone = fetch_agent(user_id)
    else:
        if len(user_data.keys())>0:
            name, uuid, phone = fetch_user_data(user_data)
        else:
            name, uuid, phone = fetch_user(user_id)

    if phone: country = get_country_name_and_flag(phone)
    user = {
        'id': user_id,
        'source': user_source,
        'type': user_type,
        'name': name,
        'uuid': uuid,
        'phone': phone,
        'country': country,
        'created_at': datetime.now(),
        'owner': None,
        'threads': [],
        'answering': False,
        'current_thread': None,
    }

    if 'owner' in user_data: user['owner'] = user_data.get('owner')
    users.insert_one(user)

    message = {
        'user_id': user_id,
        'message': 'initial_message',
        'created_at': datetime.now()
    }

    if user_source == 'inbound':
        message['text'] = 'Start message user'
        conversation.create(message)
    else:
        message['text'] = 'Start message bot'
        conversation.create(message,
                            user_type= 'bot',
                            message_type= 'bot_utterance',
                            interaction_name = 'finish_conversation')

    # Add pending conversation if the given user model is a client
    if user_type == 'user':
        if user_data.get('owner'):
            owners_pending_convo = [user_data.get('owner')]
        else:
            owners_pending_convo = []
        pending_conversations.create(user_id, owners = owners_pending_convo)

    if user_type == 'agent':
        for hour in [9, 17]:
            notification.create(
                user_id,
                notification_type = 'set_time',
                notification_nature = 'timed',
                settings = { 'hour': hour, 'minute': 0 }
            )
    clean_agent_index()
    clean_user_index()
    return True