V
V
Vladimir Mogila2021-05-14 13:29:46
PHP
Vladimir Mogila, 2021-05-14 13:29:46

Who has a working solution for the sendMediaGroup telegram bot in pure php without libraries?

As it turned out, finding a working example is very difficult.

I send one photo without problems, but with several I got stuck very much, and I tried this and that, but to no avail.
Searches on the net also did not lead to a positive result, I found such an example and tried to apply it, removing obvious errors (although it was said that it was working), but in the end nothing (

$url = "https://api.telegram.org/bot" . "TOKEN" . "/sendMediaGroup";
    
 $postContent = [
            'chat_id' => "ID",
            'media' => json_encode([
                ['type' => 'photo', 'media' => 'attach://data.jpg' ],
                ['type' => 'photo', 'media' => 'attach://data.jpg' ],
            ]),
            'data1.jpg' => new CURLFile(realpath("/var/www/html/data.jpg")),
            'data.jpg' => new CURLFile(realpath("/var/www/html/data.jpg")),
        ];
    
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $postContent);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type: multipart/form-data']);
    $result = curl_exec($curl);
    curl_close($curl);

Changed the code to this:
$url = "https://api.telegram.org/bot" . $api . "/sendMediaGroup";
    				
          $url_1 = $src_to_img . '1620919407-file_555.jpg';
          $url_2 = $src_to_img . '1620919409-file_556.png';
          $url_3 = $src_to_img . '1620919407-file_557.jpg';

         	$postContent = [
                    'chat_id' => $chat_id,
                    'media' => json_encode([
                        ['type' => 'photo', 'media' => 'attach://1620919407-file_555.jpg' ],
                        ['type' => 'photo', 'media' => 'attach://1620919409-file_556.png' ],
                        ['type' => 'photo', 'media' => 'attach://1620919410-file_557.jpg' ]
                    ]),
                    '1620919407-file_555.jpg' => new CURLFile(realpath($url_1)),
                    '1620919409-file_556.png' => new CURLFile(realpath($url_2)),
                    '1620919410-file_557.jpg' => new CURLFile(realpath($url_3))
                ];
            
            $curl = curl_init();
            curl_setopt($curl, CURLOPT_URL, $url);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $postContent);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type: multipart/form-data']);
            $result = curl_exec($curl);
            curl_close($curl);


where

$src_to_img = "/var/www/www-root/data/www/mytelegrambot.club/escooter/img/users_img/";

$api is a token.
Pictures are pictures lying on the server along the path in the variables $url_1, $url_2, $url_3

I would also send text to the images, it would be generally super if there are

Friends in the example, if the solution is simple, just send links

$ url = " https://api.telegram.org/bot " . $api . "/sendMediaGroup";

$postContent = [
'chat_id' => $chat_id,
'media' => json_encode([
['type' => 'photo', 'media' => ' https://mytelegrambot.club/escooter/img/users_img/ ... ' ],
['type' => 'photo', 'media' => '
['type' => 'photo', 'media' => ' https://mytelegrambot.club/escooter/img/users_img/... ' ]
]),

];

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postContent);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type: multipart/form-data']);
$result = curl_exec($curl);
curl_close($curl);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Shamanov, 2021-05-14
@vim-studio

"'Content-Type: multipart/form-data'" is set automatically when transferring files, and why are you suffering with an attachment? Sending it all the same from a web server would send links. And what you are passing is just the contents of the Message

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question