Beispiel #1
0
def message_send(token, channel_id, message):
    '''
    adds message to a channels list of messages
    expects parameter types:
        token: str
        channel_id: int
        message: str
    returns dictionary
    '''
    user_id = check_token(token)

    if len(message) > 1000 or len(message) < 1:
        raise InputError(
            description=
            'Your message should be less than 1000 characters and at least 1 character'
        )
    if not is_valid_channel(channel_id):
        raise InputError
    if not is_user_a_member(channel_id, user_id):
        raise AccessError
    message_id = get_num_messages()
    glob_channels = get_channels()
    channel = glob_channels[channel_id]
    time_created = get_current_timestamp()

    if message.startswith('/hangman'):
        start_hangman(channel_id, user_id, time_created, message_id)
    elif message.startswith('/guess'):
        guess(message, channel_id, user_id, time_created, message_id)
    else:
        channel['messages'].insert(
            0, create_message(user_id, message_id, time_created, message))
    set_num_messages(message_id + 1)
    return {'message_id': message_id}
Beispiel #2
0
def message_sendlater(token, channel_id, message, time_sent):
    '''
    sends a message at a given time_sent, where time_sent is a unix timestamp
    greater than the current time.
    '''
    u_id = check_token(token)
    if not is_valid_channel(channel_id):
        raise InputError(description="No channel exists with that ID")
    if not is_user_a_member(channel_id, u_id):
        raise AccessError(description='You are not a member of this channel')
    if len(message) > 1000 or len(message) < 1:
        raise InputError(
            description=
            'Your message should be less than 1000 characters and at least 1 character'
        )
    curr_time = get_current_timestamp()
    if curr_time >= time_sent:
        raise InputError(description="You can not send a message back in time")
    delay = time_sent - curr_time
    message_id = get_num_messages()
    set_num_messages(message_id + 1)
    message_template = create_message(u_id, message_id, time_sent, message)
    timer = Timer(delay, sendlater_end, args=[channel_id, message_template])
    timer.start()
    return {'message_id': message_id}
Beispiel #3
0
def get_message_id():
    '''
    uses get_num_messages, set_num_messages to get a new message id,
    then increments the global message id count.
    '''
    message_id = get_num_messages()
    set_num_messages(message_id + 1)
    return message_id
Beispiel #4
0
def backup_data():
    '''Pickles slackr data with a timestamp.'''
    slackr_data = {
        'timestamp': get_current_timestamp(),
        'global_users': get_users(),
        'global_channels': get_channels(),
        'global_num_messages': get_num_messages()
    }
    with open('slackr_data.p', 'wb') as FILE:  #pylint: disable=invalid-name
        dump(slackr_data, FILE)