@bot.message_handler(content_types=['text'])
def menu(message):
if message.text == 'Викторина':
conn = sqlite3.connect('rustest.db')
cursor = conn.cursor()
cursor.execute("SELECT * FROM users WHERE chat_id == ?", (f'{message.chat.id}',))
res = cursor.fetchone()
if res:
cursor.execute(f"SELECT id FROM quiz")
result1 = cursor.fetchall()
question_id = (random.choice(result1)[0])
cursor.execute(f"SELECT * FROM quiz WHERE id == {question_id}")
result = cursor.fetchone()
question = result[1]
answer = result[2].rsplit(',', 15)
co_id = result[3]
score = res[3] + 1
poll = bot.send_poll(message.chat.id, f'{question}', answer, is_anonymous=False, type='quiz', correct_option_id=co_id)
cursor.execute(f"UPDATE quiz SET poll_id == ? WHERE id == ?", (f'{poll.poll.id}', f'{question_id}'))
conn.commit()
@bot.poll_answer_handler()
def pola(polle):
cursor.execute("SELECT * FROM quiz WHERE poll_id == ?", (f'{polle.poll_id}',))
otvet = cursor.fetchone()
if otvet[3] == polle.option_ids[0]:
bot.send_message(message.chat.id, 'Отлично!')
else:
bot.send_message(message.chat.id, 'Все плохо')
if message.text == 'Викторина':
# Создаем рандомное число и берем вопрос с таким id
conn = sqlite3.connect('rustest.db')
cursor = conn.cursor()
cursor.execute(f"SELECT id FROM quiz")
result1 = cursor.fetchall()
question_id = (random.choice(result1)[0])
cursor.execute(f"SELECT * FROM quiz WHERE id == {question_id}")
result = cursor.fetchone()
question = result[1]
answer = result[2].rsplit(',', 15)
co_id = result[3]
# Собираем вопрос из базы и ставим ему ограничение 5 мин (300 сек)
poll = bot.send_poll(report, f'{question}', answer, is_anonymous=False, type='quiz', correct_option_id=co_id,
open_period=300)
# Присваиваем вопросу poll_id и сохраняем в базу
cursor.execute(f"UPDATE quiz SET poll_id == ? WHERE id == ?", (f'{poll.poll.id}', f'{question_id}'))
conn.commit()
# Тут отлавливаем ответ на викторину и сохраняем юзеров в базу
@bot.poll_answer_handler()
def pola(polle):
conn = sqlite3.connect('rustest.db')
cursor = conn.cursor()
cursor.execute("INSERT INTO users (chat_id, f_name, score) VALUES (?, ?, ?)",
(f'{polle.user.id}', f'{polle.user.first_name}', 0))
conn.commit()
cursor.execute("SELECT * FROM quiz WHERE poll_id == ?", (f'{polle.poll_id}',))
otvet = cursor.fetchone()
# Если юзер ответил верно плюсуем ему 1 балл, если нет, просто ничего не делаем
if otvet[3] == polle.option_ids[0]:
cursor.execute("SELECT score FROM users WHERE chat_id == ?", (f'{polle.user.id}',))
res = cursor.fetchone()
score = res[0] + 1
cursor.execute("UPDATE users SET score == ? WHERE chat_id == ?", (f'{score}', f'{polle.user.id}'))
conn.commit()
conn.close()
else:
pass