-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
fix: Telegram voice message format (OGG instead of WAV) causing issues with OpenAI STT API #5389
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
| import re | ||
| import sys | ||
| import uuid | ||
| import os | ||
|
|
||
| from apscheduler.schedulers.asyncio import AsyncIOScheduler | ||
| from telegram import BotCommand, Update | ||
|
|
@@ -21,10 +22,13 @@ | |
| register_platform_adapter, | ||
| ) | ||
| from astrbot.core.platform.astr_message_event import MessageSesion | ||
| from astrbot.core.utils.astrbot_path import get_astrbot_temp_path | ||
| from astrbot.core.utils.media_utils import convert_audio_to_wav | ||
| from astrbot.core.star.filter.command import CommandFilter | ||
| from astrbot.core.star.filter.command_group import CommandGroupFilter | ||
| from astrbot.core.star.star import star_map | ||
| from astrbot.core.star.star_handler import star_handlers_registry | ||
| from astrbot.core.utils.io import download_file, download_image_by_url, file_to_base64 | ||
|
|
||
| from .tg_event import TelegramPlatformEvent | ||
|
|
||
|
|
@@ -375,8 +379,19 @@ async def convert_message( | |
|
|
||
| elif update.message.voice: | ||
| file = await update.message.voice.get_file() | ||
|
|
||
| file_basename = os.path.basename(file.file_path) | ||
| temp_dir = get_astrbot_temp_path() | ||
| temp_path = os.path.join(temp_dir, file_basename) | ||
|
Comment on lines
+383
to
+385
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue (bug_risk): 临时文件仅使用 basename 作为文件名,在并发或重复处理场景下可能发生冲突。 由于 Original comment in Englishissue (bug_risk): Using only the basename for temp files can cause collisions under concurrent or repeated processing. Because |
||
| temp_path = await download_image_by_url(file.file_path, path=temp_path) | ||
| path_wav = os.path.join( | ||
| temp_dir, | ||
| f"{file_basename}.wav", | ||
| ) | ||
| path_wav = await convert_audio_to_wav(temp_path, path_wav) | ||
|
Comment on lines
+384
to
+391
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion (performance): 考虑一下这些临时音频文件在何时以及如何被清理,以避免磁盘空间被占满。 原始下载文件( 推荐实现方式: import os
from astrbot.core.star.star_handler import star_handlers_registry
from astrbot.core.utils.io import download_file, download_image_by_url, file_to_base64 elif update.message.voice:
file = await update.message.voice.get_file()
file_basename = os.path.basename(file.file_path)
temp_dir = get_astrbot_temp_path()
temp_path = os.path.join(temp_dir, file_basename)
path_wav = os.path.join(
temp_dir,
f"{file_basename}.wav",
)
try:
temp_path = await download_image_by_url(file.file_path, path=temp_path)
path_wav = await convert_audio_to_wav(temp_path, path_wav)
message.message = [
Comp.Record(file=path_wav, url=path_wav),
]
finally:
# Best-effort cleanup of temporary audio files to avoid disk buildup
for _path in (temp_path, path_wav):
if _path and os.path.exists(_path):
try:
os.remove(_path)
except OSError:
# Ignore cleanup errors; they shouldn't break message handling
pass这里假设所有需要读取 WAV 文件的处理逻辑都会在同一个调用栈中同步完成(即在 Original comment in Englishsuggestion (performance): Consider how/when these temporary audio files are cleaned up to avoid disk buildup. Both the original download ( Suggested implementation: import os
from astrbot.core.star.star_handler import star_handlers_registry
from astrbot.core.utils.io import download_file, download_image_by_url, file_to_base64 elif update.message.voice:
file = await update.message.voice.get_file()
file_basename = os.path.basename(file.file_path)
temp_dir = get_astrbot_temp_path()
temp_path = os.path.join(temp_dir, file_basename)
path_wav = os.path.join(
temp_dir,
f"{file_basename}.wav",
)
try:
temp_path = await download_image_by_url(file.file_path, path=temp_path)
path_wav = await convert_audio_to_wav(temp_path, path_wav)
message.message = [
Comp.Record(file=path_wav, url=path_wav),
]
finally:
# Best-effort cleanup of temporary audio files to avoid disk buildup
for _path in (temp_path, path_wav):
if _path and os.path.exists(_path):
try:
os.remove(_path)
except OSError:
# Ignore cleanup errors; they shouldn't break message handling
passThis change assumes that any processing that needs to read the WAV file happens synchronously within the same call stack (i.e., before the
Comment on lines
+383
to
+391
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A high-severity Path Traversal vulnerability exists here. The filename from # To prevent path traversal, generate a random filename instead of using the one from the URL.
safe_basename = uuid.uuid4().hex
temp_dir = get_astrbot_temp_path()
# Assume the downloaded voice file is in ogg format as per the PR description.
temp_path = os.path.join(temp_dir, f\"{safe_basename}.ogg\")
await download_image_by_url(file.file_path, path=temp_path)
path_wav = os.path.join(
temp_dir, f\"{safe_basename}.wav\"
)
path_wav = await convert_audio_to_wav(temp_path, path_wav)
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| message.message = [ | ||
| Comp.Record(file=file.file_path, url=file.file_path), | ||
| Comp.Record(file=path_wav, url=path_wav), | ||
| ] | ||
|
|
||
| elif update.message.photo: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
导入了
file_to_base64函数,但根据当前PR的改动,该函数似乎并未在tg_adapter.py文件中使用。建议移除未使用的导入,以保持代码的整洁性。