【发布时间】:2021-01-06 08:21:27
【问题描述】:
我在 python 3.5 中将 telepot python 库与我的机器人一起使用。我想阅读已经在聊天中的消息文本,知道电报聊天的 id 和消息的 id。我该怎么办?
【问题讨论】:
标签: python bots telegram telepot
我在 python 3.5 中将 telepot python 库与我的机器人一起使用。我想阅读已经在聊天中的消息文本,知道电报聊天的 id 和消息的 id。我该怎么办?
【问题讨论】:
标签: python bots telegram telepot
telepot 库是Telegram Bot HTTP API 的包装器,不幸的是,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)
【讨论】: