Add chat_id in ChatMemberHandler to Filter Specific Chat(s) #4287 by uniquetrij · Pull Request #4290 · python-telegram-bot/python-telegram-bot

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>)
)