Last active
June 23, 2024 22:05
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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