简体   繁体   中英

How can I handle migration from group to supergroup in telethon?

I asked the question in #telethon telegram group, but don't got adequate answer. So, I got "code" for supporting migrations:

@client.on(events.Raw(types.UpdateNewChannelMessage))
async def migrate_event(event: types.UpdateNewChannelMessage):
    if event.message:
        if isinstance(event.message.action, types.MessageActionChannelMigrateFrom):
            was_chat_id = -1 * event.message.action.chat_id
            new_id = int('-100' + str(event.message.to_id.channel_id))
            raise events.StopPropagation

But it doesn't work when I change group permissions or permission of certain user (for eg transfer it to admin rights). I just get something like that:

UpdateChatParticipants(participants=ChatParticipants(chat_id=496384744, participants=[ChatParticipant(user_id=849120097, inviter_id=382775249, date=datetime.datetime(2020, 8, 18, 8, 28, 30, tzinfo=datetime.timezone.utc)), ChatParticipant(user_id=1038400746, inviter_id=382775249, date=datetime.datetime(2020, 8, 18, 8, 28, 30, tzinfo=datetime.timezone.utc)), ChatParticipantCreator(user_id=382775249)], version=1))

When listening to raw events:

@client.on(events.Raw)
async def migrate_event(event):
    print(event)
    print(event.message.action)

I just quite don't get that, I though it's because of older Telethon version (1.12) since newer Telethon versions are getting layer changes (so other definitions are built in setup.py on install) but I see it's different issue. I probably don't know how to code that properly and I miss some knowledge.

So, any idea how to handle it correctly?

The proposed code is working, you simply need to do something with the values...

from telethon import TelegramClient, events, types
...
@client.on(events.Raw(types.UpdateNewChannelMessage))
async def migrate_event(event: types.UpdateNewChannelMessage):
    """
    event new incoming message, filtered by MessageActionChannelMigrateFrom

    :param param1: event is the message object, types type of  action
    """
    if event.message:
        if event.message.action is not None:
            print('=========== Chat Action  ===============')
            if isinstance(event.message.action, types.MessageActionChannelMigrateFrom):
                was_chat_id = -1 * event.message.action.chat_id
                new_id = int('-100' + str(event.message.to_id.channel_id))
                print('############ CHAT MIGRATION ############')
                print('From_id: ' + str(was_chat_id) + 'To_id: ' + str(new_id))

This should print a message on every chat event and detect migrate to supergroup etc. and print old and new ID.

To get other actions you have to filter and process them also...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM