如何使用 telegram bot API 发送带有文本的图像?

Lau*_*ura 0 python api telegram

我正在尝试使用 Telegram API 机器人向 telegram 发送消息。

我想发出 GET 请求,将文本和图像发送到我的电报频道。

现在 fetch 看起来像这样:

telegram_msg = requests.get('https://api.telegram.org/<botname>:botAPI/sendMessage?chat_id=<chat_id>=some text')
Run Code Online (Sandbox Code Playgroud)

如何包含位于此处的图像并在一条消息中同时发送文本和图像?

感谢您的帮助。

UPD:我可以使用 sendPhoto 发送图像,但是如何将这两个请求合并为一个?这样我就可以同时发送图像和文本吗?

小智 5

使用@BotFather获取您的API令牌后

import requests
chat_id = '<chat_id>'
token = '<token>'
msg = "Send text with photo "
img_uri = "https://www.ixbt.com/img/n1/news/2022/3/1/62342d1404eb2_large.jpg"
telegram_msg = requests.get(f'https://api.telegram.org/bot{token}/sendPhoto?chat_id={chat_id}&caption={msg}&photo={img_uri}')
print(telegram_msg)
print(telegram_msg.content)
Run Code Online (Sandbox Code Playgroud)

在 Telegram 中发布包含文本和图像的结果

  • 可以发本地照片吗?例如从本地驱动器/NAS 等上传。 (2认同)