Exemple #1
0
def callback_offline_remove_minutes(bot: Bot, update: Update):
    chat: Chat = update.effective_chat
    msg: Message = update.effective_message

    hours_count = get_hours_db(update)
    decrement = re.findall('\d+', msg.text)

    if decrement[0].isdigit():
        hours_count -= int(decrement[0]) * 60
        # Prevent negatives values
        if hours_count < 0:
            hours_count = 0

        save_hours_db(update, hours_count)

        bot.send_message(text='✅ Seus minutos foram atualizados!',
                         chat_id=chat.id,
                         reply_to_message_id=msg.message_id,
                         reply_markup=reply_main_keyboard)
    else:
        # Error caught
        bot.send_message(text='😓 Desculpe, algo estranho aconteceu. \n'
                         'Não foi possível editar seus minutos. '
                         'Tente novamente ou escreva /ajuda',
                         chat_id=chat.id,
                         reply_to_message_id=msg.message_id,
                         reply_markup=reply_main_keyboard)

        raise TypeError('Invalid data type in regex', decrement[0])
Exemple #2
0
def callback_offline_add_hours(bot: Bot, update: Update):
    chat: Chat = update.effective_chat
    msg: Message = update.effective_message

    hours_count = get_hours_db(update)
    increment = re.findall('\d+', msg.text)

    if len(increment) > 1:
        # Input has hours and minutes
        hours_count += int(increment[0]) * 3600 + int(increment[1]) * 60
    else:
        # Input only has hours
        if increment[0].isdigit():
            hours_count += int(increment[0]) * 3600
        else:
            # Error caught
            bot.send_message(text='😓 Desculpe, algo estranho aconteceu. \n'
                             'Não foi possível adicionar suas horas. '
                             'Tente novamente ou escreva /ajuda',
                             chat_id=chat.id,
                             reply_to_message_id=msg.message_id,
                             reply_markup=reply_main_keyboard)

            raise TypeError('Invalid data type in regex', increment[0])

    save_hours_db(update, hours_count)
    bot.send_message(text='✅ Suas horas foram adicionadas!',
                     chat_id=chat.id,
                     reply_to_message_id=msg.message_id,
                     reply_markup=reply_main_keyboard)
Exemple #3
0
def hours_callback(bot: Bot, update: Update, user_data: dict = None):
    query: CallbackQuery = update.callback_query
    hours_count = get_hours_db(update)

    if query.data == CALLBACK_HOURS_ADD:

        bot.edit_message_text(text='🕓 *Horas*\n\n'
                              'Total de Horas: ' +
                              seconds_to_hours(hours_count),
                              chat_id=query.message.chat_id,
                              message_id=query.message.message_id,
                              reply_markup=add_remove_hours_keyboard,
                              parse_mode='Markdown')

    elif query.data == CALLBACK_HOURS_MINUTES_ADD:

        bot.edit_message_text(text='🕓 *Horas*\n\n'
                              'Total de Horas: ' +
                              seconds_to_hours(hours_count),
                              chat_id=query.message.chat_id,
                              message_id=query.message.message_id,
                              reply_markup=add_remove_minutes_keyboard,
                              parse_mode='Markdown')

    # Handle Hours accordingly
    elif query.data == CALLBACK_HOURS_ADD_ONE_HOUR:
        hours_count += ONE_HOUR_IN_SECS

        bot.edit_message_text(text='🕓 *Horas*\n\n'
                              'Total de Horas: ' +
                              seconds_to_hours(hours_count),
                              chat_id=query.message.chat_id,
                              message_id=query.message.message_id,
                              reply_markup=add_remove_hours_keyboard,
                              parse_mode='Markdown')

    elif query.data == CALLBACK_HOURS_REMOVE_ONE_HOUR:
        hours_count -= ONE_HOUR_IN_SECS

        bot.edit_message_text(text='🕓 *Horas*\n\n'
                              'Total de Horas: ' +
                              seconds_to_hours(hours_count),
                              chat_id=query.message.chat_id,
                              message_id=query.message.message_id,
                              reply_markup=add_remove_hours_keyboard,
                              parse_mode='Markdown')

    elif query.data == CALLBACK_HOURS_ADD_TWO_HOURS:
        hours_count += TWO_HOURS_IN_SECS

        bot.edit_message_text(text='🕓 *Horas*\n\n'
                              'Total de Horas: ' +
                              seconds_to_hours(hours_count),
                              chat_id=query.message.chat_id,
                              message_id=query.message.message_id,
                              reply_markup=add_remove_hours_keyboard,
                              parse_mode='Markdown')

    elif query.data == CALLBACK_HOURS_REMOVE_TWO_HOURS:
        hours_count -= TWO_HOURS_IN_SECS

        bot.edit_message_text(text='🕓 *Horas*\n\n'
                              'Total de Horas: ' +
                              seconds_to_hours(hours_count),
                              chat_id=query.message.chat_id,
                              message_id=query.message.message_id,
                              reply_markup=add_remove_hours_keyboard,
                              parse_mode='Markdown')

    # Handle Minutes accordingly
    elif query.data == CALLBACK_HOURS_ADD_THIRTY_MINUTES:
        hours_count += THIRTY_MIN_IN_SECS

        bot.edit_message_text(text='🕓 *Horas*\n\n'
                              'Total de Horas: ' +
                              seconds_to_hours(hours_count),
                              chat_id=query.message.chat_id,
                              message_id=query.message.message_id,
                              reply_markup=add_remove_minutes_keyboard,
                              parse_mode='Markdown')

    elif query.data == CALLBACK_HOURS_REMOVE_THIRTY_MINUTES:
        hours_count -= THIRTY_MIN_IN_SECS

        bot.edit_message_text(text='🕓 *Horas*\n\n'
                              'Total de Horas: ' +
                              seconds_to_hours(hours_count),
                              chat_id=query.message.chat_id,
                              message_id=query.message.message_id,
                              reply_markup=add_remove_minutes_keyboard,
                              parse_mode='Markdown')

    elif query.data == CALLBACK_HOURS_ADD_TEN_MINUTES:
        hours_count += TEN_MIN_IN_SECS

        bot.edit_message_text(text='🕓 *Horas*\n\n'
                              'Total de Horas: ' +
                              seconds_to_hours(hours_count),
                              chat_id=query.message.chat_id,
                              message_id=query.message.message_id,
                              reply_markup=add_remove_minutes_keyboard,
                              parse_mode='Markdown')

    elif query.data == CALLBACK_HOURS_REMOVE_TEN_MINUTES:
        hours_count -= TEN_MIN_IN_SECS

        bot.edit_message_text(text='🕓 *Horas*\n\n'
                              'Total de Horas: ' +
                              seconds_to_hours(hours_count),
                              chat_id=query.message.chat_id,
                              message_id=query.message.message_id,
                              reply_markup=add_remove_minutes_keyboard,
                              parse_mode='Markdown')

    elif query.data == CALLBACK_HOURS_ADD_FIVE_MINUTES:
        hours_count += FIVE_MIN_IN_SECS

        bot.edit_message_text(text='🕓 *Horas*\n\n'
                              'Total de Horas: ' +
                              seconds_to_hours(hours_count),
                              chat_id=query.message.chat_id,
                              message_id=query.message.message_id,
                              reply_markup=add_remove_minutes_keyboard,
                              parse_mode='Markdown')

    elif query.data == CALLBACK_HOURS_REMOVE_FIVE_MINUTES:
        hours_count -= FIVE_MIN_IN_SECS

        bot.edit_message_text(text='🕓 *Horas*\n\n'
                              'Total de Horas: ' +
                              seconds_to_hours(hours_count),
                              chat_id=query.message.chat_id,
                              message_id=query.message.message_id,
                              reply_markup=add_remove_minutes_keyboard,
                              parse_mode='Markdown')

    # Prevent negatives values
    if hours_count < 0:
        hours_count = 0

    save_hours_db(update, hours_count)

    if user_data is not None:
        if user_data.get(START_CTIMER) is not None:
            return query.answer(text='⚠  Seu cronômetro está ativo ',
                                show_alert=False)

    # Cronometer ins't active, just go ahead.
    query.answer()