Skip to content

Commit f1f817f

Browse files
feat(filters): allow the use of filters.{private,group,channel} in callback queries
This works in a similar way to filters.regex.
1 parent 1f8193d commit f1f817f

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

hydrogram/filters.py

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -455,8 +455,14 @@ def media_spoiler_filter(_, __, m: Message):
455455

456456

457457
# region private_filter
458-
def private_filter(_, __, m: Message):
459-
return bool(m.chat and m.chat.type in {enums.ChatType.PRIVATE, enums.ChatType.BOT})
458+
def private_filter(_, __, m: CallbackQuery | Message):
459+
if isinstance(m, Message):
460+
value = m.chat
461+
elif isinstance(m, CallbackQuery):
462+
value = m.message.chat
463+
else:
464+
raise ValueError(f"Private filter doesn't work with {type(m)}")
465+
return bool(value and value.type in {enums.ChatType.PRIVATE, enums.ChatType.BOT})
460466

461467

462468
private = create(private_filter)
@@ -467,8 +473,14 @@ def private_filter(_, __, m: Message):
467473

468474

469475
# region group_filter
470-
def group_filter(_, __, m: Message):
471-
return bool(m.chat and m.chat.type in {enums.ChatType.GROUP, enums.ChatType.SUPERGROUP})
476+
def group_filter(_, __, m: CallbackQuery | Message):
477+
if isinstance(m, Message):
478+
value = m.chat
479+
elif isinstance(m, CallbackQuery):
480+
value = m.message.chat
481+
else:
482+
raise ValueError(f"Group filter doesn't work with {type(m)}")
483+
return bool(value and value.type in {enums.ChatType.GROUP, enums.ChatType.SUPERGROUP})
472484

473485

474486
group = create(group_filter)
@@ -479,8 +491,14 @@ def group_filter(_, __, m: Message):
479491

480492

481493
# region channel_filter
482-
def channel_filter(_, __, m: Message):
483-
return bool(m.chat and m.chat.type == enums.ChatType.CHANNEL)
494+
def channel_filter(_, __, m: CallbackQuery | Message):
495+
if isinstance(m, Message):
496+
value = m.chat
497+
elif isinstance(m, CallbackQuery):
498+
value = m.message.chat
499+
else:
500+
raise ValueError(f"Channel filter doesn't work with {type(m)}")
501+
return bool(value and value.type == enums.ChatType.CHANNEL)
484502

485503

486504
channel = create(channel_filter)

0 commit comments

Comments
 (0)