Guest User

Untitled

a guest
Mar 22nd, 2022
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.93 KB | None | 0 0
  1.  
  2. import logging
  3. from os import getenv
  4.  
  5. import aiogram.utils.markdown as md
  6. import aiomysql
  7. from aiogram.types import ReplyKeyboardRemove, \
  8. ReplyKeyboardMarkup, InlineKeyboardButton, InlineKeyboardMarkup, CallbackQuery, ParseMode, ContentType
  9. import asyncio
  10. import time
  11. from aiogram import Bot, Dispatcher, types
  12. from aiogram.dispatcher import FSMContext
  13. from aiogram.dispatcher.filters import Text, state
  14. from aiogram.dispatcher.filters.state import State, StatesGroup
  15. from aiogram.utils import executor
  16. from aiogram.contrib.fsm_storage.memory import MemoryStorage
  17. from aiogram.dispatcher.filters import Command, Text
  18. from aiogram.types import Message
  19. import random
  20.  
  21. bot_token = getenv("BOT_TOKEN")
  22. if not bot_token:
  23. exit("Error: no token provided")
  24.  
  25.  
  26. loop = asyncio.get_event_loop()
  27.  
  28. storage = MemoryStorage()
  29. bot = Bot(token=bot_token)
  30. # Диспетчер для бота
  31. dp = Dispatcher(bot, storage=storage)
  32. # Включаем логирование, чтобы не пропустить важные сообщения
  33. logging.basicConfig(level=logging.INFO)
  34. class Form(StatesGroup):
  35. name = State()
  36.  
  37. start_text = "Привет 👋🏻 Выбери интересующее тебя меню ниже👇🏻"
  38. start_keyboard = InlineKeyboardMarkup().add(InlineKeyboardButton(text="Каталог", callback_data="Catalog"))
  39.  
  40. count = 0
  41.  
  42. class InputData(StatesGroup):
  43. input_fio = State()
  44.  
  45. @dp.message_handler(state='*', commands='cancel')
  46. @dp.message_handler(Text(equals='cancel', ignore_case=True), state='*')
  47. async def cancel_handler(message: types.Message, state: FSMContext, query: CallbackQuery):
  48. current_state = await state.get_state()
  49. if current_state is None:
  50. return
  51.  
  52.  
  53. @dp.message_handler(Command('Start'))
  54. async def start(message: types.Message, state: FSMContext):
  55. await state.finish()
  56. keyboard = InlineKeyboardMarkup(row_width=2).add(
  57. InlineKeyboardButton(text="🎟 КУПИТЬ ПРОХОДКУ", callback_data="Купить билет"),
  58. InlineKeyboardButton(text="🔥 УЗНАТЬ О БЛИЖАЙШЕМ МЕРОПРИЯТИИ", callback_data="info"),
  59. )
  60. await message.answer(start_text, reply_markup=keyboard)
  61. await state.finish()
  62. conn = await aiomysql.connect(host='localhost', port=3306, user='root', password='', db='aiodb',
  63. loop=loop)
  64. cur = await conn.cursor()
  65. teled = message.from_user.id
  66. sql = "SELECT teleid FROM users WHERE teleid=(%s)"
  67. await cur.execute(sql, (teled))
  68. row = await cur.fetchone()
  69. if row is not None: # есть хоть одна строка?
  70. teleid = row[0]
  71. id = teled
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79. conn.close
  80. print(teled)
  81.  
  82.  
  83. else:
  84. start_sql = "INSERT INTO users (teleid) VALUES (%s)"
  85. tupe = teled
  86. await cur.execute(start_sql, tupe)
  87. await conn.commit()
  88. conn.close
  89. yes = 1
  90.  
  91.  
  92. @dp.callback_query_handler(text="MAIN")
  93. async def start(query: CallbackQuery):
  94. keyboard = InlineKeyboardMarkup(row_width=2).add(
  95. InlineKeyboardButton(text="Купить билет", callback_data="Купить билет"),
  96. InlineKeyboardButton(text="Узнать о мероприятии", callback_data="Films"),
  97. )
  98. await query.message.edit_text(text=start_text, reply_markup=keyboard)
  99.  
  100.  
  101. @dp.callback_query_handler(text="Купить билет")
  102. async def buyit(query: CallbackQuery):
  103. keyboard = InlineKeyboardMarkup(row_width=2).add(
  104. InlineKeyboardButton(text="🎫 VIP", callback_data="VIP"),
  105. InlineKeyboardButton(text="🎟 Стандарт", callback_data="default"),
  106. InlineKeyboardButton(text="Назад", callback_data="MAIN")
  107. )
  108. await query.message.edit_text("🤔Какой билет хочешь купить?", reply_markup=keyboard)
  109.  
  110.  
  111.  
  112.  
  113. @dp.callback_query_handler(text="VIP")
  114. async def vip(query: CallbackQuery):
  115. keyboard = InlineKeyboardMarkup(row_width=2).add(
  116. InlineKeyboardButton(text="Купить", callback_data="vipcount"),
  117. InlineKeyboardButton(text="Назад", callback_data="Купить билет")
  118.  
  119. )
  120.  
  121. pin = random.randint(100000, 999999)
  122. print(pin)
  123.  
  124. await query.message.edit_text("""
  125.  
  126. Стоимость - 750RUB
  127. """, reply_markup=keyboard)
  128.  
  129.  
  130. #@dp.callback_query_handler(text="vipcount")
  131. #async def vipcount(query: CallbackQuery):
  132. # keyboard = InlineKeyboardMarkup(row_width=3).add(
  133. # InlineKeyboardButton(text=1, callback_data="buyvip:1"),
  134. # InlineKeyboardButton(text=2, callback_data="buyvip:2"),
  135. # InlineKeyboardButton(text=3, callback_data="buyvip:3")
  136. # )
  137. # await query.message.edit_text("Выберите кол-во билетов", reply_markup=keyboard)
  138.  
  139. @dp.callback_query_handler(text_startswith="vipcount")
  140. async def checkname(query: CallbackQuery):
  141. keyboard = InlineKeyboardMarkup(row_width=1).add(
  142. InlineKeyboardButton(text="Назад", callback_data="Купить билет")
  143. )
  144. await query.message.answer("Введите свое ФИО")
  145. await InputData.input_fio.set()
  146.  
  147.  
  148.  
  149.  
  150. @dp.message_handler(state=InputData.input_fio)
  151. async def process_name(message: types.Message):
  152. conn = await aiomysql.connect(host='localhost', port=3306, user='root', password='',
  153. db='aiodb',
  154. loop=loop)
  155. cur = await conn.cursor()
  156. teled = message.from_user.id
  157. sql = "SELECT name FROM users WHERE teleid=(%s)"
  158. await cur.execute(sql, (teled))
  159. row = await cur.fetchone()
  160. name = message.text
  161. print(row)
  162. row = row[0]
  163. print("olk")
  164. mes = message.text
  165. teled = message.from_user.id
  166. start_sql = "UPDATE users set name = (%s) where teleid=(%s)"
  167. conn = await aiomysql.connect(host='localhost', port=3306, user='root', password='',
  168. db='aiodb',
  169. loop=loop)
  170. cur = await conn.cursor()
  171. mesg = message.text
  172. await cur.execute(start_sql, (name, teled))
  173. await conn.commit()
  174. await message.answer("Вы успешно добавлены!")
  175.  
  176. #################### EXPERIMENTAL AREA ######################
  177. pin = random.randint(100000, 999999)
  178.  
  179.  
  180. pin_install = "UPDATE users set pin = (%s) where teleid=(%s)"
  181. conn = await aiomysql.connect(host='localhost', port=3306, user='root', password='',
  182. db='aiodb',
  183. loop=loop)
  184. cur = await conn.cursor()
  185. await cur.execute(pin_install, (pin, teled))
  186. await conn.commit()
  187. await bot.send_message(
  188. message.chat.id,
  189. md.text(
  190. md.text('Ваш персональный пин-код -', md.bold(pin)),
  191. md.text('Покажите данный пин-код на входе')
  192. ),
  193. parse_mode = ParseMode.MARKDOWN,
  194. )
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201. print(pin)
  202.  
  203.  
  204. Form.next()
  205.  
  206.  
Advertisement
Add Comment
Please, Sign In to add comment