简体   繁体   English

我如何处理在 Telethon 中从组到超级组的迁移?

[英]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.我在#telethon 电报群中问了这个问题,但没有得到足够的答案。 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.我只是完全不明白,虽然这是因为较旧的 Telethon 版本(1.12),因为较新的 Telethon 版本正在发生层更改(因此其他定义在安装时 setup.py 中内置)但我认为这是不同的问题。 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.这应该在每个聊天事件上打印一条消息并检测迁移到超级组等并打印旧 ID 和新 ID。

To get other actions you have to filter and process them also...要获得其他操作,您还必须过滤和处理它们......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Telethon 中:当我不确定我的群组/频道是私人还是公共时,如何验证 ChatAction 是否仅来自我的群组/频道? - In Telethon: How can I verify If ChatAction was only from my group/channel, when I am not sure If my group/channel is private or public? 如何使用 Telethon 库从频道接收消息? - How can I receive messages from a channel using the Telethon library? 如何使用 Telethon 将照片发送给电报用户 - How can I send photo to telegram user with telethon 如何使用 Telethon 向我的私人电报频道发送消息? - How can I send messages to my private telegram channel with Telethon? 如何在 python 中使用 Telethon 发送带有标题的图像? - How can I send an image with a caption using Telethon in python? 如何避免将“全局”与 asyncio(电视节目)一起使用? - How I can avoid using "global" with asyncio (telethon)? 我如何识别电视节目事件更新程序上的机器人 - How can I Identify a bot on telethon event updater Telethon python:我怎样才能正确地为新消息使用模式? - Telethon python: how can i use pattern correctly for new message? 如何仅在 Telethon 对话中处理 NewMessage - How to handle NewMessage only in Telethon conversation 如何使用 Telethon 获取特定私人频道的更新? - How can I get updates for a specific private channel using telethon?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM