python - Telegram Telethon:如果您知道频道哈希,如何获取消息
问题描述
当我必须处理这种链接时:https ://t.me/channelName 我通常会从这样的频道收到消息:
async def main():
channel = await client.get_entity('channelName')
messages = await client.get_messages(channel, limit = N )
#...Code etc..
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
如果您的链接是这样的,您如何收到消息? https://t.me/joinchat/DDDDDxxxxAAAA
我知道'DDDDDxxxxAAAA'是channel_hash,所以我通过电报客户端加入了频道并尝试获取频道ID和消息对象:
channel_hash = "DDDDDxxxxAAAA"
channel = await client(functions.messages.CheckChatInviteRequest(hash=channel_hash))
现在,频道对象包含频道 ID 和许多其他字段。例如:(chat=Channel(id=123456789,...etcc 所以,我试过这样:
messages = await client.get_messages(channel, limit = N )
但它返回:
Traceback (most recent call last):
File "C:\Users\****\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\telethon\sessions\memory.py", line 192, in get_input_entity
return utils.get_input_peer(key)
File "C:\Users\****\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\telethon\utils.py", line 235, in get_input_peer
_raise_cast_fail(entity, 'InputPeer')
File "C:\Users\****\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\telethon\utils.py", line 138, in _raise_cast_fail
raise TypeError('Cannot cast {} to any kind of {}.'.format(
TypeError: Cannot cast ChatInviteAlready to any kind of InputPeer.
TypeError: Cannot cast ChatInviteAlready to any kind of InputPeer.
如果您只知道频道哈希,如何获取消息?感谢帮助
解决方案
推荐阅读
- html - VS Code 不再识别 html 文件
- sql - 展开数据库时看不到表,但查询时返回结果
- react-native - 如何获取 Scrollview 项目 React-native 的索引?
- vb.net - 为什么我收到“无法将列表视图转换为整数”的错误消息。?
- swift - RxSwift:compactMap 从未执行
- aws-amplify-cli - 放大 CLI:推送操作期间发生错误:无法读取 null 的属性“扩展”
- unity3d - Unity 说我的密钥库密码错误
- java - 如何根据构造函数中设置的 ID 返回某个对象?
- python - 将多个没有类名的html标签解析为不同的变量
- c - 分叉程序请查看