简体   繁体   中英

How to get Telegram user profile photo by user ID with aiogram v2?

I trying to get Telegram user profile photo by user ID with aiogram v2.

def get_photo(user_id):
    photo_data = bot.get_user_profile_photos(user_id, 1, 1)
    return photo_data

@dp.message_handler(commands=['photo'])
async def get_user_photo():
    photo_data = get_photo(367928353)
    #logger.error(photo_data)
    await message.answer(photo_data)

by command '/photo' I receive:

Task exception was never retrieved
future: <Task finished name='Task-8' coro=<Dispatcher._process_polling_updates() done, defined at /home/r/rlyzhov/.local/lib/python3.10/site-packages/aiogram/dispatcher/dispatcher.py:407> exception=TypeError('get_user_photo() takes 0 positional arguments but 1 was given')>
Traceback (most recent call last):
  File "/home/r/rlyzhov/.local/lib/python3.10/site-packages/aiogram/dispatcher/dispatcher.py", line 415, in _process_polling_updates
    for responses in itertools.chain.from_iterable(await self.process_updates(updates, fast)):
  File "/home/r/rlyzhov/.local/lib/python3.10/site-packages/aiogram/dispatcher/dispatcher.py", line 235, in process_updates
    return await asyncio.gather(*tasks)
  File "/home/r/rlyzhov/.local/lib/python3.10/site-packages/aiogram/dispatcher/handler.py", line 117, in notify
    response = await handler_obj.handler(*args, **partial_data)
  File "/home/r/rlyzhov/.local/lib/python3.10/site-packages/aiogram/dispatcher/dispatcher.py", line 256, in process_update
    return await self.message_handlers.notify(update.message)
  File "/home/r/rlyzhov/.local/lib/python3.10/site-packages/aiogram/dispatcher/handler.py", line 117, in notify
    response = await handler_obj.handler(*args, **partial_data)
TypeError: get_user_photo() takes 0 positional arguments but 1 was given

What I do wrong?

I understand last TypeError, but what and where I give it 1 argument? Perhaps my knowlege of aiogram and async functions are too small yet

Please tell me what I need to read to understand the problem and solve it. Thank you

try: get Telegram user profile photo by user ID with aiogram v2 expect: Telegram user photo recieve: error

first of all you need read the documentation, namely about handlers. Just your function get_phot is synchronous which used asynchronous method bot.get_user_profile_photos after then you create a handlers get_user_photo() but don't transmit parameters types.Message from aiogram. I rewrote your code and that's what came out.

from aiogram import types
from loader import dp


@dp.message_handler(commands=['photo'])
async def get_user_photo(message: types.Message):
    profile_pictures = await dp.bot.get_user_profile_photos(message.from_user.id)
    await message.answer_photo(dict((profile_pictures.photos[0][0])).get("file_id"))

For this task i think we don't need create another function, but if you want you can create. In profile_pictures we saved PhotoSize which store all information about user profile photo. In the next line we convert PhotoSize in `dict and we take only the "file_id". For the test, we send this photo to the chat

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM