-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
105 lines (79 loc) · 3.63 KB
/
main.py
File metadata and controls
105 lines (79 loc) · 3.63 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
from aiogram.utils.text_decorations import MarkdownDecoration
from aiogram.client.default import DefaultBotProperties
from aiogram import Bot, Dispatcher, F, types
from aiogram.filters.command import Command
from aiogram.enums import ParseMode
from dotenv import load_dotenv
from logfire import instrument
import logfire
import logging
import asyncio
import io
import os
from message_tools import MessageTools
from stt import NexaraSTT
import texts
load_dotenv()
TG_TOKEN = os.getenv("TG_TOKEN")
NEXARA_TOKEN = os.getenv("NEXARA_TOKEN")
LOGFIRE_TOKEN = os.getenv("LOGFIRE_TOKEN")
STT = NexaraSTT(NEXARA_TOKEN)
logfire.configure(token=LOGFIRE_TOKEN)
logging.basicConfig(level=logging.INFO)
bot = Bot(token=TG_TOKEN, default=DefaultBotProperties(parse_mode=ParseMode.MARKDOWN_V2))
dp = Dispatcher()
md = MarkdownDecoration()
@dp.message(Command("start"))
@instrument("Processing start command")
async def cmd_start(message: types.Message):
await MessageTools.send_response(message, texts.start_command)
@dp.message(F.voice)
@instrument("Processing voice message")
async def process_voice(message: types.Message, bot: Bot):
"""Handler for voice messages."""
try:
file_info = await bot.get_file(message.voice.file_id)
logfire.info("Downloading voice file")
downloaded_file = await bot.download_file(file_info.file_path)
logfire.info("Starting audio transcription")
data = await STT.audio_analyze(downloaded_file.read())
await MessageTools.send_response(message, md.expandable_blockquote(data)[:-2])
except Exception as e:
logfire.error("Error processing voice message", error=str(e), _exc_info=True)
await MessageTools.send_response(message, texts.stt_error.format(e))
@dp.message(F.audio, F.chat.type == "private")
@instrument("Processing audio file")
async def process_audio_file(message: types.Message, bot: Bot):
"""Handler for audio files for transcription."""
try:
file_info = await bot.get_file(message.audio.file_id)
logfire.info("Downloading audio file")
downloaded_file = await bot.download_file(file_info.file_path)
logfire.info("Starting audio transcription")
data = await STT.audio_analyze(downloaded_file.read())
# Create a new file in memory with the transcription text
temp_file = io.BytesIO(data.encode('utf-8'))
text_file = types.BufferedInputFile(temp_file.read(), filename="transcription.txt")
await MessageTools.send_document(message, text_file)
except Exception as e:
logfire.error("Error processing audio file", error=str(e), _exc_info=True)
await MessageTools.send_response(message, texts.stt_error.format(e))
@dp.message(F.video_note)
@instrument("Processing video note")
async def process_voice(message: types.Message, bot: Bot):
"""Handler for voice messages."""
try:
file_info = await bot.get_file(message.video_note.file_id)
logfire.info("Downloading voice file")
downloaded_file = await bot.download_file(file_info.file_path)
logfire.info("Starting audio transcription")
data = await STT.audio_analyze(downloaded_file.read())
await MessageTools.send_response(message, md.expandable_blockquote(data)[:-2])
except Exception as e:
logfire.error("Error processing voice message", error=str(e), _exc_info=True)
await MessageTools.send_response(message, texts.stt_error.format(e))
async def main():
await dp.start_polling(bot, allowed_updates=dp.resolve_used_update_types())
logfire.info("Bot has been started")
if __name__ == "__main__":
asyncio.run(main())