python - 我如何处理在 Telethon 中从组到超级组的迁移?
问题描述
我在#telethon 电报群中问了这个问题,但没有得到足够的答案。所以,我得到了支持迁移的“代码”:
@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
但是当我更改组权限或某些用户的权限时它不起作用(例如将其转移到管理员权限)。我只是得到类似的东西:
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))
收听原始事件时:
@client.on(events.Raw)
async def migrate_event(event):
print(event)
print(event.message.action)
我只是完全不明白,虽然这是因为较旧的 Telethon 版本(1.12),因为较新的 Telethon 版本正在发生层更改(因此其他定义在安装时 setup.py 中内置)但我认为这是不同的问题。我可能不知道如何正确编码,我错过了一些知识。
那么,知道如何正确处理它吗?
解决方案
建议的代码正在运行,您只需要对这些值做一些事情......
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))
这应该在每个聊天事件上打印一条消息并检测迁移到超级组等并打印旧 ID 和新 ID。
要获得其他操作,您还必须过滤和处理它们......
推荐阅读
- google-cloud-storage - 使用没有云函数的 GCS 创建事件触发 Dag
- javascript - 网络重新连接后如何从 offline.html 中恢复
- angular - 以角度动态创建表格
- javascript - 这是匹配 javascript 中 15,000 个 URL 主机名的最有效方法吗
- python-3.x - 如何在python中实现nc命令?
- javascript - React 问题 - 您如何控制网格中特定“图块”的状态?
- javascript - 在for循环中使用变量
- r - 如何在 r 中更改 biplot 的加载标签?
- python - 某些运营商的电话号码没有收到来自我的 Python 代码的 SMS 消息?
- node.js - 不能在模块 babel 之外使用 import 语句