-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhandlers.py
114 lines (93 loc) · 4.52 KB
/
handlers.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
from glob import glob
import os
from random import choice
from db import (db, get_or_create_user, subscribe_user, unsubscribe_user, save_cat_image_vote,
user_voted, get_image_rating)
from jobs import alarm
from utils import has_object_on_image, main_keyboard, play_random_numbers, get_bot_number, cat_rating_inline_keyboard
def greet_user(update, context):
user = get_or_create_user(db, update.effective_user, update.message.chat.id)
print("Вызван /start")
update.message.reply_text(
f"Здравствуй, пользователь {user['emoji']}!",
reply_markup=main_keyboard()
)
def talk_to_me(update, context):
user = get_or_create_user(db, update.effective_user, update.message.chat.id)
text = update.message.text
print(text)
update.message.reply_text(f"{text} {user['emoji']}", reply_markup=main_keyboard())
def guess_number(update, context):
user = get_or_create_user(db, update.effective_user, update.message.chat.id)
if context.args:
try:
user_number = int(context.args[0])
bot_number = get_bot_number(user_number)
message = play_random_numbers(user_number, bot_number)
except (TypeError, ValueError):
message = "Введите целое число"
else:
message = "Введите число"
update.message.reply_text(message, reply_markup=main_keyboard())
def send_cat_picture(update, context):
user = get_or_create_user(db, update.effective_user, update.message.chat.id)
cat_photo_list = glob('images/cat*.jp*g')
cat_photo_filename = choice(cat_photo_list)
chat_id = update.effective_chat.id
if user_voted(db, cat_photo_filename, user['user_id']):
rating = get_image_rating(db, cat_photo_filename)
keyboard = None
caption = f"Рейтинг картинки {rating}"
else:
keyboard = cat_rating_inline_keyboard(cat_photo_filename)
caption = None
context.bot.send_photo(
chat_id=chat_id,
photo=open(cat_photo_filename, 'rb'),
reply_markup=keyboard,
caption=caption
)
def user_coordinates(update, context):
user = get_or_create_user(db, update.effective_user, update.message.chat.id)
coords = update.message.location
update.message.reply_text(
f"Ваши координаты {coords} {user['emoji']}!",
reply_markup=main_keyboard()
)
def check_user_photo(update, context):
user = get_or_create_user(db, update.effective_user, update.message.chat.id)
update.message.reply_text("Обрабатываем фотографию")
os.makedirs("downloads", exist_ok=True)
user_photo = context.bot.getFile(update.message.photo[-1].file_id)
file_name = os.path.join("downloads", f"{user_photo.file_id}.jpg")
user_photo.download(file_name)
if has_object_on_image(file_name, 'cat'):
update.message.reply_text("Обнаружен котик, добавляю в библиотеку")
new_filename = os.path.join("images", f"cat_{user_photo.file_id}.jpg")
os.rename(file_name, new_filename)
else:
update.message.reply_text("Тревога, котик на фото не обнаружен")
os.remove(file_name)
def subscribe(update, context):
user = get_or_create_user(db, update.effective_user, update.message.chat.id)
subscribe_user(db, user)
update.message.reply_text('Вы успешно подписались')
def unsubscribe(update, context):
user = get_or_create_user(db, update.effective_user, update.message.chat.id)
unsubscribe_user(db, user)
update.message.reply_text('Вы успешно отписались')
def set_alarm(update, context):
try:
alarm_seconds = abs(int(context.args[0]))
context.job_queue.run_once(alarm, alarm_seconds, context=update.message.chat.id)
update.message.reply_text(f'Уведомление через {alarm_seconds} секунд')
except (ValueError, TypeError):
update.message.reply_text('Введите целое число секунд после команды')
def cat_picture_rating(update, context):
update.callback_query.answer()
callback_type, image_name, vote = update.callback_query.data.split("|")
vote = int(vote)
user = get_or_create_user(db, update.effective_user, update.effective_chat.id)
save_cat_image_vote(db, user, image_name, vote)
rating = get_image_rating(db, image_name)
update.callback_query.edit_message_caption(caption=f"Рейтинг картинки {rating}")