php - 使用 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'
]
]
);
我错过了一些简单的事情,还是我偏离了目标?
解决方案
推荐阅读
- asp.net-core - Asp.net core 3 Unicode slug url段作为控制器HttpGet操作方法中的参数没有收到任何url段值
- sql-server - 尝试对表中具有相同后缀的数据进行计数
- java - Jackson 2:无法将类型 id 'C' 解析为 `test.protocol.StatusEventObject 的子类型
- r - 在 R 中不使用 lme4 包估计混合级逻辑回归系数
- java - 访问 Firestore 时出现无效的集合引用错误
- java - 如何将json对象映射到apache spark中的java对象
- apache-flink - flink 流式处理与 flink 批处理中的内存管理
- java - 读取,然后将文本文件拆分为不同的数组
- c# - 您可以仅使用 DataGridRow 检索数据网格对象吗?WPF
- google-apps-script - 基于单击 Google 电子表格中的 URL 以重新打开表单来清除单元格的功能?