Skip to content
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

Add chat_id in ChatMemberHandler to Filter Specific Chat(s) #4287 #4290

Conversation

uniquetrij
Copy link
Contributor

@uniquetrij uniquetrij commented Jun 2, 2024

closes #4287

Added a chat_id param to ChatMemberHandler to filter updates only from specified chat(s).

So, now instead of having to filter chat_id within the callback methods as the following,

async def member_callback_1(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
    if update.chat_member.chat.id != <chat_id_1>:
        return
    ...

async def member_callback_2(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
    if update.chat_member.chat.id != <chat_id_2>:
        return
    ...

you can now pass chat_id while declaring your ChatMemberHandler.

application.add_handler(
    ChatMemberHandler(member_callback_1, ChatMemberHandler.CHAT_MEMBER, chat_id=<chat_id_1>)
)

application.add_handler(
    ChatMemberHandler(member_callback_2, ChatMemberHandler.CHAT_MEMBER, chat_id=<chat_id_2>)
)

@Bibo-Joshi Bibo-Joshi self-requested a review June 2, 2024 18:59
Copy link
Member

@Bibo-Joshi Bibo-Joshi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the PR :) I left some comments below. Please also have a look at the failing doc build test. We can check the other tests after that if needed.

Comment on lines 133 to 134
def __is_chat_restricted(self, update: Update) -> bool:
"""Checks if the handler is chat ID restricted and doesn't match with update's chat ID."""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally find the negation a bit confusing. I would find it more intuitive to have a function __is_chat_allowed

Copy link
Contributor Author

@uniquetrij uniquetrij Jun 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had to introduce this function only because pylint was failing with so many if conditions in check_update. Your suggestion to use update.effective_chat instead resolved it. So moved the condition back to check_update and removed this function altogether. Fixed the other issues as well. Thanks for your review and suggestions. Let me know if everything looks okay now :)

@uniquetrij uniquetrij requested a review from Bibo-Joshi June 4, 2024 17:57
Copy link
Member

@Bibo-Joshi Bibo-Joshi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the updates, LGTM now :)
before merging, let me just check if someone else from the dev team wants to review.

@harshil21 harshil21 self-requested a review June 5, 2024 23:33
Copy link
Member

@harshil21 harshil21 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@Bibo-Joshi Bibo-Joshi merged commit 9e70ac8 into python-telegram-bot:master Jun 6, 2024
22 checks passed
@Bibo-Joshi
Copy link
Member

Thank you for the nice contribution!

@uniquetrij uniquetrij deleted the chatmemberhandler-filter-child-id branch June 6, 2024 19:29
@github-actions github-actions bot locked and limited conversation to collaborators Jun 14, 2024
@Bibo-Joshi Bibo-Joshi added 🔌 enhancement pr description: enhancement and removed enhancement labels Nov 3, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
🔌 enhancement pr description: enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[FEATURE] Add chat_id in ChatMemberHandler to Filter Specific Chat(s)
3 participants