1

Here my code (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)

Here a result of the JSON containing an array with the profile pictures of my account Telegram (it is the result of the variable "number" in my code):

{'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>]]}

And here the involved classes from the pyTelegramBotAPI link

But printing the variable njson or nlist[0] it don't show nothing. Where is the problem? I want to obtain the file_id of a single PhotoSize object inside this array (doing photosizeName.file_id, so i can download the picture with the regular default Telegram API).

4

1 回答 1

2

如果你密切关注这一点:

{'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>]]}

照片是一个数组数组。照片[0] 是一个列表,那么您必须更深入地选择其中一个文件,例如这张照片[0][0]。

无论如何,您应该添加更多信息,您的程序是否抛出错误?

于 2020-07-14T15:32:36.773 回答