与电报机器人交互的机器人

Bot which interacts with telegram bots

我想知道是否可以自动从其他电报机器人下载文件。我在网上搜索了如何制作一个 Python 机器人(或用 Python 编写的 Telegram 机器人)来执行此操作,但我没有找到任何东西。有人可以帮助我吗?

无法在电报中直接与机器人交互,因为机器人 API 不支持。 但是您可以使用 MTProto 库来自动执行几乎所有与机器人的交互(包括文件下载)。因为这些库只是简单地自动化普通用户帐户,所以它们没有机器人的限制 api.

这是一个使用 telethon lib 下载文件的示例:

from telethon import TelegramClient, events

api_id = <API_ID>
api_hash = '<API_HASH>'
client = TelegramClient('session', api_id, api_hash)

BOT_USER_NAME="@filesending_sample_bot"  # the username of the bot that sends files (images, docs, ...)

@client.on(events.NewMessage(func=lambda e: e.is_private))
async def message_handler(event):
    if event.message.media is not None: # if there's something to download (media)
        await client.download_media(message=event.message, )

async def main():
    await client.send_message(BOT_USER_NAME, 'some text or command to trigger file sending') # trigger the bot here by sending something so that the bot sends the media


client.start()
client.loop.run_until_complete(main())
client.run_until_disconnected()

在我的示例中,我在 javascript 中创建了一个最小的电报机器人,它在收到任何消息时发送一张照片(作为文档)来测试上面的脚本(但是你配置上面的脚本以匹配你的情况):

const bot = new (require("telegraf"))(<MY_BOT_TOKEN>);
bot.on("message", (ctx) => ctx.replyWithDocument("https://picsum.photos/200/300"));
bot.launch();

请注意,您必须使用常规电报帐户(不是使用机器人令牌而是 phone 号码)进行连接才能正常工作。如果必须使用电报机器人,您可以在后台使用脚本(通过 运行 将其作为新进程或作为 REST api,等等...)和 return电报机器人的结果。