Core: How to use the sendMediaGroup method with local media (and not URL)?

Created on 28 Mar 2018  路  3Comments  路  Source: php-telegram-bot/core

Hello,

I am having issues using the sendMediaGroup command, when I try to send album of "local" pictures (not from URL).
Documentation says to use the "attach" syntax ("attach://"), however after multiple tries, I am unable to get this working.
I am currently using a workaround by sending the pictures one by one, however sending all pictures in album would be better.

Could you explain how to use the sendMediaGroup command with local media?

thanks

Most helpful comment

On my machine running (PHP 7.4) @TheSleepySlee code produces warnings:

[04-Jan-2020 15:18:33 UTC] PHP Warning:  file_exists(): Unable to find the wrapper "attach" - did you forget to enable it when you configured PHP? in /var/www/html/telegram/vendor/longman/telegram-bot/src/Request.php on line 443

It works, but by simply doing the warning is gone:

Request::sendMediaGroup([
    'chat_id'  => $chat_id,
    'media'    => [
        new InputMediaPhoto(['media' => '/path/to/photo_1']),
        new InputMediaPhoto(['media' => '/path/to/photo_2']),
        new InputMediaPhoto(['media' => '/path/to/photo_3']),
        new InputMediaPhoto(['media' => '/path/to/photo_4']),
    ],
]);

All 3 comments

Hi @TheSleepySlee

The attach:// method requires the key name of the multipart form that gets sent to Telegram, not the file name/path.
This is as easy as adding custom entries to the $data array being passed to Request::sendMediaGroup(...);. So all you need to do, is add custom elements that contain the file handlers and reference them :+1: Same thing applies for videos too!
(Be sure to not use any key that may be reserved by Telegram. It's very unlikely, but just check the documentation to be safe.)

Request::sendMediaGroup([
    'chat_id'  => $chat_id,
    'photo_1'  => Request::encodeFile('/path/to/some/photo.png'),
    'photo_2'  => Request::encodeFile('/path/to/another/photo.png'),
    'my_video' => Request::encodeFile('/path/to/my/video.mp4'),
    'media'    => [
        new InputMediaPhoto(['media' => 'attach://photo_1']),
        new InputMediaVideo(['media' => 'attach://my_video']),
        new InputMediaPhoto(['media' => 'attach://photo_2']),
    ],
]);

Thanks a lot. Clearly I am not an expert with this library.

Here is a working example, if it can help others.
pic1/2/3/4.jpg are located in same location where PHP file runs.

<?php
require_once "vendor/autoload.php";

use Longman\TelegramBot\Request;
use Longman\TelegramBot\Entities\InputMedia\InputMediaPhoto;
use Longman\TelegramBot\Telegram;
use Longman\TelegramBot\Exception\TelegramException;

$bot_api_key = "YOUR_BOT_TOKEN";
$chat_id = "YOUR_CHAT_ID";
$bot_username = 'YOUR_BOT_USERNAME';

$telegram = new Telegram($bot_api_key, $bot_username);

Request::sendMediaGroup([
    'chat_id'  => $chat_id,
    'photo_1'  => Request::encodeFile('pic1.jpg'),
    'photo_2'  => Request::encodeFile('pic2.jpg'),
    'photo_3'  => Request::encodeFile('pic3.jpg'),
    'photo_4'  => Request::encodeFile('pic4.jpg'),
    'media'    => [
        new InputMediaPhoto(['media' => 'attach://photo_1']),
        new InputMediaPhoto(['media' => 'attach://photo_2']),
        new InputMediaPhoto(['media' => 'attach://photo_3']),
        new InputMediaPhoto(['media' => 'attach://photo_4']),
    ],
]);
?>

On my machine running (PHP 7.4) @TheSleepySlee code produces warnings:

[04-Jan-2020 15:18:33 UTC] PHP Warning:  file_exists(): Unable to find the wrapper "attach" - did you forget to enable it when you configured PHP? in /var/www/html/telegram/vendor/longman/telegram-bot/src/Request.php on line 443

It works, but by simply doing the warning is gone:

Request::sendMediaGroup([
    'chat_id'  => $chat_id,
    'media'    => [
        new InputMediaPhoto(['media' => '/path/to/photo_1']),
        new InputMediaPhoto(['media' => '/path/to/photo_2']),
        new InputMediaPhoto(['media' => '/path/to/photo_3']),
        new InputMediaPhoto(['media' => '/path/to/photo_4']),
    ],
]);
Was this page helpful?
0 / 5 - 0 ratings

Related issues

ttvd94 picture ttvd94  路  4Comments

vansanblch picture vansanblch  路  3Comments

marcolino7 picture marcolino7  路  3Comments

irmmr picture irmmr  路  3Comments

dorcu picture dorcu  路  3Comments