MCPcopy Index your code
hub / github.com/python-telegram-bot/python-telegram-bot / track_chats

Function track_chats

examples/chatmemberbot.py:65–100  ·  view source on GitHub ↗

Tracks the chats the bot is in.

(update: Update, context: ContextTypes.DEFAULT_TYPE)

Source from the content-addressed store, hash-verified

63
64
65async def track_chats(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
66 """Tracks the chats the bot is in."""
67 result = extract_status_change(update.my_chat_member)
68 if result is None:
69 return
70 was_member, is_member = result
71
72 # Let's check who is responsible for the change
73 cause_name = update.effective_user.full_name
74
75 # Handle chat types differently:
76 chat = update.effective_chat
77 if chat.type == Chat.PRIVATE:
78 if not was_member and is_member:
79 # This may not be really needed in practice because most clients will automatically
80 # send a /start command after the user unblocks the bot, and start_private_chat()
81 # will add the user to "user_ids".
82 # We're including this here for the sake of the example.
83 logger.info("%s unblocked the bot", cause_name)
84 context.bot_data.setdefault("user_ids", set()).add(chat.id)
85 elif was_member and not is_member:
86 logger.info("%s blocked the bot", cause_name)
87 context.bot_data.setdefault("user_ids", set()).discard(chat.id)
88 elif chat.type in [Chat.GROUP, Chat.SUPERGROUP]:
89 if not was_member and is_member:
90 logger.info("%s added the bot to the group %s", cause_name, chat.title)
91 context.bot_data.setdefault("group_ids", set()).add(chat.id)
92 elif was_member and not is_member:
93 logger.info("%s removed the bot from the group %s", cause_name, chat.title)
94 context.bot_data.setdefault("group_ids", set()).discard(chat.id)
95 elif not was_member and is_member:
96 logger.info("%s added the bot to the channel %s", cause_name, chat.title)
97 context.bot_data.setdefault("channel_ids", set()).add(chat.id)
98 elif was_member and not is_member:
99 logger.info("%s removed the bot from the channel %s", cause_name, chat.title)
100 context.bot_data.setdefault("channel_ids", set()).discard(chat.id)
101
102
103async def show_chats(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:

Callers

nothing calls this directly

Calls 2

extract_status_changeFunction · 0.85
setdefaultMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…