ttvd94
ttvd94

Reputation: 330

Telegram bot sendPhoto not working

What I'm trying to do is retrieving profile photos from a user and sned them for another, all by PHP.

The problem is when I send photos using file_id string, all of the photos sent to user are the same single picture!

I've not really run that but I'm sending my own photos to myself for testing the functionality and the result is my current telegram profile picture, every time.

My code:

<?php
define('my_id', '12345678');

$userPhotos = apiRequestJson("getUserProfilePhotos", array('user_id' => my_id, 'offset' => 0, 'limit' => 1));

apiRequestJson("sendPhoto", array('chat_id' => my_id, 'photo' => $userPhotos['photos'][0][0]['file_id']));
apiRequestJson("sendPhoto", array('chat_id' => my_id, 'photo' => $userPhotos['photos'][0][1]['file_id']));
?>

Link to the telegram bot api: https://core.telegram.org/bots/api

I'd be thankfull for any help.

Upvotes: 0

Views: 1944

Answers (1)

fusion3k
fusion3k

Reputation: 11689

There are two problems in your code.

First of all, when in the request you set the limit parameter to 1, you request for only one photo. Simply remove the optional offset and limit parameters to retrieve first 100 photos:

$userPhotos = apiRequestJson( 'getUserProfilePhotos', array( 'user_id' => my_id ) );

Second problem: the returned response is an “Array of Array of PhotoSize”, that means an array of photos that are arrays of different photo sizes:

$userPhotos['photos'][0][0]['file_id']
                      │  │
              photos ─┘  └─ photo sizes

You iterate second index (the size of same photo); you have instead to iterate first index:

apiRequestJson( 'sendPhoto', array( 'chat_id' => my_id, 'photo' => $userPhotos['photos'][0][0]['file_id'] ) );
apiRequestJson( 'sendPhoto', array( 'chat_id' => my_id, 'photo' => $userPhotos['photos'][1][0]['file_id'] ) );

Since you don't know in advance the total photo number of each user, the best way is to iterate through a foreach loop:

foreach( $userPhotos['photos'] as $photo )
{
    apiRequestJson( 'sendPhoto', array( 'chat_id' => my_id, 'photo' => $photo[0]['file_id'] ) );
}

Last but not least, please note that with this request you retrieve the user profile photos, so in most cases you will obtain anyway only one photo.

Upvotes: 1

Related Questions