Пример #1
0
def cmd_save_text(message):
    if len(message.text) > 1000:
        bot.send_message(message.chat.id, config.lang.s_error_note_too_long)
        return None
    global offset_storage

    # Check, if message starts with bot mention. If yes -> remove it
    if message.text.startswith(r'@'):
        message.text = ' '.join(message.text.split()[1:])

    # Convert user's time to server's local time to set "at" command taking offset into account
    time_to_set = utils.convert_user_time_to_at_command(utils.get_time_storage().get(str(message.chat.id)), offset_storage.get(key=str(message.chat.id)))
    logger.debug('User {0!s} is going to set time: {1!s}'.format(
        str(message.from_user.username) + ' (' + str(message.chat.id) + ')', time_to_set))
    # Get Unixtime to set to SQLite DB
    unixtime_to_save_to_db = utils.convert_user_time_to_local_timestamp(utils.get_time_storage().get(str(message.chat.id)), offset_storage.get(str(message.chat.id)))
    # Set "at" command and recieve Job ID from it
    job_id = systemtools.set_new_at_job(message.chat.id, time_to_set, message.text.replace('"', r'\"'))
    # Probably this is not the best choice, because some errors can have "job" word in them
    # If not job id provided (error happened or something else)
    if not job_id:
        bot.send_message(message.chat.id, config.lang.s_error_could_not_save_note)
        return None
    logger.info('Successfully set reminder #{0!s} at {1!s}'.format(job_id, time_to_set))
    # Insert new row in table
    mydb = SQLighter(config.database_schedules_file)
    mydb.insert(message.chat.id, unixtime_to_save_to_db, job_id)
    mydb.close()
    bot.send_message(message.chat.id, config.lang.s_common_note_added.format(
        utils.get_time_storage().get(str(message.chat.id))))
    # After setting note, reset to START
    set_new_state(message.chat.id, StateMachine.States.STATE_START)
Пример #2
0
def cmd_set_time(message):
    global time
    # Check if timezone already set
    # I don't remember this if-clause to fire
    if not offset_storage.exists(str(message.chat.id)):
        'No offset storage'
        logger.warning('Whoa! It looks like {0!s} hasn\'t set offset yet! What a shame!'.format(
            str(message.from_user.username) + ' (' + str(message.chat.id) + ')'))
        bot.send_message(message.chat.id, config.lang.s_error_timezone_not_set)
        set_new_state(message.chat.id, StateMachine.States.STATE_SETTING_TIMEZONE_FOR_ALARM)
        return None
    timezone = offset_storage.get(str(message.chat.id))
    time = None
    global error_msg
    error_msg = None
    try:
        time = utils.parse_time(message.text, int(timezone))
    except utils.PastDateError as ex:
        error_msg = str(ex)
    except utils.ParseError as ex:
        error_msg = str(ex)
    else:
        pass
    # If there was an error getting time
    if time is None:
        logger.warning(
            'User {0!s} set incorrect time: {1!s}'.format(
                str(message.from_user.username) + ' (' + str(message.chat.id) + ')', message.text))
        if error_msg is None:
            # "Could not recognize timezone. Please try again"
            bot.send_message(message.chat.id, config.lang.s_error_time_not_recognized)
        else:
            bot.send_message(message.chat.id, error_msg)
        set_new_state(message.chat.id, StateMachine.States.STATE_SETTING_TIME)
    else:
        logger.debug('User {0!s} set time: {1!s}'.format(
            str(message.from_user.username) + ' (' + str(message.chat.id) + ')', time))
        utils.get_time_storage().save(str(message.chat.id), time, force_save=True)
        set_new_state(message.chat.id, StateMachine.States.STATE_SETTING_TEXT)
        bot.send_message(message.chat.id, config.lang.s_common_is_time_correct.format(time))
        pass