How to track group ID change to supergroup in aiogram 2.x

71 Views Asked by At

My telegram bot works in chat groups and saves known user data and assigns a group ID to it. When the group type changes, the ID of the group also changes, so the code cannot find the corresponding users in the database.

I checked the aiogram documentation for the current version and didn't find anything useful, but I found some solutions on python-telegram-bot:

def migchat(bot, update):
    oldchatid = update.message.migrate_from_chat_id
    newchatid = update.message.chat.id
    # code access db to change id's.

dispatcher.add_handler(MessageHandler(Filters.status_update.migrate, migchat))

But I don’t understand at all how this can be implemented on aiogram. Have you encountered a similar situation?

1

There are 1 best solutions below

0
latteisuser On

I found a solution to the problem by reading the documentation on the Telegram API and aiogram. We can use a message hendler with a certain content type. The solution is attached below, it instantly registers the migration and proceeds from there.

@dp.message_handler(content_types=types.ContentType.MIGRATE_TO_CHAT_ID)
async def migrate_event(message: types.Message):
    """
    event new incoming message, filtered by MigrateToChatID
    :param message: message object
    """
    if message.migrate_to_chat_id:
        old_id, new_id = message.chat.id, message.migrate_to_chat_id
        # writing new values to db.

If that didn't work for you, I suggest reading about how telegram works here, and about aiogram here.