confetti
confetti

Reputation: 1100

Is there a way to get profile picture updates using python Telegram-Bot?

So I'm using python-telegram-bot for telegram integration into another application. My goal is to have the profile pictures of a user on telegram within my application. (Users and group chats)

Getting a user's or group's avatar is easy, so is downloading and using it in my app. However, what if the user changes their profile picture? I couldn't find any update message or handler in the documentations that allows for a bot to retrieve a picture change, not even for groups.

My first thought was to first retrieve all pictures and store the file_id in a database, then periodically check that user's/group's pictures and go back through their pictures until file_id matches the last saved file_id in the database.

This combined with a JobQueue is the best thing I can come up with, so I'll self-answer using that, but I think it's still not a perfect solution so if anyone has a better idea I'd appreciate an answer.

I'm specifically looking for a better solution for groups, since I don't think there is a way to retrieve any but the most recent picture for groups, and my application should retrieve all of them. Another flaw my self-answer has is that if a user changes the profile picture twice within those six hours, I will only get the most recent one. This can be fixed for users with the offset attribute in the bot call, but the method to get profile pictures of a group does not seem to have that.

tl;dr:

How can I retrieve updates whenever a user changes their or a groups profile picture the most efficient and reliable way using python-telegram-bot and python 3.5?

Upvotes: 0

Views: 3654

Answers (1)

confetti
confetti

Reputation: 1100

This is using telegram.ext.JobQueue to check for profile picture updates every 6 hours.

# define job queue
j = updater.job_queue

def dl_pfps(bot, job):
    # this assumes that we have a textfile with the following
    # layout: "user_id:last_pfp_file_id" - One per line
    # later we'll write a list back into it with the newest IDs
    user_pfp_list = []
    with open("user_pfps.txt") as f:
        for line in f:
            user_id = line.split(':')[0]
            last_file_id = line.split(':')[1]
            most_recent_pfp = bot.get_user_profile_photos(user_id, limit=1).photos[0]
            if last_file_id == most_recent_pfp[-1].file_id:
                print("No change")
                user_pfp_list.append(user_id + ":" + last_file_id)
            else:
                print("User updated profile picture. Geting full size picture...")
                # download and process the picture
                file_id = most_recent_pfp[-1].file_id
                newFile = bot.getFile(file_id)
                newFile.download('my/filename.jpg')
                user_pfp_list.append(user_id + ":" + file_id)

    # write new list back to file (overwrite current list)
    with open("user_pfps.txt", "w") as f:
        f.write("\n".join(user_pfp_list))

# check for new profile pictures every 6 hours
job_dlpfps = j.run_repeating(dl_pfps, interval=21600, first=0)

This is the best I can come up with. If you want to use this in your code you have to adjust 'my/filename.jpg' to a proper filename and you need to generate an initial list in user_pfps.txt with one line per user like this: user_id:0

Upvotes: 1

Related Questions