Getting the user profile pictures with pyTelegramBotAPI (JSON)
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).
If you pay close attention to this:
{'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 is an array of arrays. photos[0] is a list, then you’d have to go a level deeper and select one of its files, like this photos[0][0].
Anyway, you should add more info, is your program throwing an error?