17

Is it possible to receive images (and store it on the server) which was sent by any user to the bot?

If a image was sent, I receive this JSON post. This is a link to the Telegram Bot-API description. I don't know if it's possible to receive the whole image or not: https://core.telegram.org/bots/api#available-types

{"update_id":XXXXX,
"message":{"message_id":2222,"from":{"id":XXXXX,"first_name":"Christoph","last_name":"XXXXX"},"chat":{"id":XXXXX,"first_name":"Christoph","last_name":"XXXXX"},"date":1435704055,"forward_from":{"id":XXXXX,"first_name":"Christoph","last_name":"XXXXX"},"forward_date":1435703471,"photo":[{"file_id":"AgADAgADmaoxG9KknwF4O978o3EMqb_EWSoABI5s-WWq46dqiR0AAgI","file_size":998,"width":51,"height":90},{"file_id":"AgADAgADmaoxG9KknwF4O978o3EMqb_EWSoABHax4HvxYqktiB0AAgI","file_size":9912,"width":180,"height":320},{"file_id":"AgADAgADmaoxG9KknwF4O978o3EMqb_EWSoABNzzDwp3sT2whx0AAgI","file_size":41020,"width":450,"height":800},{"file_id":"AgADAgADmaoxG9KknwF4O978o3EMqb_EWSoABE0Gg-AefJ7Yhh0AAgI","file_size":66058,"width":720,"height":1280}]}}
Charles Okwuagwu
  • 10,538
  • 16
  • 87
  • 157
C.E.
  • 664
  • 2
  • 5
  • 21
  • 1
    Where is your PHP code? – server_kitten Jul 01 '15 at 22:19
  • At this time i have nothing, because i dont know what to do in this case. Now, only store the contents of the Telegram Webhook-Call to my script. – C.E. Jul 01 '15 at 22:21
  • 1
    Assume we don't know the Telegram API, and walk us through what is happening. Is the JSON here a reply message from Telegram? It references some `file_id` codes - where are they stored? Is there an API call to fetch them? – halfer Jul 01 '15 at 22:41
  • 1
    Would you point us to the relevant part of the documentation that would help us to answer? – halfer Jul 01 '15 at 22:43
  • I originally linked the telegram api documentation, but it seems to be removed. The JSON Here is a reply from Telegram, which would be send via an Webhook to my script. I make a logfile for incoming JSON Data with file_get_contents('php://input'), and the whole content, is in the code-box in my first Post. There is no Api-Call to fetch them, but is it possible that Telegram sends the Pic in any other format in the Webhook ? I try to print_r($_POST) and $_GET and so on, but the arrays was empty everytime i tried, so it seems like this above is the only i receive. Sry for my bad eng – C.E. Jul 01 '15 at 22:47
  • There is the link for incoming "types": https://core.telegram.org/bots/api#available-types I hope its allowed to post it here. – C.E. Jul 01 '15 at 22:53
  • It's available now! Check here: http://stackoverflow.com/questions/31096358/how-do-i-download-a-file-or-photo-that-was-sent-to-my-telegram-bot – zxcmehran Sep 21 '15 at 21:39

3 Answers3

16

Telegram support download file now with getFile:

You can see it in the api documentation: https://core.telegram.org/bots/api#getfile

AdrieanKhisbe
  • 3,899
  • 8
  • 37
  • 45
Samuel Surya
  • 433
  • 5
  • 10
9

It's possible to download the image from Telegram server. Do this:
1. Get the file using the getFile api

//Telegram link
$telegram_link = 'https://api.telegram.org/bot' . $this->tg_configs['api_key'] . '/getFile?file_id=' . $photo['file_id'];

2. Get the file path //Create guzzle client $guzzle_client = new GuzzleClient();

//Call telegram
$request = $guzzle_client->get($telegram_link);
//Decode json
$json_response = json_decode($request->getBody(), true);
if ($json_response['ok'] == 'true') {

    //Telegram file link
    $telegram_file_link = 'https://api.telegram.org/file/bot' . $this->tg_configs['api_key'] . '/' . $json_response['result']['file_path'];

3. If using PHP use Intervention/Image to download the image and save it on your server.

//Build upload path
$upload_path = public_path() . \Config::get('media::media.uploadPath');
//Get image
$image = $thumbnail = InterventionImage::make($telegram_file_link);

//Get mime
$mime = $image->mime();

if ($mime == 'image/jpeg') {
    $extension = '.jpg';
} elseif ($mime == 'image/png') {
    $extension = '.png';
} elseif ($mime == 'image/gif') {
    $extension = '.gif';
} else {
    $extension = '';
}//E# if else statement
//Resize images
$image->resize(\Config::get('media::media.mainWidth'), \Config::get('media::media.mainHeight'));
$thumbnail->resize(\Config::get('media::media.thumbnailWidth'), \Config::get('media::media.thumbnailHeight'));

//Build media name
$media_name = \Str::random(\Config::get('media::media.mediaNameLength')) . $extension;

//Save images
$image->save($upload_path . '/' . $media_name);
$thumbnail->save($upload_path . '/thumbnails/' . $media_name);
Sergey
  • 5,208
  • 25
  • 36
Edwin M
  • 351
  • 3
  • 4
0

The accepted answer is fine. But here's an answer in more detail. There are three main steps involved before you can get an actual image file. 1) Request from Telegram the file_id. 2) Load a Guzzle client. 3) Request from Telegram the file_path. Pay close attention to the different URL paths below.

// get file id
$id = $update['message']['photo'][0]['file_id'];
$file_id = "https://api.telegram.org/bot" . 
 config('app.telegram_api_key') . "/getFile?file_id=" . $id;

// guzzle client
$guzzle_client = new GuzzleClient();
$request = $guzzle_client->get($file_id);
$response = json_decode($request->getBody(),true);

// get file path
$file_url = "https://api.telegram.org/file/bot" .
 config('app.telegram_api_key') . "/" . $response['result']['file_path'];
kaleazy
  • 5,922
  • 2
  • 47
  • 51