-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
88 lines (65 loc) · 2.43 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
import asyncio
import os
import logging
from aiogram import Bot, types
from aiogram.dispatcher import Dispatcher
from aiogram.types import ContentType
from aiogram.utils import executor
default_logging_format = (
'%(asctime)s %(levelname)-5.5s %(name)s %(module)s:%(funcName)s:%(lineno)d %(message)s'
)
logging.basicConfig(
level=logging.INFO,
format=default_logging_format,
)
try:
from dotenv import load_dotenv
load_dotenv()
except ImportError:
logging.info('Skipping .env loading')
pass
bot = Bot(token=os.environ.get('TOKEN'))
dp = Dispatcher(bot)
TMP_DIR = '/tmp'
@dp.message_handler(commands=['start'])
async def process_start_command(msg: types.Message):
await msg.reply("Привет!\nЗапиши мне что нибудь!")
logging.info(f'Bot started for {msg.from_user.username}')
@dp.message_handler(commands=['help'])
async def process_help_command(msg: types.Message):
await msg.reply("Запиши мне войс или видео и я нормализую его громкость")
@dp.message_handler(content_types=[ContentType.VOICE, ContentType.VIDEO_NOTE])
async def voice_message_handler(msg: types.Message):
await bot.send_message(
msg.from_user.id,
reply_to_message_id=msg.message_id,
text='Ожидайте результата...',
)
msg_content = getattr(msg, msg.content_type)
bot_send_content = getattr(bot, f'send_{msg.content_type}')
container_fmt = {
'voice': 'ogg',
'video_note': 'mp4',
}[msg.content_type]
logging.info(
f'Processing {msg.content_type} file size {msg_content.file_size} '
f'for {msg.from_user.username}'
)
in_name = f'{TMP_DIR}/{msg_content.file_id}'
out_name = f'{TMP_DIR}/{msg_content.file_id}-normalized.{container_fmt}'
await bot.download_file_by_id(msg_content.file_id, in_name)
# todo processing pool / queue to limit simultaneous processing
# todo pipe to avoid files
proc = await asyncio.create_subprocess_exec(
'ffmpeg-normalize', '-v', in_name, '-c:a', 'libopus',
'-o', out_name, '-ofmt', container_fmt,
)
await proc.wait()
normalized = types.InputFile(out_name)
await bot_send_content(msg.from_user.id, normalized)
# cleanup
os.unlink(in_name)
os.unlink(out_name)
logging.info(f'Handled {msg.content_type} message for {msg.from_user.username}')
if __name__ == '__main__':
executor.start_polling(dp)