首页 > 解决方案 > 使用 Guzzle 将 MediaGroup 发送到 Telegram

问题描述

我正在构建一个 PHP 电报机器人平台,并且一直在努力让 MediaGroup 消息正常工作。

我已经设法使用下面的代码使用 Guzzle 上传一张照片。

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);
}

然而,媒体集团似乎要复杂得多。在官方文档和我在一个 Multipart 请求中发送多个文件的建议之间,这就是我的尝试之一。

$gRequest = new GuzzleHttp\Client();
$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'
    ]
]
);

我错过了一些简单的事情,还是我偏离了目标?

标签: phptelegram-botguzzle

解决方案


推荐阅读