Advertisement
Guest User

Untitled

a guest
Apr 12th, 2021
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.20 KB | None | 0 0
  1. import os
  2. import re
  3. import json
  4. import telebot
  5. from telebot import types
  6.  
  7. import config
  8.  
  9. bot = telebot.TeleBot(config.bot_token, skip_pending=True, threaded=False)
  10.  
  11. if not os.path.exists("catalog.json"):
  12.   with open("catalog.json","w") as f:
  13.     f.write("""{
  14.  "123": {
  15.    "name": "Фильтр для воды мышьяковый",
  16.    "price": 1000
  17.  },
  18.  "456": {
  19.    "name": "Фильтр для воды ртутный",
  20.    "price": 1200
  21.  },
  22.  "789": {
  23.    "name": "Ловушка для мышей",
  24.    "price": 850
  25.  }
  26. }""")
  27.  
  28. with open("catalog.json","r") as f:
  29.   catalog = json.load(f)
  30.  
  31. @bot.message_handler(content_types=["text"])
  32. def process_message(message):
  33.   print(message)
  34.   if re.match('\d+', message.text):
  35.     number = message.text
  36.     if number in catalog:
  37.       name = catalog[number]["name"]
  38.       price = catalog[number]["price"]
  39.       keyboard = types.InlineKeyboardMarkup()
  40.       keyboard.add(types.InlineKeyboardButton(text="Заказать", callback_data=f"order:{number}"))
  41.       keyboard.add(types.InlineKeyboardButton(text="Посмотреть на сайте", url=f"http://site.ru/product/{number}"))
  42.       bot.send_message(message.chat.id, f"Вы выбрали *{name}*, цена *{price}*", parse_mode="MarkdownV2", reply_markup=keyboard)
  43.       return
  44.     else:
  45.       bot.send_message(message.chat.id, f"Такой товар не найден\!")
  46.       return
  47.   else:
  48.     words = message.text.split(" ")
  49.     products = []
  50.     for number, item in catalog.items():
  51.       name = item["name"]
  52.       price = item["price"]
  53.       found = True
  54.       for word in words:
  55.         if word not in name:
  56.           found = False
  57.           break
  58.       if found:
  59.         products.append(f"Арт. *{number}*: *{name}*, цена *{price}*")
  60.     if len(products) > 0:
  61.       products_text = "\n".join(products)
  62.       bot.send_message(message.chat.id, f"Найденые товары:\n\n{products_text}\n\nВведите артикул нужного товара", parse_mode="MarkdownV2")
  63.  
  64. @bot.callback_query_handler(func=lambda call: call.data.startswith("order:"))
  65. def process_order(call):
  66.   number = call.data.split(":")[1]
  67.   name = catalog[number]["name"]
  68.   price = catalog[number]["price"]
  69.   keyboard = types.InlineKeyboardMarkup()
  70.   keyboard.add(types.InlineKeyboardButton(text="Подтвердить", callback_data=f"accept_order"))
  71.   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)
  72.   bot.register_next_step_handler(call.message, comment_order)
  73.  
  74. def comment_order(message):
  75.   comment = message.text
  76.   bot.clear_step_handler(message)
  77.   bot.send_message(message.chat.id, f"Комментарий: {comment}")
  78.  
  79. @bot.callback_query_handler(func=lambda call: call.data == "accept_order")
  80. def accept_order(call):
  81.   bot.clear_step_handler(call.message)
  82.   bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.id, text=call.message.text+"\n\nЗаказ подтверждён, ожидайте доставки\!", parse_mode="MarkdownV2")
  83.  
  84. bot.polling()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement