-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandlers.py
More file actions
59 lines (52 loc) · 2.35 KB
/
handlers.py
File metadata and controls
59 lines (52 loc) · 2.35 KB
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
from glob import glob
import os
from random import choice
from utils import get_smile, is_cat, main_keyboard, play_random_numbers
def greet_user(update, context):
print("Вызван /start")
context.user_data['emoji'] = get_smile(context.user_data)
update.message.reply_text(
f"Здравствуй, пользователь {context.user_data['emoji']}!",
reply_markup=main_keyboard()
)
def talk_to_me(update, context):
context.user_data['emoji'] = get_smile(context.user_data)
text = update.message.text
print(text)
update.message.reply_text(f"{text} {context.user_data['emoji']}", reply_markup=main_keyboard())
def guess_number(update, context):
print(context.args)
if context.args:
try:
user_number = int(context.args[0])
message = play_random_numbers(user_number)
except (TypeError, ValueError):
message = "Введите целое число"
else:
message = "Введите число"
update.message.reply_text(message, reply_markup=main_keyboard())
def send_cat_picture(update, context):
cat_photo_list = glob('images/cat*.jp*g')
cat_photo_filename = choice(cat_photo_list)
chat_id = update.effective_chat.id
context.bot.send_photo(chat_id=chat_id, photo=open(cat_photo_filename, 'rb'), reply_markup=main_keyboard())
def user_coordinates(update, context):
context.user_data['emoji'] = get_smile(context.user_data)
coords = update.message.location
update.message.reply_text(
f"Ваши координаты {coords} {context.user_data['emoji']}!",
reply_markup=main_keyboard()
)
def check_user_photo(update, context):
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 is_cat(file_name):
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)