Den*_*nis 6 python telegram python-telegram-bot telegram-bot
我目前正在使用python-telegram-bot,基本上我想用它实现的是发送这样的电报消息:
因此,该消息包含 2 张以上的照片/视频,下方带有文本消息。
我已经尝试过的:
使用send_message方法发送消息,并包括照片 URL,但它只显示文本下方的 1 张图片
使用send_media_group发送媒体组,但此方法没有caption作为send_photo 的参数。
Sle*_*ker 21
您应该使用sendMediaGroup,您可以在其中指定media带有照片/视频对象数组的字段,但诀窍是caption仅为数组的第一个元素设置属性。在这种情况下,电报将在媒体内容下方显示该标题。
如果您要为多个元素指定标题,则电报将仅在您分别单击每张照片的照片预览时显示它们。
send_media_group有效,但必须在创建media_group和 仅第一张图像时添加标题。假设我们有三个图像img0.png,img1.png并且img2.png,我们将它们添加到media_groupusing InputMediaPhoto,参数caption等于我们只想为第一张图像发送的文本,否则我们设置caption等于''。
import telegram
from telegram import InputMediaPhoto
TOKEN = '' # token to access the HTTP API of your bot created with @BotFather
CHANNEL_ID = '' # id of your channel, for example @durov
bot = telegram.Bot(token = TOKEN)
media_group = []
text = 'some caption for album'
for num in range(3):
media_group.append(InputMediaPhoto(open('img%d.png' % num, 'rb'),
caption = text if num == 0 else ''))
bot.send_media_group(chat_id = CHANNEL_ID, media = media_group)
Run Code Online (Sandbox Code Playgroud)