sendPhoto method sends identical image despite different file_id values

I’m working on a PHP Telegram bot that should grab user profile pictures and forward them to another chat. The issue I’m facing is weird - even though I’m using different file_id values, the bot keeps sending the same photo over and over again.

I’m testing this by sending my own profile photos back to myself. What happens is that no matter which file_id I use, I always get my current profile picture instead of the different photos from my profile history.

Here’s my code:

<?php
define('TARGET_USER', '87654321');

$profilePics = makeApiCall("getUserProfilePhotos", array('user_id' => TARGET_USER, 'offset' => 0, 'limit' => 1));

makeApiCall("sendPhoto", array('chat_id' => TARGET_USER, 'photo' => $profilePics['photos'][0][0]['file_id']));
makeApiCall("sendPhoto", array('chat_id' => TARGET_USER, 'photo' => $profilePics['photos'][0][1]['file_id']));
?>

Any ideas why this might be happening? Thanks in advance for the help.

Others already covered your array indexing problem, but here’s something to consider - manually handling Telegram bot operations gets messy when you scale up.

I’ve built tons of Telegram bots for internal tools. Those manual API calls become a nightmare once you need error handling, rate limiting, or complex workflows.

I moved everything to Latenode. It handles Telegram API complexity automatically and gives you visual workflows instead of PHP code. Set up triggers for profile photo requests, add error handling for missing photos, chain multiple operations together.

When you want features like cloud storage or processing multiple users, just drag and drop new nodes. No code rewrites.

Check it out: https://latenode.com

This happens because you’re misunderstanding how Telegram structures profile photo data. I ran into the same issue building a user verification bot last year. The getUserProfilePhotos response gives you a photos array - each element is a different profile picture from the user’s history. Inside each photo element, there’s another array with different resolutions of that same image. Right now you’re just accessing different resolutions of the first photo. To get different profile pictures, bump up your limit parameter and use different indices for the outer array: $profilePics['photos'][0][0] for the first photo, $profilePics['photos'][1][0] for the second. Don’t forget bounds checking though - some users have fewer profile pics than you’d expect, and you’ll get array index errors otherwise.

ur pulling file_ids from the same pic but different sizes. $profilePics['photos'][0][0] and $profilePics['photos'][0][1] are just different res of the same image. Change the first index instead - use $profilePics['photos'][0][0] then $profilePics['photos'][1][0] to grab different pics from their history.

The Problem:

You’re attempting to send different profile pictures from a Telegram user’s history to another chat using their file_ids. However, your bot keeps sending the same picture regardless of the file_id used. This is happening even when testing by sending your own profile photos back to yourself. The issue is that you’re not retrieving multiple profile pictures from the Telegram API.

:thinking: Understanding the “Why” (The Root Cause):

The getUserProfilePhotos API call, as used in your code, is limited to retrieving only one profile photo due to the limit => 1 parameter. Your current code accesses elements within this single photo’s data ($profilePics['photos'][0][0] and $profilePics['photos'][0][1]), but these elements represent different sizes (resolutions) of the same photo, not different photos from the user’s history. To get different profile photos, you must request more than one from the API.

:gear: Step-by-Step Guide:

Step 1: Request Multiple Profile Pictures:

Modify your getUserProfilePhotos API call to increase the limit parameter. This will retrieve multiple profile photos from the user’s history. A value of 5 or 10 is a good starting point, depending on the expected number of profile photos a user might have.

<?php
define('TARGET_USER', '87654321');

$profilePics = makeApiCall("getUserProfilePhotos", array('user_id' => TARGET_USER, 'offset' => 0, 'limit' => 5)); //Increased limit to 5

//Iterate through the different photos
foreach ($profilePics['photos'] as $photoSet){
    $fileId = $photoSet[0]['file_id']; //Get the largest size available
    makeApiCall("sendPhoto", array('chat_id' => TARGET_USER, 'photo' => $fileId));
}

?>

Step 2: Iterate and Send Photos:

The response to getUserProfilePhotos now contains an array of arrays. The outer array represents the set of distinct photos, while the inner array contains different sizes of the same photo. We iterate through the outer array to access each unique photo, selecting the largest size image for sending.

Step 3: Handle Potential Errors:

Add error handling to gracefully manage situations where a user might have fewer profile pictures than the requested limit. Check if $profilePics['photos'] is empty or if a specific index is out of bounds.

<?php
// ... (previous code) ...

if (!empty($profilePics['photos'])) {
    foreach ($profilePics['photos'] as $photoSet) {
        if (!empty($photoSet)) {
            $fileId = $photoSet[0]['file_id'];
            makeApiCall("sendPhoto", array('chat_id' => TARGET_USER, 'photo' => $fileId));
        }
    }
} else {
    //Handle the case where no photos were found. For example:
    echo "No profile photos found for user " . TARGET_USER . "\n";
}

?>

:mag: Common Pitfalls & What to Check Next:

  • API Rate Limits: Make sure you’re not exceeding Telegram’s API rate limits. If you’re sending many photos rapidly, you might need to add delays between requests.
  • makeApiCall Function: Ensure your makeApiCall function handles errors appropriately and provides informative error messages. Log the response from the Telegram API for debugging purposes.
  • User Privacy: Be mindful of user privacy when accessing and sharing profile pictures. Always ensure you have the user’s consent before accessing their data.
  • file_id structure: Double-check the structure of the file_id returned by the API. Ensure you are correctly accessing the file_id within the nested array structure.

:speech_balloon: Still running into issues? Share your (sanitized) config files, the exact command you ran, and any other relevant details. The community is here to help!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.