我在python3.5中使用了telepot python库和我的机器人。我想要阅读已经在聊天中的消息的文本,知道电报聊天的id和消息的id。我该怎么做呢?
发布于 2020-09-20 03:48:33
telepot库是Telegram Bot HTTP API的包装器,不幸的是,目前还没有这样的方法。(所有可用方法的see here for full list)。此外,telepot is no longer actively maintained.
但是,您可以使用基于mtproto protocol的库(如Telethon、Pyrogram、MadelineProto等)直接向电报服务器发出请求(跳过中间HTTP API)。而不是。
下面是一个使用Telethon的例子,让你对此有所了解:
from telethon import TelegramClient
API_ID = ...
API_HASH = ' ... '
BOT_TOKEN = ' ... '
client = TelegramClient('bot_session', API_ID, API_HASH).start(bot_token = BOT_TOKEN)
async def main():
message = await client.get_messages(
-10000000000, # channel ID
ids=3 # message ID
)
print("MESSAGE:\n---\n")
print(message.text)
client.start()
client.loop.run_until_complete(main())[user@pc ~]$ python main.py
MESSAGE:
---
test message您可以通过在my.telegram.org上创建应用程序来获取API_ID和API_HASH的值(有关更详细的说明,请参阅this page )
https://stackoverflow.com/questions/63971937
复制相似问题