LonamiWebs / Telethon

Pure Python 3 MTProto API Telegram client library, for bots too!
https://docs.telethon.dev
MIT License
10.01k stars 1.41k forks source link

The file reference is invalid or you can't do that operation on such message (caused by UpdateProfilePhotoRequest) #4111

Closed APf0x closed 1 year ago

APf0x commented 1 year ago

hi so basically i am trying to make a program that automatically changes my pfp every day on telegram without me having to do anything but i have encountered this problem that has been bothering me for the past few hours and i really cant seem to find a way to fix it.

try:
    from telethon.sync import TelegramClient
    import os
    import re
    from telethon.tl.functions.photos import UploadProfilePhotoRequest
    from telethon.sync import TelegramClient
    from telethon import functions, types
except KeyError:
    print("ay man this shit does not work")

try:
    API_ID = "API_ID"
    API_HASH = "API_HASH"
except KeyError:
    API_ID = "Token not available!"
    API_HASH = "Token not available!"

def change_profile_picture(API_ID, API_HASH, image_path):
    with TelegramClient('session_name',API_ID, API_HASH) as client:
        image = str(client(UploadProfilePhotoRequest(fallback=True, file=client.upload_file(image_path))))
        id = re.search('id=(.*), access_hash=', image)
        access_hash = re.search('access_hash=(.*), file_reference=', image)
        image = bytes(image, encoding='UTF-8')

        file_reference = re.search(b'file_reference=b\'(.*?)\'', image)

        print(id.group(1), ", ", access_hash.group(1), ", ", file_reference.group(1))

        client(functions.photos.UpdateProfilePhotoRequest(id=types.InputPhoto(
            id=int(id.group(1)),
            access_hash=int(access_hash.group(1)),
            file_reference=file_reference.group(1),
            ), fallback=True))

image_path = 'counting_burning_money.jpg'

change_profile_picture(API_ID, API_HASH, image_path)

ignore my fucked up way of writing code but i could not find a better way to extract the id and the access hash of the image other than making that unstable mess. this is the error that the terminal has been giving me

Traceback (most recent call last):
  File "/home/redfox/telegram-pfp/main.py", line 44, in <module>
    change_profile_picture(API_ID, API_HASH, image_path)
  File "/home/redfox/telegram-pfp/main.py", line 34, in change_profile_picture
    client(functions.photos.UpdateProfilePhotoRequest(id=types.InputPhoto(
  File "/home/redfox/.local/lib/python3.10/site-packages/telethon/sync.py", line 39, in syncified
    return loop.run_until_complete(coro)
  File "/usr/lib/python3.10/asyncio/base_events.py", line 646, in run_until_complete
    return future.result()
  File "/home/redfox/.local/lib/python3.10/site-packages/telethon/client/users.py", line 30, in __call__
    return await self._call(self._sender, request, ordered=ordered)
  File "/home/redfox/.local/lib/python3.10/site-packages/telethon/client/users.py", line 83, in _call
    result = await future
telethon.errors.rpcerrorlist.FileReferenceInvalidError: The file reference is invalid or you can't do that operation on such message (caused by UpdateProfilePhotoRequest)

what i am mostly scared of is that i just cant do the operation for witch the function updateprofilephoto was made for.

Lonami commented 1 year ago

Hi, this is not an issue with the library but rather a question. The issues section in GitHub is only to report bugs or enhancements in the library itself. If you have usage questions, you should ask those in StackOverflow or the @TelethonChat group.