-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransliterate_bot.py
More file actions
86 lines (65 loc) · 3.13 KB
/
transliterate_bot.py
File metadata and controls
86 lines (65 loc) · 3.13 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
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
import asyncio
import logging
from aiogram import Bot, types
from aiogram.dispatcher import Dispatcher
from aiogram.utils.markdown import bold, italic, underline
import transliterate
from string import punctuation
from config_reader import config
# Настраиваем логгер
logging.basicConfig(
filename='logfile.log',
level=logging.INFO,
format='[%(asctime)s | %(levelname)s]: %(message)s',
datefmt='%H:%M:%S'
)
# Создаем бота
bot = Bot(config.bot_token.get_secret_value())
dp = Dispatcher(bot)
# Фотографии для отправки сообщения
sad_photo = 'AgACAgIAAxkBAAMQY1adKfoYOb19B2DH9JymxYSsSMQAAqe_MRuLhrhKZyGRIQ5x7RABAAMCAAN4AAMqBA'
dm_photo = 'AgACAgIAAxkBAAMgY1alzAlSXQGnwpOvoZjYyVwYrSUAAsK_MRuLhrhK8o2madweKDUBAAMCAAN4AAMqBA'
# id фото для пересылки сообщения
photo = []
# Функция проверяет корректность ввода
def check_messege(message: str) -> bool:
if [s for s in message if s in '12345']:
return (False, 'В имени или фамилии присутствуют недопустимые символы(')
for char in punctuation:
if char in message:
return (False, 'В имени или фамилии присутствуют недопустимые символы(')
if len(message.split(' ')) != 2:
return(False, 'Вы не ввели имя или фамилию(')
return (True,)
# Функция приветствия
@dp.message_handler(commands=['start'])
async def start_func(message: types.Message):
user_name = message.from_user.full_name
user_id = message.from_user.id
text = 'Привет, напиши свое имя и фамилию и увидишь, что будет)'
await bot.send_photo(message.from_user.id, dm_photo)
await bot.send_message(message.from_user.id, text)
logging.info(f'{user_name=} {user_id=} sent message: {message.text}')
# Если пользователь отправляет фото, бот пересылает это фото
@dp.message_handler(content_types=types.ContentType.PHOTO)
async def resend_photo(message: types.Message):
if message.photo[-1].file_id not in photo:
photo.append(message.photo[-1].file_id)
print(message.photo[-1].file_id)
await bot.send_photo(message.from_user.id, message.photo[-1].file_id)
# Функция переводит кириллицу в латиницу
@dp.message_handler()
async def transliterate_func(message: types.Message):
if check_messege(message.text)[0]:
text = transliterate.translit(message.text, language_code='ru', reversed=True)
await message.reply(text)
else:
text = check_messege(message.text)[1]
await bot.send_photo(message.from_user.id, sad_photo, caption=text)
user_name = message.from_user.username
user_id = message.from_user.id
logging.info(f'{user_name=} {user_id=} received message: {message.text} \nsent message: {text}')
async def main():
await dp.start_polling(bot)
if __name__ == '__main__':
asyncio.run(main())