如何在 python telegram bot api 中获取照片描述?

How to get photo description in python telegram bot api?

我正在尝试使用 python 和 Telegram 机器人 api

在电报中获取照片描述

Function to get it:

from telebot import *
from config import bot
from tasks.sport import sport


@bot.message_handler(content_types=['photo', 'text'])
def sport_photo(message):
    try:
        print(message.photo[0])
        if '#тренировка' in message.text or '#Тренировка' in message.text:
            sport(message)
    except TypeError:
        print(11111)

机器人从聊天中收到一张带有标签的照片。如果我尝试 message.text 我会得到 TypeError。在 message.photo 中,只有这样尺寸的照片:[<telebot.types.PhotoSize object at 0x0000024036E03B80>, <telebot.types.PhotoSize object at 0x0000024036E03AF0>, <telebot.types.PhotoSize object at 0x0000024036E03940>, <telebot.types.PhotoSize object at 0x0000024036E0F880>]。在照片尺寸中,此信息 {'file_size': 1081, 'file_unique_id': 'AQADP7oxG0qkEEl4', 'height': 51, 'width': 90, 'file_id': 'AgACAgIAAxkBAAICmGGhOnsbVNrGtpvmKuCHl6kEjgakAAI_ujEbSqQQSYIajNiAr9yaAQADAgADcwADIgQ'}

接收照片(或其他媒体)时。描述位于 message.caption 而不是 message.text

试试这个:

@bot.message_handler(content_types=['photo', 'text'])
def sport_photo(message):
    try:
        print(message.photo[0])
        if '#тренировка' in message.caption or '#Тренировка' in message.text:
            sport(message)
    except TypeError:
        print(11111)