Sending a MediaGroup to Telegram using Guzzle

  guzzle, php, telegram-bot

I am building a PHP telegram bot platform, and I have been struggling with getting MediaGroup messages to work.

I have managed to get a single Photo to upload using Guzzle using the code below.

function sendPhoto($chat_id, $photo, $filename, $caption, $parse_mode = "markdown", $disable_notification = false, $reply_to_message_id = 0, $allow_sending_without_reply = false, $reply_markup = null){
$request = new stdClass();
$request->type = 'multipart';

$request->payload = [ 'multipart' =>
    [
        [
            'name' => 'chat_id',
            'contents' => $chat_id
        ],
        [
            'name'     => 'photo',
            'filename' => $filename,
            'contents' => $photo,
            'headers'  => [ 'Content-type' => 'application/octet-stream' ]
        ],
        [
            'name' => 'caption',
            'contents' => $caption
        ],
        [
            'name' => 'parse_mode',
            'contents' => $parse_mode
        ],
        [
            'name' => 'disable_notification',
            'contents' => $disable_notification
        ],
        [
            'name' => 'reply_to_message_id',
            'contents' => $reply_to_message_id
        ],
        [
            'name' => 'allow_sending_without_reply',
            'contents' => $allow_sending_without_reply
        ],
        [
            'name' => 'reply_markup',
            'contents' => $reply_markup
        ]   
    ]
];

return sendRequest2("sendPhoto", $request);
}

However, media groups seem massively more complicated.
Between the official documentation, and the suggestions I’ve had with getting guzzle to send multiple files in one Multipart request, this is what I have for one of my attempts.

$gRequest = new GuzzleHttpClient();
$gRequest->request(
'POST',
'http://api.telegram.org/bot' . $configs["token"] . '/sendMediaGroup',
[
    'multipart' => [
        [
        'name' => 'chat_id',
        'contents' => $chat_id
        ],
        [
            'name'     => 'media',
            'contents' =>
            [
                [
                    [
                        'name' => 'type',
                        'content' => 'photo'
                    ],
                    [
                        'name' => 'media',
                        'content' => 'attach://photo1'
                    ]
                ],
                [
                    [
                        'name' => 'type',
                        'content' => 'photo'
                    ],
                    [
                        'name' => 'media',
                        'content' => 'attach://photo2'
                    ]
                ],
                [
                    [
                        'name' => 'type',
                        'content' => 'photo'
                    ],
                    [
                        'name' => 'media',
                        'content' => 'attach://photo3'
                    ]
                ],
                [
                    [
                        'name' => 'type',
                        'content' => 'photo'
                    ],
                    [
                        'name' => 'media',
                        'content' => 'attach://photo4'
                    ]
                ],
                
            ]   
        ],
        [
            'name' => 'disable_notification',
            'contents' => $disable_notification
        ],
        [
            'name' => 'reply_to_message_id',
            'contents' => $reply_to_message_id
        ],
        [
            'name' => 'allow_sending_without_reply',
            'contents' => $allow_sending_without_reply
        ],
    
        [
            'name' => 'photo1',
            'contents' => fopen('1.png', 'r'),
            'filename' => 'photo1',
        ],
        [
            'name' => 'photo2',
            'contents' => fopen('2.png', 'r'),
            'filename' => 'photo2',
        ],
        [
            'name' => 'photo3',
            'contents' => fopen('3.png', 'r'),
            'filename' => 'photo3',
        ],
        [
            'name' => 'photo4',
            'contents' => fopen('4.png', 'r'),
            'filename' => 'photo4',
        ]
    ],
    'headers' => [
        # Do not override the Content-Type header here, Guzzle takes care of it
        'Accept' => 'application/json',
        'Accept-Language' => 'us'
    ]
]
);

Did I miss something simple, or am I way off target?

Source: Ask PHP

LEAVE A COMMENT