使用 pyTelegramBotAPI 获取用户个人资料图片 (JSON)
Getting the user profile pictures with pyTelegramBotAPI (JSON)
这是我的代码 (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"[=25] 的结果=] 在我的代码中 ):
{'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 link
但是打印变量 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 是数组的数组。 photos[0] 是一个列表,那么您必须更深入地研究 select 其中一个文件,例如 photos[0][0].
无论如何,您应该添加更多信息,您的程序是否抛出错误?
这是我的代码 (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"[=25] 的结果=] 在我的代码中 ):
{'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 link
但是打印变量 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 是数组的数组。 photos[0] 是一个列表,那么您必须更深入地研究 select 其中一个文件,例如 photos[0][0].
无论如何,您应该添加更多信息,您的程序是否抛出错误?