【发布时间】:2020-02-28 02:26:56
【问题描述】:
我想创建一个小脚本,用于从公共频道获取 Telegram 文本(我不是该频道的管理员)。
我在这里发现了另一个问题:
Read the messages of the public channels from Telegram
我已尝试使用答案中所述的 Telethon,但它不起作用:
from telethon.tl.functions.contacts import ResolveUsernameRequest
import telethon
client = telethon.TelegramClient("session.txt", api_id=XYZ, api_hash='XYZ')
client.connect()
response = client.invoke(ResolveUsernameRequest("test"))
print(response.channel_id)
print(response.access_hash)
抛出此错误:
C:/Users/mypc/PycharmProjects/untitled/aa.py:5: RuntimeWarning: coroutine 'TelegramBaseClient.connect' was never awaited
client.connect()
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
Traceback (most recent call last):
File "C:/Users/mypc/PycharmProjects/untitled/aa.py", line 6, in <module>
response = client.invoke(ResolveUsernameRequest("test"))
AttributeError: 'TelegramClient' object has no attribute 'invoke'
我已尝试阅读 API 文档,但我并没有完全理解这些调用的工作原理:
https://core.telegram.org/method/channels.exportMessageLink
如果有人能向我解释这些是如何工作的,我将不胜感激。
【问题讨论】:
-
client.invoke(request)是旧方法。您需要使用client(request)。
标签: python-3.x telegram channel telethon