2

I need my bot to do specific things when an user is joining end exiting a group. First, I write proof-of-concept code:

@dp.chat_member_handler()
async def user_joined_chat(update: types.ChatMemberUpdated):
    print('Users changed')

But that does nothing. I added-deleted the test user to the test group many times, but nothing. Of course, I have made sure "privacy mode" is disabled and the bot is an administrator of the group before.

What's wrong? Do I use wrong handler?

1 Answers1

2

You have to use other handler, handler that you are trying to use handles ChatMember status changes. You have to use classic message_handler and handle content types like: NEW_CHAT_MEMBERS and LEFT_CHAT_MEMBER you can find such types here so there is working code:

@dp.message_handler(content_types=[types.ContentType.NEW_CHAT_MEMBERS, types.ContentType.LEFT_CHAT_MEMBER])
async def user_joined_chat(message: types.Message):
    print('Users changed')
pryvit
  • 73
  • 6
  • Is it possible to get the old and new statuses of user from a message? – ShkiperDesna Jul 10 '22 at 11:26
  • User object doesnt has 'status', but ChatMember has 'status'. From chat_member_handler() you receive [Update object](https://core.telegram.org/bots/api#update:~:text=by%20the%20user.-,chat_member,-ChatMemberUpdated) from chat_member field you can get [ChatMemberUpdated](https://core.telegram.org/bots/api#chatmemberupdated:~:text=is%20banned%20forever-,ChatMemberUpdated,-This%20object%20represents) object and there are previous information about the chat member and new one – pryvit Jul 10 '22 at 13:49