[英]How to get photo description in python telegram bot api?
I'm trying to get a photo description in telegram using python and Telegram bot api我正在尝试使用 python 和 Telegram bot api 在电报中获取照片描述
Function to get it:
Function 得到它:
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)
The bot receives a photo with a hashtag from the chat.该机器人从聊天中收到一张带有主题标签的照片。 If I try
message.text
I get TypeError.如果我尝试
message.text
我得到 TypeError。 In message.photo only photo sizes like this: [<telebot.types.PhotoSize object at 0x0000024036E03B80>, <telebot.types.PhotoSize object at 0x0000024036E03AF0>, <telebot.types.PhotoSize object at 0x0000024036E03940>, <telebot.types.PhotoSize object at 0x0000024036E0F880>]
. In message.photo only photo sizes like this:
[<telebot.types.PhotoSize object at 0x0000024036E03B80>, <telebot.types.PhotoSize object at 0x0000024036E03AF0>, <telebot.types.PhotoSize object at 0x0000024036E03940>, <telebot.types.PhotoSize object at 0x0000024036E0F880>]
。 In photo size this info {'file_size': 1081, 'file_unique_id': 'AQADP7oxG0qkEEl4', 'height': 51, 'width': 90, 'file_id': 'AgACAgIAAxkBAAICmGGhOnsbVNrGtpvmKuCHl6kEjgakAAI_ujEbSqQQSYIajNiAr9yaAQADAgADcwADIgQ'}
在照片大小中,此信息
{'file_size': 1081, 'file_unique_id': 'AQADP7oxG0qkEEl4', 'height': 51, 'width': 90, 'file_id': 'AgACAgIAAxkBAAICmGGhOnsbVNrGtpvmKuCHl6kEjgakAAI_ujEbSqQQSYIajNiAr9yaAQADAgADcwADIgQ'}
when receiving photos (or other media).接收照片(或其他媒体)时。 the description is found at
message.caption
instead of message.text
描述位于
message.caption
而不是message.text
Try this:尝试这个:
@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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.