Skip to content

Instantly share code, notes, and snippets.

@daviddanielng
Last active June 23, 2024 22:05
Show Gist options
  • Save daviddanielng/bd9bd8e1c417a04c7ed5a1c7443c31fe to your computer and use it in GitHub Desktop.
Save daviddanielng/bd9bd8e1c417a04c7ed5a1c7443c31fe to your computer and use it in GitHub Desktop.
Python Telegram Bot (PTB) check if user if a user is subscribed or a member of a group
async def __is_member_of_group(self, userId: int, bot: Bot) -> bool:
try:
aa = await bot.get_chat_member("groupIdOrUsername", user_id=userId)
status = aa.status.lower()
match status:
case "member" | "administrator" | "owner" | "creator":
return True
case "restricted":
await bot.send_message(
chat_id=userId,
text="You are restricted from using this bot",
)
return False
case "banned":
await bot.send_message(
chat_id=userId, text="You are banned from using this bot"
)
return False
case "left":
await bot.send_message(
chat_id=userId,
text=f"You must be a member of <a href='https://t.me/groupusername'>groupname</a> group to use this bot.",
parse_mode="HTML",
)
return False
case _:
await bot.send_message(
chat_id=userId,
text=f"You must be a member of <a href='https://t.me/groupusername'>groupname</a> group to use this bot.",
parse_mode="HTML",
)
return False
except Exception as e:
self.logger.exception(f"Error: {e}")
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment