-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
489 lines (393 loc) · 15.2 KB
/
main.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
from telegram import (
ReplyKeyboardMarkup,
ReplyKeyboardRemove,
Update,
InlineKeyboardButton,
InlineKeyboardMarkup,
KeyboardButton,
)
from telegram.ext import (
Application,
CommandHandler,
ContextTypes,
ConversationHandler,
MessageHandler,
CallbackQueryHandler,
filters,
)
from configparser import ConfigParser
from random import choice
from datetime import time
import aiosqlite
import asyncio
from warnings import filterwarnings
from telegram.warnings import PTBUserWarning
import pytz
ADMINS = [5712064064, 1010205515]
# dont show per_message error
filterwarnings(
action="ignore", message=r".*CallbackQueryHandler", category=PTBUserWarning
)
config = ConfigParser()
config.read("config.ini")
BOT_TOKEN = config["Telegram"]["tg_token"]
# states
CHOOSING, CHOOSE_ACTION, NAME, PHONE, GET_MAIL = range(5)
# callbacks
CANCEL, PLAY, REGISTER, CONVERSIONS, LEADER_BOARD, MAIL, YES_MAIL, NO_MAIL = range(8)
# conversation statuces
START, PLAYED, REGISTERED, PLAYED_AND_REGISTERED = range(4)
# job statuces
ONCE, DAYLY = range(2)
# database initialization
async def init_db():
async with aiosqlite.connect("bot.db") as db:
await db.execute("""
CREATE TABLE IF NOT EXISTS users (
user_id INTEGER PRIMARY KEY,
wins INTEGER,
defeats INTEGER,
name TEXT,
phone TEXT,
conversation_status INTEGER default 0
)
""")
await db.commit()
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
if update.effective_user.id in ADMINS:
keyboard = [
[
InlineKeyboardButton("Conversions", callback_data=CONVERSIONS),
InlineKeyboardButton("List of Users", callback_data=LEADER_BOARD),
],
[
InlineKeyboardButton("Send everyone", callback_data=MAIL),
],
]
await context.bot.send_message(
chat_id=update.effective_chat.id,
text="You are in admin panel",
reply_markup=InlineKeyboardMarkup(keyboard),
)
else:
keyboard = [
[
InlineKeyboardButton("Play", callback_data=PLAY),
InlineKeyboardButton("Register", callback_data=REGISTER),
]
]
await context.bot.send_message(
chat_id=update.effective_chat.id,
text="Hi! Would to play Rock & Paper & Scissors with me?",
reply_markup=InlineKeyboardMarkup(keyboard),
)
return CHOOSING
async def check_winner(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
computer = choice(["Rock", "Paper", "Scissors"])
user = update.effective_message.text
user_id = update.effective_user.id
"""Ответил, поэтому писать уже не нужно"""
remove_job_if_exists(name=f"{user_id}-{ONCE}", context=context)
is_winner = False
keyboard = [
[
InlineKeyboardButton("Play again", callback_data=PLAY),
InlineKeyboardButton("Cancel", callback_data=CANCEL),
],
]
message_to_user = "No one won"
if (
(user == "Paper" and computer == "Rock")
or (user == "Scissors" and computer == "Paper")
or (user == "Rock" and computer == "Scissors")
):
message_to_user = "You won!!"
is_winner = True
elif (
(user == "Rock" and computer == "Paper")
or (user == "Paper" and computer == "Scissors")
or (user == "Scissors" and computer == "Rock")
):
message_to_user = "You lost!!"
is_winner = False
async with aiosqlite.connect("bot.db") as db:
async with db.execute(
"SELECT wins, defeats FROM users WHERE user_id = ?", (user_id,)
) as cursor:
row = await cursor.fetchone()
if row:
# if user exists, increment their points
if is_winner:
w = row[0] + 1 if row[0] is not None else 1
await db.execute(
"""
UPDATE users
SET wins = ?, conversation_status = ?
WHERE user_id = ?
""",
(w, PLAYED_AND_REGISTERED, user_id),
)
else:
d = row[1] + 1 if row[1] is not None else 1
await db.execute(
"""
UPDATE users
SET defeats = ?, conversation_status = ?
WHERE user_id = ?
""",
(d, PLAYED_AND_REGISTERED, user_id),
)
else:
# if user does not exist set 1 or 0 change on get name
await db.execute(
"""
INSERT INTO users (user_id, wins, defeats, conversation_status)
VALUES (?, ?, ?, ?)
""",
(user_id, 1 if is_winner else 0, 0 if is_winner else 1, PLAYED),
)
await db.commit()
await update.message.reply_text(
f"Computer - {computer}\n{message_to_user}",
reply_markup=InlineKeyboardMarkup(keyboard),
)
return CHOOSING
async def choosing_callback(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
query = update.callback_query
await query.answer()
user_id = update.effective_chat.id
if int(query.data) == PLAY:
await query.edit_message_text(text="Let's play!")
reply_keyboard = [["Rock", "Paper"], ["Scissors"]]
"""если не походит, то через минуту напишем, чтобы ходил"""
context.job_queue.run_once(
once_job_step, 10, chat_id=user_id, name=f"{user_id}-{ONCE}"
)
await query.message.reply_text(
"Choose option:",
reply_markup=ReplyKeyboardMarkup(
reply_keyboard, one_time_keyboard=True, resize_keyboard=True
),
)
return CHOOSE_ACTION
elif int(query.data) == REGISTER:
async with aiosqlite.connect("bot.db") as db:
async with db.execute(
"SELECT name FROM users WHERE user_id = ?", (update.effective_user.id,)
) as cursor:
row = await cursor.fetchone()
if row and row[0] is not None:
await query.edit_message_text(
"You are already registered with the name"
)
return await start(update, context)
await query.edit_message_text(text="Let's start registration")
await query.message.reply_text("Please, send me your name")
return NAME
elif int(query.data) == CONVERSIONS:
await query.edit_message_text(text="It's conversions:")
states_list = ["START", "PLAYED", "REGISTERED", "PLAYED_AND_REGISTERED"]
number_users = await get_conversions()
message = f"{states_list[0]}"
for i in range(len(states_list) - 1):
conversion = round(number_users[i + 1] / number_users[i] * 100, 2)
message += f"\n|\n| {conversion}%\nv\n{states_list[i+1]}"
await context.bot.send_message(
chat_id=update.effective_chat.id,
text=message,
)
return await start(update, context)
elif int(query.data) == LEADER_BOARD:
await query.edit_message_text(text="There are all users:")
await get_users(query, context)
return await start(update, context)
elif int(query.data) == MAIL:
await query.edit_message_text(text="Send me message and I'll send it everyone")
return GET_MAIL
elif int(query.data) == YES_MAIL:
await send_mail(query, context)
return await start(update, context)
elif int(query.data) == NO_MAIL:
return await start(update, context)
else:
await context.bot.delete_message(
chat_id=update.effective_chat.id,
message_id=update.effective_message.message_id,
)
return await start(update, context)
async def save_name(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
name = update.effective_message.text
user_id = update.effective_user.id
await update.message.reply_text(f"Thank you for sharing your name: {name}")
async with aiosqlite.connect("bot.db") as db:
async with db.execute(
"SELECT user_id FROM users WHERE user_id = ?", (user_id,)
) as cursor:
row = await cursor.fetchone()
if row and row[0]:
await db.execute(
"""
UPDATE users
SET name = ?
WHERE user_id = ?
""",
(name, user_id),
)
else:
await db.execute(
"""
INSERT INTO users (user_id, name)
VALUES (?, ?)
""",
(user_id, name),
)
await db.commit()
contact_button = KeyboardButton(
text="Share your phone number", request_contact=True
)
reply_markup = ReplyKeyboardMarkup(
[[contact_button]], one_time_keyboard=True, resize_keyboard=True
)
await update.message.reply_text(
"Please, send me your phone number:", reply_markup=reply_markup
)
return PHONE
async def save_phone(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
phone_number = update.message.contact.phone_number
user_id = update.effective_user.id
await update.message.reply_text(
f"Thank you for sharing your phone number: {phone_number}"
)
async with aiosqlite.connect("bot.db") as db:
async with db.execute(
"SELECT conversation_status FROM users WHERE user_id = ?", (user_id,)
) as cursor:
current_status = await cursor.fetchone()
new_status = current_status[0]
if current_status[0] == PLAYED:
new_status = PLAYED_AND_REGISTERED
elif current_status[0] == START:
new_status = REGISTERED
await db.execute(
"""
UPDATE users
SET phone = ?, conversation_status = ?
WHERE user_id = ?
""",
(phone_number, new_status, user_id),
)
await db.commit()
return await start(update, context)
async def get_users(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
async with aiosqlite.connect("bot.db") as db:
async with db.execute(
"SELECT user_id, wins, defeats, name, phone, conversation_status FROM users"
) as cursor:
rows = await cursor.fetchall()
messages = "\n".join(
[
f"{row[0]}: ({row[1]} - {row[2]}) - {row[3]} - {row[4]} #{row[5]}"
for row in rows
]
)
if len(messages) != 0:
await update.message.reply_text(
"user_id: (wins - defeats) - name - phone #conversationStatus"
)
await update.message.reply_text(messages)
else:
await update.message.reply_text("- no users -")
async def get_mail(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
msg = update.effective_message.text
keyboard = [
[
InlineKeyboardButton("Yes", callback_data=YES_MAIL),
InlineKeyboardButton("No", callback_data=NO_MAIL),
]
]
context.user_data["msg"] = msg
await context.bot.send_message(
chat_id=update.effective_chat.id,
text=f"Send it:\n\n{msg}",
reply_markup=InlineKeyboardMarkup(keyboard),
parse_mode="MarkdownV2",
)
return CHOOSING
async def send_mail(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
msg = context.user_data.get("msg", "no message found")
async with aiosqlite.connect("bot.db") as db:
async with db.execute("SELECT user_id FROM users") as cursor:
async for row in cursor:
await context.bot.send_message(
chat_id=row[0], text=msg, parse_mode="MarkdownV2"
)
async def get_conversions():
db = await aiosqlite.connect("bot.db")
number_users = []
statuses = [START, PLAYED, REGISTERED, PLAYED_AND_REGISTERED]
for status in statuses:
total_users_with_status = await db.execute(
"""SELECT COUNT(*) FROM users WHERE conversation_status >= ?""", (status,)
)
total_users_with_status = await total_users_with_status.fetchone()
total_users_with_status = total_users_with_status[0]
number_users.append(total_users_with_status)
return number_users
def remove_job_if_exists(name: str, context: ContextTypes.DEFAULT_TYPE) -> bool:
current_jobs = context.job_queue.get_jobs_by_name(name)
if not current_jobs:
return False
for job in current_jobs:
job.schedule_removal()
return True
async def daily_job_progrev(context: ContextTypes.DEFAULT_TYPE) -> None:
async with aiosqlite.connect("bot.db") as db:
async with db.execute("SELECT user_id, name FROM users") as cursor:
rows = await cursor.fetchall()
for row in rows:
print(row)
if row[1]:
await context.bot.send_message(
chat_id=row[0], text=f"Hello, {row[1]}, let's play!"
)
else:
await context.bot.send_message(
chat_id=row[0], text="Hello, let's play!"
)
async def once_job_step(context: ContextTypes.DEFAULT_TYPE) -> None:
job = context.job
await context.bot.send_message(chat_id=job.chat_id, text="Go ahead))")
async def cancel(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
await update.message.reply_text("Bye!", reply_markup=ReplyKeyboardRemove())
return await start(update, context)
def main() -> None:
print("MAIN")
application = Application.builder().token(BOT_TOKEN).build()
conv_handler = ConversationHandler(
entry_points=[CommandHandler("start", start)],
states={
CHOOSING: [
CallbackQueryHandler(choosing_callback),
],
CHOOSE_ACTION: [
MessageHandler(filters.Regex("^(Rock|Paper|Scissors)$"), check_winner),
],
NAME: [MessageHandler(filters.TEXT & ~filters.COMMAND, save_name)],
PHONE: [MessageHandler(filters.CONTACT, save_phone)],
GET_MAIL: [MessageHandler(filters.TEXT & ~filters.COMMAND, get_mail)],
},
# always handles
fallbacks=[CommandHandler("cancel", cancel)],
# per_message=True,
)
# set correct time zone
application.job_queue.run_daily(
daily_job_progrev,
time=time(hour=17, minute=54, tzinfo=pytz.timezone("Europe/Moscow")),
)
application.add_handler(conv_handler)
application.run_polling(allowed_updates=Update.ALL_TYPES)
if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.run_until_complete(init_db())
main()