mdarmanam
mdarmanam

Reputation: 35

Using PHP Telegram Bot InputMediaPhoto Method

I was trying to send some images into one message with my Telegram bot. I used InputMediaPhoto method to send, but unfortunately doesn't work.

Here is my code:

$url = "https://api.telegram.org/bot" . "TOKEN" . "/InputMediaPhoto";
$postContent = [
    'chat_id' => $GLOBALS['chatId'],
    'media' => [
        ['type'=>'photo' ,'media' => 'http://www.alcan5000.com/JPG/64Caliente.jpg'], //Just for test
        ['type' => 'photo' ,'media' => 'http://www.alcan5000.com/JPG/64Caliente.jpg'],
        ['type' => 'photo' ,'media' => 'http://www.alcan5000.com/JPG/64Caliente.jpg']
    ]
];
post($url, $postContent);


function post($url, $postContent)
{

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $postContent);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    $result = curl_exec($curl);
    curl_close($curl);
    return $result;
}

Upvotes: 1

Views: 1819

Answers (1)

newsha
newsha

Reputation: 1438

There are two problems.

The medthod you need to use is called sendMediaGroup. Also the media you are sending must be in a JSON-serialized array.

Changing your code to this should work:

 $url = "https://api.telegram.org/bot" . "TOKEN" . "/sendMediaGroup";
 $postContent = [
        'chat_id' => $GLOBALS['chatId'],
        'media' => json_encode([
            ['type'=>'photo' ,'media' => 'http://www.alcan5000.com/JPG/64Caliente.jpg'],
            ['type' => 'photo' ,'media' => 'http://www.alcan5000.com/JPG/64Caliente.jpg'],
            ['type' => 'photo' ,'media' => 'http://www.alcan5000.com/JPG/64Caliente.jpg']
        ])
    ];

Upvotes: 1

Related Questions