[英]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.