def basket(message): user_id = message.from_user.id user_basket = utils.get_basket(user_id) keyboard = telebot.types.ReplyKeyboardMarkup(row_width=1, resize_keyboard=True) confirm_button = telebot.types.KeyboardButton(config.confirm_button) clear_button = telebot.types.KeyboardButton(config.clear_button) back_button = telebot.types.KeyboardButton(config.back_button) keyboard.add(confirm_button, clear_button, back_button) bot.send_message(message.chat.id, config.basket, reply_markup=keyboard) if user_basket: for item in user_basket: product = cat.find(item) if (product): bot.send_message(message.chat.id, '*' + product.item + '*', parse_mode='Markdown') bot.send_photo(message.chat.id, caption=product.categories[0], photo=product.categories[1]) else: bot.send_message( message.chat.id, 'База данных товаров обновилась! Похоже, некоторых товаров из вашей корзины больше нет...' ) else: bot.send_message(chat_id=message.chat.id, text=config.empty_basket)
def catalog(query): category = cat.find(query.data) bot.answer_callback_query(query.id) print('Пользователь', query.message.from_user.id, 'открыл', category.item) if isinstance(category.categories[0].categories[0], str): for item in category.categories: keyboard = telebot.types.InlineKeyboardMarkup(row_width=1) keyboard.add( telebot.types.InlineKeyboardButton( text=config.add_to_basket, callback_data=config.add_to_basket + '_' + item.item)) bot.send_message(query.message.chat.id, '*' + item.item + '*', reply_markup=back_keyboard, parse_mode='Markdown') bot.send_photo(query.message.chat.id, caption=item.categories[0], photo=item.categories[1], reply_markup=keyboard) else: keyboard = telebot.types.InlineKeyboardMarkup(row_width=1) buttons = (telebot.types.InlineKeyboardButton( text=subcat.item, callback_data=subcat.item) for subcat in category.categories) keyboard.add(*buttons) bot.delete_message(query.message.chat.id, query.message.message_id - 1) bot.delete_message(query.message.chat.id, query.message.message_id) bot.send_message(query.message.chat.id, '*' + category.item + '*', reply_markup=back_keyboard, parse_mode='Markdown') bot.send_message(query.message.chat.id, 'Выберите нужный вам пункт меню', reply_markup=keyboard)
def add_to_basket(query): logging.info(str(query.data)) item = cat.find(query.data.split('_')[1]) if not utils.get_basket(query.from_user.id): utils.set_basket(query.from_user.id) utils.add_to_basket(query.from_user.id, str(hash(item.item))) bot.answer_callback_query(callback_query_id=query.id, text='Успешно добавлено!')
def check_basket(message): user_id = message.from_user.id user_basket = utils.get_basket(user_id) bot.send_message(message.chat.id, config.ordering, reply_markup=back_keyboard) if user_basket: res = '' keyboard = telebot.types.InlineKeyboardMarkup(row_width=1) pay_button = telebot.types.InlineKeyboardButton(text=config.pay_button, pay=True) keyboard.add(pay_button) price = 0 for item in user_basket: item_price = cat.find(item).categories[0].split('\n')[-1] res += cat.find(item).item + ' - ' + item_price + ' руб.\n' price += int(item_price) # bot.send_message(message.chat.id, res, parse_mode='Markdown', reply_markup=keyboard) bot.send_invoice(chat_id=message.chat.id, title=config.user_basket, description=res, invoice_payload='invoice', provider_token=config.provider_token, start_parameter='invoice', currency='rub', prices=[ telebot.types.LabeledPrice( label=config.user_basket, amount=price * 100) ], need_name=True, need_email=True, need_phone_number=True, need_shipping_address=True, is_flexible=True, reply_markup=keyboard) else: bot.send_message(chat_id=message.chat.id, text=config.empty_basket)
def catalog(query): category = cat.find(query.data) logging.info(str(category)) bot.answer_callback_query(query.id) logging.info('Пользователь ' + str(query.from_user.id) + ' открыл' + str(category.item)) if isinstance(category.categories[0].categories[0], str): for item in category.categories: keyboard = telebot.types.InlineKeyboardMarkup(row_width=1) keyboard.add( telebot.types.InlineKeyboardButton( text=config.add_to_basket, callback_data=config.add_to_basket + '_' + str(hash(item.item)))) bot.send_message(query.message.chat.id, '*' + item.item + '*', reply_markup=back_keyboard, parse_mode='Markdown') try: bot.send_photo(query.message.chat.id, caption=item.categories[0], photo=item.categories[1], reply_markup=keyboard) except telebot.apihelper.ApiException as ex: print("err pic not found on ", item.categories[1]) bot.send_message(query.message.chat.id, "err: pic not found", reply_markup=back_keyboard, parse_mode='Markdown') else: keyboard = telebot.types.InlineKeyboardMarkup(row_width=1) buttons = (telebot.types.InlineKeyboardButton(text=subcat.item, callback_data=str( hash(subcat.item))) for subcat in category.categories) keyboard.add(*buttons) bot.delete_message(query.message.chat.id, query.message.message_id - 1) bot.delete_message(query.message.chat.id, query.message.message_id) bot.send_message(query.message.chat.id, '*' + category.item + '*', reply_markup=back_keyboard, parse_mode='Markdown') bot.send_message(query.message.chat.id, 'Выберите нужный вам пункт меню', reply_markup=keyboard)
def got_payment(message): bot.send_message(chat_id=message.chat.id, text=config.successful_payment.format( message.successful_payment.total_amount / 100, message.successful_payment.currency), parse_mode='Markdown') order_info = message.successful_payment.order_info user_id = message.from_user.id user_basket = utils.get_basket(user_id) buys_list = '' for item in user_basket: buys_list += cat.find(item).item + '\n' append_request(order_info.name, order_info.email, order_info.phone_number, order_info.shipping_address, buys_list, message.successful_payment.total_amount / 100, '') utils.del_user_basket(message.from_user.id)