这是我的代码(Python):
import telebot
import time
import json
bot_token = "..."
bot = telebot.TeleBot(token=bot_token)
@bot.message_handler(commands=['myphoto'])
def send_welcome(message):
number = bot.get_user_profile_photos(message.from_user.id)
njson = json.loads(number)
nlist = njson['photos']
bot.reply_to(message, nlist[0].file_size)
while True:
try:
bot.polling(none_stop=True)
except Exception as e:
logger.error(e) # or just print(e) if you don't have logger,
# or import traceback; traceback.print_exc() for print full info
time.sleep(15)
这是JSON的结果,其中包含一个包含我的帐户Telegram的个人资料图片的数组(这是我的代码中变量“ number”的结果):
{'total_count': 2, 'photos': [[<telebot.types.PhotoSize object at
0x7fc0fd9069a0>, <telebot.types.PhotoSize object at 0x7fc0fd906970>,
<telebot.types.PhotoSize object at 0x7fc0fd906a30>],
[<telebot.types.PhotoSize object at 0x7fc0fd906550>,
<telebot.types.PhotoSize object at 0x7fc0fd906eb0>,
<telebot.types.PhotoSize object at 0x7fc0fd906790>]]}
这里是pyTelegramBotAPI链接中涉及的类
但是打印变量njson或nlist [0]不会显示任何内容。问题出在哪儿?我想在此数组中获取单个PhotoSize对象的file_id(这样做是photosizeName.file_id,因此我可以使用常规的默认Telegram API下载图片)。
如果您密切注意这一点:
{'total_count': 2, 'photos': [[<telebot.types.PhotoSize object at
0x7fc0fd9069a0>, <telebot.types.PhotoSize object at 0x7fc0fd906970>,
<telebot.types.PhotoSize object at 0x7fc0fd906a30>],
[<telebot.types.PhotoSize object at 0x7fc0fd906550>,
<telebot.types.PhotoSize object at 0x7fc0fd906eb0>,
<telebot.types.PhotoSize object at 0x7fc0fd906790>]]}
照片是一个数组数组。photos [0]是一个列表,那么您必须更深入地选择它的一个文件,例如photos [0] [0]。
无论如何,您应该添加更多信息,程序是否抛出错误?
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句