Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import re
- import json
- import telebot
- from telebot import types
- import config
- bot = telebot.TeleBot(config.bot_token, skip_pending=True, threaded=False)
- if not os.path.exists("catalog.json"):
- with open("catalog.json","w") as f:
- f.write("""{
- "123": {
- "name": "Фильтр для воды мышьяковый",
- "price": 1000
- },
- "456": {
- "name": "Фильтр для воды ртутный",
- "price": 1200
- },
- "789": {
- "name": "Ловушка для мышей",
- "price": 850
- }
- }""")
- with open("catalog.json","r") as f:
- catalog = json.load(f)
- @bot.message_handler(content_types=["text"])
- def process_message(message):
- print(message)
- if re.match('\d+', message.text):
- number = message.text
- if number in catalog:
- name = catalog[number]["name"]
- price = catalog[number]["price"]
- keyboard = types.InlineKeyboardMarkup()
- keyboard.add(types.InlineKeyboardButton(text="Заказать", callback_data=f"order:{number}"))
- keyboard.add(types.InlineKeyboardButton(text="Посмотреть на сайте", url=f"http://site.ru/product/{number}"))
- bot.send_message(message.chat.id, f"Вы выбрали *{name}*, цена *{price}*", parse_mode="MarkdownV2", reply_markup=keyboard)
- return
- else:
- bot.send_message(message.chat.id, f"Такой товар не найден\!")
- return
- else:
- words = message.text.split(" ")
- products = []
- for number, item in catalog.items():
- name = item["name"]
- price = item["price"]
- found = True
- for word in words:
- if word not in name:
- found = False
- break
- if found:
- products.append(f"Арт. *{number}*: *{name}*, цена *{price}*")
- if len(products) > 0:
- products_text = "\n".join(products)
- bot.send_message(message.chat.id, f"Найденые товары:\n\n{products_text}\n\nВведите артикул нужного товара", parse_mode="MarkdownV2")
- @bot.callback_query_handler(func=lambda call: call.data.startswith("order:"))
- def process_order(call):
- number = call.data.split(":")[1]
- name = catalog[number]["name"]
- price = catalog[number]["price"]
- keyboard = types.InlineKeyboardMarkup()
- keyboard.add(types.InlineKeyboardButton(text="Подтвердить", callback_data=f"accept_order"))
- bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.id, text=call.message.text+"\n\nОставьте комментарий и подтвердите заказ", parse_mode="MarkdownV2", reply_markup=keyboard)
- bot.register_next_step_handler(call.message, comment_order)
- def comment_order(message):
- comment = message.text
- bot.clear_step_handler(message)
- bot.send_message(message.chat.id, f"Комментарий: {comment}")
- @bot.callback_query_handler(func=lambda call: call.data == "accept_order")
- def accept_order(call):
- bot.clear_step_handler(call.message)
- bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.id, text=call.message.text+"\n\nЗаказ подтверждён, ожидайте доставки\!", parse_mode="MarkdownV2")
- bot.polling()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement