【问题标题】:group send failed. telegram api bot, sendMediaGroup function群发失败。 telegram api bot,sendMediaGroup 函数
【发布时间】:2018-05-30 15:20:50
【问题描述】:

在函数输入 $files = $_FILES

不要从我这里得到 Telegram 想要的东西。

它说:“{“ok”:false,"error_code":400,"description":"Bad Request: group send failed"}"。希尔普莱斯!

function sendMediaGroup($files)
{
    $url = "https://api.telegram.org/bot" . $this->token . "/" . __FUNCTION__;
    $media = [];
    $ch = curl_init();
    $type = "photo";
    $caption = "";

    foreach ($files as $file)
    {
        $media[] = [
            'type' => $type,
            'media' => $file['tmp_name'],
            'caption' => $caption
        ];
    }

    $disable_notification = false;
    $reply_to_message_id = null;
    $parameters = [
        'chat_id' => $this->chat_id,
        'media' => json_encode($media),
        'disable_notification' => $disable_notification,
        'reply_to_message_id' => $reply_to_message_id,
    ];

    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type:multipart/form-data"));
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);

    return $output = curl_exec($ch);
}

【问题讨论】:

  • 您只传递了文件名,请将其附加到请求中

标签: php json telegram


【解决方案1】:

您应该根据文件名称命名文件并将文件附加到请求中。所以像这样改变你的代码:

function sendMediaGroup($files)
{
    $url = "https://api.telegram.org/bot" . $this->token . "/" . __FUNCTION__;
    $media = [];
    $ch = curl_init();
    $type = "photo";
    $caption = "";
    $parameters = array();

    foreach ($files as $file)
    {
        $media[] = [
            'type' => $type,
            'media' => "attach://" . $file['tmp_name'],
            'caption' => $caption
        ];

        $parameters[] = [
            $file['tmp_name'] => $file
        ];
    }

    $disable_notification = false;
    $reply_to_message_id = null;
    $parameters[] = [
        'chat_id' => $this->chat_id,
        'media' => json_encode($media),
        'disable_notification' => $disable_notification,
        'reply_to_message_id' => $reply_to_message_id,
    ];

    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type:multipart/form-data"));
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);

    return $output = curl_exec($ch);
}

【讨论】:

    【解决方案2】:

    为了使 Telegram从 Internet 中的照片 URL 创建媒体组,您作为 media 属性值传递的文件链接必须可供 Telegram 的服务器访问。有时情况并非如此,sendMediaGroup API 调用失败并显示神秘错误消息Bad Request: group send failed。在这种情况下,您可以尝试另一种发送照片的方法,例如在 text 字段中使用 sendMessage 方法或转到 @webpagebot 并向他发送指向您文件的链接 - 这将更新链接的预览,您将能够在媒体组内发送该链接。

    版主注意:此答案并非 100% 针对原始问题,但在使用标题中的单词进行搜索时,会在前十个中弹出指向此问题的链接。

    【讨论】:

    • 嗯,它有帮助,某些文件总是返回错误“错误请求:组发送失败”,直到我将该链接发送到此机器人,并且我无法再次使用其他文件重复此错误
    【解决方案3】:

    如果您为媒体设置 URL,您的 URL 应该在 :80 或 :443 端口上工作。例如:https:examplesite.com/image1.jpg 可以,https:examplesite.com:8443/image1.jpg 不行!

    【讨论】:

      猜你喜欢
      • 2020-11-09
      • 1970-01-01
      • 2021-08-08
      • 2018-04-07
      • 2016-04-06
      • 2018-11-12
      • 2018-09-04
      • 2021-11-26
      • 1970-01-01
      相关资源
      最近更新 更多