Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Telegram Bot Bad Gateway

I am trying to upload picture using Telegram Bot API using the following code

if(file_exists($_FILES['fileToUpload']['tmp_name'])){
        $new = fopen($_FILES['fileToUpload']['tmp_name'], "rb");
        $contents = fread($new, $_FILES['fileToUpload']['size']);
        fclose($new);
        $client = new Client();
        $response = $client->post("https://api.telegram.org/botMyApiKey/sendPhoto", [
            'body'    => ['chat_id' => '11111111', 'photo' => $contents]
        ]);
        var_dump($response);
}else{
        echo("No File");
}

I am getting Nginx 502 Bad Gateway. Am I using the correct method? I have no issues in obtaining getMe using the API.

P.S I am using Guzzle 5.3.0 for php compatibility.

like image 251
curious_coder Avatar asked Oct 31 '22 00:10

curious_coder


1 Answers

Try doing it as a multipart post.

$client->post(
    'https://api.telegram.org/botMyApiKey/sendPhoto', 
    array(
        'multipart' => array(
            array(
                'name'     => 'chat_id',
                'contents' => '1111111'
            ),
            array(
                'name'     => 'photo',
                'contents' => $contents
            )
        )
    )
);

Guzzle documentation reference

For Guzzle 5.3

use GuzzleHttp\Client;

$client = new Client(['defaults' => [
    'verify' => false
]]);

$response = $client->post('https://api.telegram.org/bot[token]/sendPhoto', [
    'body' => [
        'chat_id' => 'xxxxx',
        'photo' => fopen(__DIR__ . '/test.jpg', 'r')
    ]
]);

var_dump($response);

Note: you must pass the file handle to the 'photo' attribute and not the contents of the file.

like image 152
Chris Brand Avatar answered Nov 15 '22 04:11

Chris Brand