Commit 98dbaf27 authored by Aryan Noshirvnai's avatar Aryan Noshirvnai

Fix chat music scan crashing with "'str' object has no attribute 'value'"

get_chat_music() called search_messages(filter="audio") - a plain
string - but Pyrogram's filter param is a MessagesFilter enum and
internally reads filter.value, so every chat scan crashed the moment
a user tapped a chat. Use MessagesFilter.AUDIO instead. Added a unit
test that mocks pyrogram.Client and asserts the exact enum is passed,
since the existing router tests only mocked get_chat_music() itself
and never exercised this line.
Co-Authored-By: 's avatarClaude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019i3NAUkLkHNbsPg5vKfzF9
parent 0414a226
Pipeline #1107 passed with stage
in 12 minutes and 33 seconds
from pyrogram import Client
from pyrogram.enums import ChatType
from pyrogram.enums import ChatType, MessagesFilter
import os
API_ID = int(os.getenv("TELEGRAM_API_ID", "2040"))
......@@ -45,7 +45,7 @@ async def get_chat_music(session_string: str, chat_id: int, limit: int = 50):
music = []
try:
async for message in client.search_messages(chat_id, filter="audio", limit=limit):
async for message in client.search_messages(chat_id, filter=MessagesFilter.AUDIO, limit=limit):
if message.audio:
music.append({
"message_id": message.id,
......
import asyncio
from pyrogram.enums import MessagesFilter
from src.telegram_worker import telegram_sync
class _FakeAudio:
def __init__(self, title="Test Song", performer="Test Artist", duration=180, file_name="test.mp3"):
self.title = title
self.performer = performer
self.duration = duration
self.file_name = file_name
class _FakeMessage:
def __init__(self, id_, audio):
self.id = id_
self.audio = audio
class _FakeClient:
"""Stands in for pyrogram.Client - records what get_chat_music/get_chats actually call
it with, so a regression like passing filter="audio" (a plain string) instead of
MessagesFilter.AUDIO (what search_messages actually requires) fails a test instead of
only surfacing live as "'str' object has no attribute 'value'"."""
last_search_kwargs = None
def __init__(self, *args, **kwargs):
pass
async def connect(self):
pass
async def disconnect(self):
pass
def search_messages(self, chat_id, query="", offset=0, filter=None, limit=0, from_user=None):
_FakeClient.last_search_kwargs = {"chat_id": chat_id, "filter": filter, "limit": limit}
async def _gen():
yield _FakeMessage(1, _FakeAudio())
yield _FakeMessage(2, None) # non-audio message should be skipped
return _gen()
def test_get_chat_music_filters_with_the_messages_filter_enum_not_a_string(mocker):
mocker.patch("src.telegram_worker.telegram_sync.Client", _FakeClient)
result = asyncio.run(telegram_sync.get_chat_music("session-string", chat_id=123, limit=10))
assert _FakeClient.last_search_kwargs == {"chat_id": 123, "filter": MessagesFilter.AUDIO, "limit": 10}
assert result == [
{
"message_id": 1,
"title": "Test Song",
"artist": "Test Artist",
"duration": 180,
"file_name": "test.mp3",
}
]
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment