-
Notifications
You must be signed in to change notification settings - Fork 52.6k
fix(telegram): pass explicit duration to send_voice/send_audio for long clips #36009
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
Closed
liuhao1024
wants to merge
1
commit into
NousResearch:main
from
liuhao1024:fix/telegram-voice-audio-duration
+206
β16
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| """Regression test for issue #36005. | ||
|
|
||
| Telegram's Bot API only auto-derives duration from container metadata for | ||
| short clips. For voice/audio longer than ~4 min 50 s it delivers the message | ||
| with duration 0 unless the sender passes an explicit ``duration`` kwarg. | ||
|
|
||
| This test verifies that: | ||
| 1. ``_probe_audio_duration`` returns a sensible integer for OGG and MP3 files. | ||
| 2. ``TelegramAdapter.send_voice`` passes ``duration`` through to the Bot API | ||
| for both voice (ogg/opus) and audio (mp3/m4a) paths. | ||
| """ | ||
|
|
||
| import os | ||
| import struct | ||
| import tempfile | ||
| from types import SimpleNamespace | ||
| from unittest.mock import AsyncMock, MagicMock, patch | ||
|
|
||
| import pytest | ||
|
|
||
| from gateway.platforms.telegram import _probe_audio_duration | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # _probe_audio_duration unit tests | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
| class TestProbeAudioDuration: | ||
| """Unit tests for the ``_probe_audio_duration`` helper.""" | ||
|
|
||
| def test_returns_none_for_missing_file(self): | ||
| assert _probe_audio_duration("/nonexistent/path.ogg") is None | ||
|
|
||
| def test_ogg_file_size_fallback(self, tmp_path): | ||
| """Without mutagen, falls back to file-size estimate for OGG.""" | ||
| ogg = tmp_path / "voice.ogg" | ||
| # ~100 KB β 100000 / 2000 = 50 seconds | ||
| ogg.write_bytes(b"\x00" * 100_000) | ||
| result = _probe_audio_duration(str(ogg)) | ||
| assert result is not None | ||
| assert result >= 1 | ||
| # Should be roughly 50s (Β±20% tolerance for rounding) | ||
| assert 40 <= result <= 60 | ||
|
|
||
| def test_mp3_file_size_fallback(self, tmp_path): | ||
| """Without mutagen, falls back to file-size estimate for MP3.""" | ||
| mp3 = tmp_path / "audio.mp3" | ||
| # ~256 KB β 256000 / 16000 = 16 seconds | ||
| mp3.write_bytes(b"\x00" * 256_000) | ||
| result = _probe_audio_duration(str(mp3)) | ||
| assert result is not None | ||
| assert result >= 1 | ||
| assert 12 <= result <= 20 | ||
|
|
||
| def test_minimum_duration_is_one(self, tmp_path): | ||
| """Even a tiny file should report at least 1 second.""" | ||
| tiny = tmp_path / "tiny.ogg" | ||
| tiny.write_bytes(b"\x00" * 10) | ||
| result = _probe_audio_duration(str(tiny)) | ||
| assert result is not None | ||
| assert result >= 1 | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Integration: send_voice passes duration | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
| class TestSendVoicePassesDuration: | ||
| """Verify that ``TelegramAdapter.send_voice`` forwards ``duration``.""" | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_voice_path_includes_duration(self, tmp_path): | ||
| """OGG voice calls should include ``duration`` in kwargs.""" | ||
| from gateway.platforms.telegram import TelegramAdapter | ||
| from gateway.config import PlatformConfig, Platform | ||
|
|
||
| ogg = tmp_path / "voice.ogg" | ||
| ogg.write_bytes(b"\x00" * 200_000) # ~100s at 2kB/s | ||
|
|
||
| adapter = object.__new__(TelegramAdapter) | ||
| adapter._bot = MagicMock() | ||
| adapter._reply_to_mode = "quote" | ||
|
|
||
| # Mock internal helpers | ||
| adapter._metadata_thread_id = MagicMock(return_value=None) | ||
| adapter._reply_to_message_id_for_send = MagicMock(return_value=None) | ||
| adapter._thread_kwargs_for_send = MagicMock(return_value={}) | ||
| adapter._notification_kwargs = MagicMock(return_value={}) | ||
|
|
||
| sent_kwargs = {} | ||
|
|
||
| async def _capture_send_voice(**kwargs): | ||
| sent_kwargs.update(kwargs) | ||
| return SimpleNamespace(message_id=42) | ||
|
|
||
| async def _fake_retry(fn, kw, *args, **kwargs): | ||
| return await _capture_send_voice(**kw) | ||
|
|
||
| adapter._send_with_dm_topic_reply_anchor_retry = AsyncMock(side_effect=_fake_retry) | ||
|
|
||
| with patch("os.path.exists", return_value=True): | ||
| result = await adapter.send_voice( | ||
| chat_id="12345", | ||
| audio_path=str(ogg), | ||
| caption=None, | ||
| reply_to=None, | ||
| metadata=None, | ||
| ) | ||
|
|
||
| assert result.success is True | ||
| assert "duration" in sent_kwargs | ||
| assert isinstance(sent_kwargs["duration"], int) | ||
| assert sent_kwargs["duration"] >= 1 | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_audio_path_includes_duration(self, tmp_path): | ||
| """MP3 audio calls should include ``duration`` in kwargs.""" | ||
| from gateway.platforms.telegram import TelegramAdapter | ||
|
|
||
| mp3 = tmp_path / "audio.mp3" | ||
| mp3.write_bytes(b"\x00" * 320_000) # ~20s at 16kB/s | ||
|
|
||
| adapter = object.__new__(TelegramAdapter) | ||
| adapter._bot = MagicMock() | ||
| adapter._reply_to_mode = "quote" | ||
|
|
||
| adapter._metadata_thread_id = MagicMock(return_value=None) | ||
| adapter._reply_to_message_id_for_send = MagicMock(return_value=None) | ||
| adapter._thread_kwargs_for_send = MagicMock(return_value={}) | ||
| adapter._notification_kwargs = MagicMock(return_value={}) | ||
|
|
||
| sent_kwargs = {} | ||
|
|
||
| async def _capture_send_audio(**kwargs): | ||
| sent_kwargs.update(kwargs) | ||
| return SimpleNamespace(message_id=43) | ||
|
|
||
| async def _fake_retry(fn, kw, *args, **kwargs): | ||
| return await _capture_send_audio(**kw) | ||
|
|
||
| adapter._send_with_dm_topic_reply_anchor_retry = AsyncMock(side_effect=_fake_retry) | ||
|
|
||
| with patch("os.path.exists", return_value=True): | ||
| result = await adapter.send_voice( | ||
| chat_id="12345", | ||
| audio_path=str(mp3), | ||
| caption="test", | ||
| reply_to=None, | ||
| metadata=None, | ||
| ) | ||
|
|
||
| assert result.success is True | ||
| assert "duration" in sent_kwargs | ||
| assert isinstance(sent_kwargs["duration"], int) | ||
| assert sent_kwargs["duration"] >= 1 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This is a guessed bitrate rather than a duration probe: OGG/Opus and MP3/M4A can use different or variable bitrates, so this may make Telegram display an incorrect duration. Prefer omitting
durationwhen metadata cannot be read rather than sending an estimate.