Esempio n. 1
0
def count_callback_query(call):
    def _total_cart_sum(cart) -> int:
        summary_dishes_sum = [
            cart_item.dish.price * cart_item.count for cart_item in cart
        ]
        total = sum(summary_dishes_sum)
        return total

    chat_id = call.message.chat.id
    user_id = call.from_user.id
    language = userservice.get_user_language(user_id)
    bot.answer_callback_query(call.id)
    bot.clear_step_handler_by_chat_id(chat_id)
    selected_number = int(call.data[6:])
    current_dish = userservice.get_current_user_dish(user_id)
    dish_to_check = Dish.query.get(current_dish.id)
    if selected_number > dish_to_check.quantity:
        not_enough_count = strings.get_string(
            'not_enough_count', language).format(dish_to_check.quantity)
        msg = bot.send_message(chat_id, text=not_enough_count)
        bot.register_next_step_handler(msg, dish_action_processor)
    else:
        userservice.add_dish_to_cart(user_id, current_dish, selected_number)
        cart = userservice.get_user_cart(user_id)
        total = _total_cart_sum(cart)
        bot.delete_message(chat_id, call.message.message_id)
        cart_contains_message = strings.from_cart_items(cart, language, total)
        continue_message = strings.get_string(
            'catalog.continue', language).format(cart_contains_message)
        back_to_the_catalog(chat_id, language, continue_message)
        catalog_message = strings.get_string('catalog.start', language)
        bot.send_message(chat_id, catalog_message, parse_mode='HTML')
Esempio n. 2
0
def notification_callback_query(call):
    try:
        order = orderservice.get_order_by_id(int(call.data[6:]))
        user_id = order.user_id
        telegram_bot.send_message(user_id,
                                  strings.get_string('notifications.accepted'))
        telegram_bot.edit_message_reply_markup(call.message.chat.id,
                                               call.message.message_id,
                                               reply_markup=None)
        telegram_bot.answer_callback_query(call.id)
    except:
        telegram_bot.answer_callback_query(
            call.id, strings.get_string('notifications.blocked'), True)
Esempio n. 3
0
def _answer_callback_query_safely(query_id, text, show_alert=None):
    """
    Answer callback query without throwing an exception
    :param query_id: Callback query id
    :param text: Text answer
    :param show_alert: Show a popup alert
    """
    try:
        telegram_bot.answer_callback_query(query_id,
                                           text,
                                           show_alert=show_alert)
    except ApiException:
        pass
Esempio n. 4
0
def answer_handler(query: CallbackQuery):
    if settings.get_bot_state() != settings.BotStates.WORK:
        return
    data = query.data
    data = data.split('@')
    test_id = data[0]
    option_id = data[1]
    user = query.from_user
    channel_id = query.message.chat.id
    try:
        answers_processor(test_id, option_id, user, channel_id, query)
    except Exception as e:
        telegram_bot.answer_callback_query(query.id,
                                           strings.get_string('answer.error'))
        raise e
Esempio n. 5
0
def notification_callback_query(call):
    order = orderservice.get_order_by_id(call.data[6:])
    if call.data[0:6] == 'accept':
        user_id = order.user_id
        current_user = userservice.get_user_by_id(user_id)
        current_user.count_orders += order.order_items.all()[0].count
        db.session.add(current_user)
        db.session.commit()
        telegram_bot.send_message(user_id, strings.get_string('notifications.accepted'))
        if (10 - (current_user.count_orders % 10)) - 1 == 0:
            free_coffee_msg = '<b>При оформлении следующего заказа, один кофе бесплатный!</b>'
            telegram_bot.send_message(user_id, text=free_coffee_msg, parse_mode='HTML')
        else:
            free_coffee_msg = '<b>До бесплатного кофе: {}</b>'.format((10 - (current_user.count_orders % 10)) - 1)
            telegram_bot.send_message(user_id, text=free_coffee_msg, parse_mode='HTML')
        telegram_bot.edit_message_reply_markup(call.message.chat.id, call.message.message_id, reply_markup=None)
        telegram_bot.answer_callback_query(call.id)
    else:
        user_id = order.user_id
        userservice.clear_user_cart(user_id)
        telegram_bot.send_message(user_id, strings.get_string('notifications.canceled'))
        telegram_bot.edit_message_reply_markup(call.message.chat.id, call.message.message_id, reply_markup=None)
        telegram_bot.answer_callback_query(call.id)
Esempio n. 6
0
def notification_callback_query(call):
    order = orderservice.get_order_by_id(call.data[6:])
    if call.data[0:6] == 'accept':
        user_id = order.user_id
        current_user = userservice.get_user_by_id(user_id)
        current_user.count_orders += order.order_items.all()[0].count
        db.session.add(current_user)
        db.session.commit()
        telegram_bot.send_message(user_id,
                                  strings.get_string('notifications.accepted'))
        telegram_bot.edit_message_reply_markup(call.message.chat.id,
                                               call.message.message_id,
                                               reply_markup=None)
        telegram_bot.answer_callback_query(call.id)
    else:
        user_id = order.user_id
        userservice.clear_user_cart(user_id)
        telegram_bot.send_message(user_id,
                                  strings.get_string('notifications.canceled'))
        telegram_bot.edit_message_reply_markup(call.message.chat.id,
                                               call.message.message_id,
                                               reply_markup=None)
        telegram_bot.answer_callback_query(call.id)