-
Notifications
You must be signed in to change notification settings - Fork 0
/
vk_quiz_bot.py
129 lines (111 loc) · 5.02 KB
/
vk_quiz_bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import argparse
import logging
import os
import random
import time
import redis
import vk_api
from dotenv import load_dotenv
from telegram.ext import CallbackContext
from vk_api.keyboard import VkKeyboard, VkKeyboardColor
from vk_api.longpoll import VkEventType, VkLongPoll
from get_sentences import get_quiz
def send_messages(event, vk, text):
vk.messages.send(
user_id=event.user_id,
message=text,
keyboard=keyboard.get_keyboard(),
random_id=random.randint(1, 1000),
)
def handle_new_question_request(event, vk, quiz):
total_questions = int(redis_db.hget("user" + event.user_id, "total_questions"))
question_number = int(redis_db.hget("user" + event.user_id, "question_number"))
total_questions += 1
question_number += 2
redis_db.hset("user" + event.user_id, "total_questions", total_questions)
redis_db.hset("user" + event.user_id, "question_number", question_number)
send_messages(event, vk, quiz[question_number])
def handle_solution_attempt(event, vk, quiz):
question_number = int(redis_db.hget("user" + event.user_id, "question_number"))
if event.text in quiz[question_number - 1]:
correct_answers = int(redis_db.hget("user" + event.user_id, "correct_answers"))
correct_answers += 1
redis_db.hset("user" + event.user_id, "correct_answers", correct_answers)
send_messages(
event,
vk,
"Правильно! Поздравляю! Для следующего вопроса нажми «Новый вопрос»",
)
if question_number == len(quiz):
finish_quiz(update, context)
else:
send_messages(event, vk, "Неправильно… Попробуешь ещё раз?")
def take_surrender(event, vk, quiz):
question_number = int(redis_db.hget("user" + event.user_id, "question_number"))
surrender = int(redis_db.hget("user" + event.user_id, "surrender"))
surrender += 1
redis_db.hset("user" + event.user_id, "surrender", surrender)
send_messages(
event,
vk,
f'Пфф, слабак.. Правильный ответ:\n{quiz[question_number - 1]}\nЧтобы продолжить, нажмите "Новый вопрос"',
)
if question_number == len(quiz):
finish_quiz(update, context)
def view_score(event, vk):
total_questions = redis_db.hget("user" + event.user_id, "total_questions")
correct_answers = redis_db.hget("user" + event.user_id, "correct_answers")
surrender = redis_db.hget("user" + event.user_id, "surrender")
send_messages(
event,
vk,
f"Всего вопросов: {total_questions}\nПравильных ответов: {correct_answers}\nПотерпели неудач: {surrender}",
)
def finish_quiz(event, vk):
send_messages(event, vk, "К сожалению вопросы закончились..")
vk.longpoll.stop()
if __name__ == "__main__":
load_dotenv()
parser = argparse.ArgumentParser(description="Проводит викторину в VK")
parser.add_argument(
"-f",
dest="file_path",
help="Путь к файлу с вопросами",
default="questions/1vs1200.txt",
)
redis_db = redis.Redis(
host=os.environ["REDIS_HOST"],
port=os.environ["REDIS_PORT"],
password=os.environ["REDIS_PASWORD"],
decode_responses=True,
)
quiz = get_quiz(parser.parse_args().file_path)
vk_session = vk_api.VkApi(token=os.environ["VK_TOKEN"])
vk = vk_session.get_api()
keyboard = VkKeyboard(one_time=True)
keyboard.add_button("Новый вопрос", color=VkKeyboardColor.PRIMARY)
keyboard.add_button("Сдаться", color=VkKeyboardColor.NEGATIVE)
keyboard.add_line()
keyboard.add_button("Мой счёт", color=VkKeyboardColor.POSITIVE)
while True:
try:
longpoll = VkLongPoll(vk_session)
for event in longpoll.listen():
if event.type == VkEventType.MESSAGE_NEW and event.to_me:
if event.text == "Start":
redis_db.hset("user" + event.user_id, "question_number", 0)
redis_db.hset("user" + event.user_id, "total_questions", 0)
redis_db.hset("user" + event.user_id, "correct_answers", 0)
redis_db.hset("user" + event.user_id, "surrender", 0)
send_messages(event, vk, "Привет! Начнём викторину?")
elif event.text == "Новый вопрос":
handle_new_question_request(event, vk, quiz)
elif event.text == "Сдаться":
take_surrender(event, vk, quiz)
elif event.text == "Мой счёт":
view_score(event, vk)
else:
handle_solution_attempt(event, vk, quiz)
except Exception:
logging.exception("VK бот упал с ошибкой: ")
time.sleep(10)