-
Notifications
You must be signed in to change notification settings - Fork 2
/
bot.py
251 lines (189 loc) · 7.98 KB
/
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import logging
import os
import pickle
import random
import sys
from telegram import InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import CallbackQueryHandler, CommandHandler, Filters, MessageHandler, Updater
from questions_parser import parser_list, read_file
logging.basicConfig(format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO)
logger = logging.getLogger(__name__)
questions = read_file("questions.dat")
questions = parser_list(questions)
with open("sessions.dat", "rb") as fp:
sessions = pickle.load(fp)
TOKEN = os.getenv("TOKEN")
mode = os.getenv("MODE")
if mode == "dev":
def run(updater):
updater.start_polling()
elif mode == "prod":
def run(updater):
PORT = int(os.environ.get("PORT", "8443"))
HEROKU_APP_NAME = os.environ.get("HEROKU_APP_NAME")
updater.start_webhook(
listen="0.0.0.0",
port=PORT,
url_path=TOKEN,
webhook_url=f"https://{HEROKU_APP_NAME}.herokuapp.com/{TOKEN}",
)
else:
logger.error("No MODE specified")
sys.exit(1)
def dump_session():
with open("sessions.dat", "wb") as fp:
pickle.dump(sessions, fp)
def add_handler(dispatcher):
start_handler = CommandHandler("start", start)
dispatcher.add_handler(start_handler)
help_handler = CommandHandler("help", help)
dispatcher.add_handler(help_handler)
create_session_handler = CommandHandler("create_session", create_session)
dispatcher.add_handler(create_session_handler)
join_session_handler = CommandHandler("join_session", join_session)
dispatcher.add_handler(join_session_handler)
session_draw_question_handler = CommandHandler("session_draw_question", session_draw_question)
dispatcher.add_handler(session_draw_question_handler)
current_session_question_handler = CommandHandler("current_session_question", current_session_question)
dispatcher.add_handler(current_session_question_handler)
message_handler = MessageHandler(Filters.text, send_message)
dispatcher.add_handler(message_handler)
def send_message(update, context):
logger.info(update.message.text)
context.bot.send_message(chat_id=update.effective_chat.id, text=draw_question())
def help(update, context):
text = "Welcome to the English PyBar Bot by @grupysanca!\n\n"
text += "Send /start to get started.\n\n"
text += "To create a new session, click on the 'Create session' button.\n\n"
text += "You can get a random question from the default question list without joining a session clicking on the 'Draw random question' button.\n\n"
text += "If you already have a session id, send /join_session SESSION_ID to join.\n\n"
text += (
"Once you join a session, there are buttons to draw a new question and to get the current one.\n\n"
)
text += "Have fun!\n\n"
text += "This bot is open source and can be found here: https://github.com/grupy-sanca/english-pybar-bot"
context.bot.send_message(chat_id=update.effective_chat.id, text=text)
def start(update, context):
keyboard = [
[
InlineKeyboardButton("Create session", callback_data="/create_session"),
InlineKeyboardButton("Draw random question", callback_data="/random_question"),
]
]
reply_markup = InlineKeyboardMarkup(keyboard)
update.message.reply_text(
"Hello World!\nPlease, create a new session or draw a random question", reply_markup=reply_markup
)
def on_button_press(update, context):
query = update.callback_query
query.answer()
if "/create_session" in query.data:
create_session(query, context)
return
if "/draw_question" in query.data:
session_draw_question(query, context)
return
if "/current_question" in query.data:
current_session_question(query, context)
return
if "/random_question" in query.data:
keyboard = [[InlineKeyboardButton("Draw random question", callback_data="/random_question")]]
reply_markup = InlineKeyboardMarkup(keyboard)
query.edit_message_text(text=f"Random question:\n{draw_question()}", reply_markup=reply_markup)
return
query.edit_message_text(text=f"Selected: {query.data}")
def draw_question():
global questions
return random.choice(questions)
def create_session(update, context):
global sessions, questions
session_questions = questions[:]
random.shuffle(session_questions)
session_id = str(update.message.from_user.id)
sessions[session_id] = {"user_list": [session_id], "questions": session_questions}
dump_session()
update.edit_message_text(text=f"Session created!\nUse this code to join: {session_id}")
def join_session(update, context):
global sessions
text_split = update.message.text.split()
if len(text_split) < 2:
context.bot.send_message(chat_id=update.effective_chat.id, text="Usage: /join_session SESSION_ID")
return
session_id = text_split[-1]
if session_id not in sessions.keys():
context.bot.send_message(chat_id=update.effective_chat.id, text=f"SESSION_ID {session_id} is invalid")
return
user = update.message.from_user.id
if user in sessions[session_id]["user_list"]:
context.bot.send_message(chat_id=update.effective_chat.id, text="You already are in this session!")
return
sessions[session_id]["user_list"].append(user)
dump_session()
keyboard = [
[
InlineKeyboardButton("Draw next question", callback_data="/draw_question"),
InlineKeyboardButton("Get current question", callback_data="/current_question"),
]
]
reply_markup = InlineKeyboardMarkup(keyboard)
context.bot.send_message(
chat_id=update.effective_chat.id,
text=f"Session joined!\nThe current questions is:\n{sessions[session_id]['questions'][-1]}",
reply_markup=reply_markup,
)
def get_session_id(user):
global sessions
session_id = None
for session_id, session in sessions.items():
if user in session["user_list"]:
session_id = session_id
break
return session_id
def session_draw_question(update, context):
global sessions
user = update.message.from_user.id
session_id = get_session_id(user)
if not session_id:
update.edit_message_text(text=f"You're not in a session.\nRandom question:\n{draw_question()}")
return
if not sessions[session_id]["questions"]:
update.edit_message_text(text="Questions list is empty :(")
return
sessions[session_id]["questions"].pop()
dump_session()
keyboard = [
[
InlineKeyboardButton("Draw next question", callback_data="/draw_question"),
InlineKeyboardButton("Get current question", callback_data="/current_question"),
]
]
reply_markup = InlineKeyboardMarkup(keyboard)
update.edit_message_text(
text=f"The next question is:\n{sessions[session_id]['questions'][-1]}", reply_markup=reply_markup
)
def current_session_question(update, context):
global sessions
user = update.message.from_user.id
session_id = get_session_id(user)
if not session_id:
update.edit_message_text(text="You currently don't belong to a session")
return
if not sessions[session_id]["questions"]:
update.edit_message_text(text="Questions list is empty :(")
return
keyboard = [
[
InlineKeyboardButton("Draw next question", callback_data="/draw_question"),
InlineKeyboardButton("Get current question", callback_data="/current_question"),
]
]
reply_markup = InlineKeyboardMarkup(keyboard)
update.edit_message_text(
text=f"The current question is:\n{sessions[session_id]['questions'][-1]}", reply_markup=reply_markup
)
if __name__ == "__main__":
updater = Updater(token=TOKEN, use_context=True)
dispatcher = updater.dispatcher
add_handler(dispatcher)
dispatcher.add_handler(CallbackQueryHandler(on_button_press))
run(updater)