Example #1
0
def handle_description(bot, update, user_id, user_reply):
    type_of_callback, callback_value = user_reply.split('_')
    if type_of_callback == 'menu':
        bot.send_message(
            text='Выберите пиццу:',
            chat_id=user_id,
            reply_markup=create_menu(user_id)
        )
        delete_last_message(bot, update)
        return 'MENU'
    elif type_of_callback == 'cart':
        user_cart_items = get_user_cart_items(user_id)
        cart_info = get_cart_info(user_cart_items)
        bot.send_message(
            text=cart_info,
            chat_id=user_id,
            reply_markup=create_keyboard_for_cart(user_cart_items)
        )
        delete_last_message(bot, update)
        return 'CART'
    elif type_of_callback == 'add':
        number_of_pizza = 1
        bot.send_message(
            text='Выберите количество',
            chat_id=user_id,
            reply_markup=create_selection_buttons(
                number_of_pizza,
                product_id=callback_value
            )
        )
        delete_last_message(bot, update)
        return 'SELECTION_QUANTITY_OF_PIZZAS'
Example #2
0
def send_message_to_courier(bot, user_id):
    user_cart_items = get_user_cart_items(user_id)
    username = database.get(f'user_{user_id}:name').decode('utf-8')
    user_phone_number = database.get(
        f'tg:user_{user_id}:phone_number'
    ).decode('utf-8')
    order_info = get_order_info_for_courier(
        user_cart_items,
        username,
        user_phone_number
    )

    user_entry_id = database.get(f'user_{user_id}:entry_id').decode('utf-8')
    user_entry = get_flow_entries(CUSTOMER_ADDRESS_SLUG, user_entry_id)
    user_latitude = user_entry['latitude']
    user_longitude = user_entry['longitude']

    nearest_pizzeria = get_nearest_pizzeria(
        (user_latitude, user_longitude),
        get_flow_entries('pizzeria')
    )
    courier_id = nearest_pizzeria['courier']

    bot.send_location(
        chat_id=courier_id,
        latitude=user_latitude,
        longitude=user_longitude
    )
    bot.send_message(
        chat_id=courier_id,
        text=order_info
    )
Example #3
0
def handle_menu(bot, update, user_id, user_reply):
    if update.message:
        bot.send_message(
            text='Выберите пиццу:',
            chat_id=user_id,
            reply_markup=create_menu(user_id)
        )
        return 'MENU'

    type_of_callback, callback_value = user_reply.split('_')
    if type_of_callback == 'start':
        bot.send_message(
            text='Выберите пиццу:',
            chat_id=user_id,
            reply_markup=create_menu(user_id)
        )
        return 'MENU'
    elif type_of_callback == 'page':
        bot.edit_message_reply_markup(
            chat_id=user_id,
            message_id=update.callback_query.message.message_id,
            reply_markup=create_menu(user_id, int(callback_value))
        )
        return 'MENU'
    elif type_of_callback == 'id':
        product = get_product(callback_value)
        product_image_id = product['relationships']['main_image']['data']['id']
        bot.send_photo(
            chat_id=user_id,
            photo=get_product_image_url(product_image_id),
            caption=get_product_info(product),
            parse_mode=ParseMode.MARKDOWN,
            reply_markup=create_keyboard_for_product_description(
                user_id,
                product_id=callback_value
            ),
        )
        delete_last_message(bot, update)
        return 'DESCRIPTION'
    elif type_of_callback == 'cart':
        user_cart_items = get_user_cart_items(user_id)
        cart_info = get_cart_info(user_cart_items)
        bot.send_message(
            text=cart_info,
            chat_id=user_id,
            reply_markup=create_keyboard_for_cart(user_cart_items)
        )
        delete_last_message(bot, update)
        return 'CART'
Example #4
0
def get_cards_for_cart(cart_id):
    products = get_user_cart_items(cart_id)
    return [{
        "title": (f"{PIZZA} {product['name']} "
                  f"({product['unit_price']['amount']}"
                  f"{RUB})"),
        "image_url":
        product['image']['href'],
        "subtitle":
        get_short_description_for_fb_cart(product),
        "buttons": [{
            'type': 'postback',
            'title': f'{CROSS_MARK} Удалить из корзины',
            'payload': f'remove_{product["id"]}'
        }, {
            'type': 'postback',
            'title': f'{PLUS} Добавить еще',
            'payload': f'add_{product["id"]}'
        }]
    } for product in products]
Example #5
0
def handle_payment(bot, update, user_id, user_reply):
    shipping_price = int(user_reply)
    order_price = get_user_cart(user_id)['meta']['display_price']['with_tax']['amount']
    order_info = get_order_info_for_invoice(
        get_user_cart_items(user_id),
        order_price,
        shipping_price,
    )

    bot.sendInvoice(
        chat_id=user_id,
        title='Тестовый платеж',
        description=order_info,
        payload='Custom-Payload',
        provider_token=os.getenv('PROVIDER_TOKEN'),
        start_parameter='test-payment',
        currency='rub',
        prices=[
            LabeledPrice(label='Pizza', amount=order_price*100),
            LabeledPrice(label='Shipping', amount=shipping_price*100)
        ]
    )
    return 'MENU'
Example #6
0
def handle_cart(bot, update, user_id, user_reply):
    query, callback_value = user_reply.split('_')
    if query == 'remove':
        delete_product_from_cart(
            user_id,
            product_id=callback_value
        )
        user_cart_items = get_user_cart_items(user_id)
        cart_info = get_cart_info(user_cart_items)
        bot.edit_message_text(
            chat_id=user_id,
            message_id=update.callback_query.message.message_id,
            text=cart_info,
            reply_markup=create_keyboard_for_cart(user_cart_items)
        )
        bot.answer_callback_query(
            update.callback_query.id,
            text='Пицца удалена из корзины',
            show_alert=False
        )
        return 'CART'
    elif query == 'menu':
        bot.send_message(
            text='Выберите пиццу:',
            chat_id=user_id,
            reply_markup=create_menu(user_id)
        )
        delete_last_message(bot, update)
        return 'MENU'
    elif query == 'ordering':
        bot.send_message(
            chat_id=user_id,
            text='Напишите, пожалуйста, своё имя.'
        )
        delete_last_message(bot, update)
        return 'WAITING_USERNAME'