Skip to content

Commit fe3aa67

Browse files
committed
v0.2.9 - Fix cost processing and enhance attachment download handling
router.py - Fix type consistency in total_cost and operation_costs conversions - Clarify bot instruction text formatting utils/download_attachment.py - Handle TelegramBadRequest for large files explicitly, removing generic exception handling utils/download_attachment_pyrogram.py - Update default target_dir path to "./downloads/"
1 parent 69ed3c0 commit fe3aa67

File tree

4 files changed

+26
-27
lines changed

4 files changed

+26
-27
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "new-whisper-bot"
3-
version = "0.2.8"
3+
version = "0.2.9"
44
description = ""
55
authors = ["Petr Lavrov <calmmage@gmail.com>"]
66
readme = "README.md"

src/router.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,7 @@ async def start_handler(message: Message, app: App):
2626
You can also <b>reply</b> to any text message from me to chat about its transcript or summary with LLM.
2727
"""
2828
)
29-
await send_safe(
30-
message.chat.id,
31-
welcome_message
32-
)
29+
await send_safe(message.chat.id, welcome_message)
3330

3431

3532
@commands_menu.botspot_command("help", "Show this help message")
@@ -63,14 +60,13 @@ async def help_handler(message: Message, app: App):
6360
So you might need to explore that as well to understand it. Basic template is available <a href="https://github.com/calmmage/botspot-template">here</a>
6461
"""
6562
)
66-
await send_safe(
67-
message.chat.id,
68-
help_message
69-
)
63+
await send_safe(message.chat.id, help_message)
64+
7065

7166
class Language(BaseModel):
7267
language_code: str
7368

69+
7470
@router.message(F.audio | F.voice | F.video | F.document | F.video_note)
7571
async def media_handler(message: Message, app: App, state: FSMContext):
7672
assert message.from_user is not None
@@ -99,7 +95,7 @@ async def media_handler(message: Message, app: App, state: FSMContext):
9995
notif = await reply_safe(
10096
message, "Processing your media file. Estimating transcription time..."
10197
)
102-
98+
10399
# Transcribe the audio
104100
transcription = await app.process_message(
105101
message,
@@ -128,9 +124,9 @@ async def media_handler(message: Message, app: App, state: FSMContext):
128124

129125
# Get and display cost information
130126
cost_info = await app.get_total_cost(username, message_id=message.message_id)
131-
total_cost = cost_info["total_cost"]
127+
total_cost = float(cost_info["total_cost"])
132128
if total_cost > 0.01:
133-
operation_costs = float(cost_info["operation_costs"])
129+
operation_costs = cost_info["operation_costs"]
134130

135131
cost_breakdown = "\n".join(
136132
[
@@ -150,7 +146,7 @@ async def media_handler(message: Message, app: App, state: FSMContext):
150146

151147
def create_notification_callback(notification_message: Message):
152148
text = notification_message.text
153-
149+
154150
async def callback(update_text: str):
155151
nonlocal text
156152
assert text is not None
@@ -159,6 +155,7 @@ async def callback(update_text: str):
159155

160156
return callback
161157

158+
162159
async def ask_user_language(message: Message, app: App, state: FSMContext):
163160
assert message.from_user is not None
164161
username = message.from_user.username
@@ -190,18 +187,21 @@ async def ask_user_language(message: Message, app: App, state: FSMContext):
190187
await message.reply("No language provided, please retry.")
191188
return
192189
parsed_language: Language = await aquery_llm_structured(
193-
prompt = language_str,
190+
prompt=language_str,
194191
output_schema=Language,
195192
model="gpt-4.1-nano",
196193
system_message="You are a language detection assistant. Your goal is to return the language code in ISO 639-1 format (e.g., 'en' for English, 'ru' for Russian).",
197194
user=username,
198195
)
199196
language_code = parsed_language.language_code
200-
logger.info(f"User input: {language_str}, detected language code: {language_code}")
197+
logger.info(
198+
f"User input: {language_str}, detected language code: {language_code}"
199+
)
201200
else:
202201
language_code = language
203202
return language_code
204203

204+
205205
@router.message()
206206
async def chat_handler(message: Message, app: App):
207207
if message.reply_to_message:
@@ -224,7 +224,9 @@ async def _reply_chat_handler(message: Message, app: App):
224224
prompt = await get_message_text(message, include_reply=True)
225225

226226
# Process chat request
227-
response = await app.chat_about_transcript(full_prompt=prompt, username=username, message_id=message.message_id)
227+
response = await app.chat_about_transcript(
228+
full_prompt=prompt, username=username, message_id=message.message_id
229+
)
228230
response = markdown_to_html(response)
229231

230232
cost = await app.get_total_cost(username, message_id=message.message_id)
@@ -246,6 +248,6 @@ async def _basic_chat_handler(message: Message, app: App):
246248
await answer_safe(
247249
message,
248250
"This is a Whisper bot. "
249-
"\n\nSend an Audio, Video, Voice or Video Note message to transcribe and summarize it.",
251+
"\n\nSend an Audio, Video, Voice or Video Note message to transcribe and summarize it."
250252
"\n\n<b>Reply</b> to a text message from bot to chat about its transcript or summary with LLM",
251253
)

src/utils/download_attachment.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from aiogram.exceptions import TelegramBadRequest
12
from typing import BinaryIO, Optional, Union
23
from pathlib import Path
34
from loguru import logger
@@ -26,15 +27,11 @@ async def download_file(
2627
in_memory=True if in_memory is None else in_memory,
2728
)
2829

29-
except Exception as e: # todo: specify api error
30-
# use pyrogram downloader
31-
logger.warning(
32-
f"Generic error captured, update code to handle specific error, Type: {type(e)}, Error: {e}"
33-
)
34-
import traceback
35-
36-
traceback.print_exc()
37-
30+
except TelegramBadRequest as e:
31+
if "file is too big" in str(e):
32+
logger.info("File is too big, using pyrogram downloader")
33+
else:
34+
raise e
3835
from src.utils.download_attachment_pyrogram import (
3936
download_file_from_aiogram_message,
4037
)

src/utils/download_attachment_pyrogram.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ async def download_file_with_pyrogram(
204204
assert not isinstance(pyrogram_message, list)
205205

206206
if target_dir is None:
207-
target_dir = Path("../../utils")
207+
target_dir = Path("./downloads/")
208208

209209
if file_name is None:
210210
file_name = extract_file_name_from_pyrogram_message(

0 commit comments

Comments
 (0)