在我不和谐的Webhook中,出现错误:{“ embeds”:[“ 0”]}

时间:2018-12-26 17:31:46

标签: php discord

我正在为记录某些内容创建不协调的Webhook,我已经借助模板(我不擅长php)完成此操作,并且不断收到错误消息:{"embeds": ["0"]}

我已经尝试研究它,但还没有得到任何帮助。介意我做此测试的麻烦。

这是我的代码:

<?php
  $url = "https://discordapp.com/api/webhooks/xxx"; // Censored for privacy

  $hookObject = json_encode([
      "username" => "Promotion Logs",
      "avatar_url" => "https://cdn.discordapp.com/icons/472520717515096078/60cc7dd2864c95a749516d1213359b67.png",
      "tts" => false,
      "embeds" => [
          [
              "title" => "Promotion Logs",
              "type" => "rich",
              "description" => "",
              "url" => "http://police.riverside-roleplay.com/promologs.php",
              "color" => hexdec( "0099ff" ),
              "fields" => [
                  [
                      "name" => "Name",
                      "value" => "dd",
                      "inline" => false
                  ],
                  [
                      "name" => "Rank",
                      "value" => "$rank",
                      "inline" => true
                  ],
                  [
                    "name" => "Their name",
                    "value" => "dd",
                    "inline" => true
                ],
                [
                  "name" => "Old rank",
                  "value" => "dd",
                  "inline" => true
              ],
              [
                "name" => "New rank",
                "value" => "dd",
                "inline" => true
            ],
            [
              "name" => "Reason",
              "value" => "dd",
              "inline" => true
          ],
          [
            "name" => "Date",
            "value" => "dd",
            "inline" => true
        ],
            ]
          ]
      ]

  ], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );

  $ch = curl_init();

  curl_setopt_array( $ch, [
      CURLOPT_URL => $url,
      CURLOPT_POST => true,
      CURLOPT_POSTFIELDS => $hookObject,
      CURLOPT_HTTPHEADER => [
          "Length" => strlen( $hookObject ),
          "Content-Type" => "application/json"
      ]
  ]);

  $response = curl_exec( $ch );
  curl_close( $ch );

  ?>

这是我使用的模板:

<?php

// Replace the URL with your own webhook url
$url = "https://discordapp.com/api/webhooks/0000000/ABCDEFGH....";

$hookObject = json_encode([
    /*
     * The general "message" shown above your embeds
     */
    "content" => "A message will go here",
    /*
     * The username shown in the message
     */
    "username" => "MyUsername",
    /*
     * The image location for the senders image
     */
    "avatar_url" => "https://pbs.twimg.com/profile_images/972154872261853184/RnOg6UyU_400x400.jpg",
    /*
     * Whether or not to read the message in Text-to-speech
     */
    "tts" => false,
    /*
     * File contents to send to upload a file
     */
    // "file" => "",
    /*
     * An array of Embeds
     */
    "embeds" => [
        /*
         * Our first embed
         */
        [
            // Set the title for your embed
            "title" => "Google.com",

            // The type of your embed, will ALWAYS be "rich"
            "type" => "rich",

            // A description for your embed
            "description" => "",

            // The URL of where your title will be a link to
            "url" => "https://www.google.com/",

            /* A timestamp to be displayed below the embed, IE for when an an article was posted
             * This must be formatted as ISO8601
             */
            "timestamp" => "2018-03-10T19:15:45-05:00",

            // The integer color to be used on the left side of the embed
            "color" => hexdec( "FFFFFF" ),

            // Footer object
            "footer" => [
                "text" => "Google TM",
                "icon_url" => "https://pbs.twimg.com/profile_images/972154872261853184/RnOg6UyU_400x400.jpg"
            ],

            // Image object
            "image" => [
                "url" => "https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png"
            ],

            // Thumbnail object
            "thumbnail" => [
                "url" => "https://pbs.twimg.com/profile_images/972154872261853184/RnOg6UyU_400x400.jpg"
            ],

            // Author object
            "author" => [
                "name" => "Alphabet",
                "url" => "https://www.abc.xyz"
            ],

            // Field array of objects
            "fields" => [
                // Field 1
                [
                    "name" => "Data A",
                    "value" => "Value A",
                    "inline" => false
                ],
                // Field 2
                [
                    "name" => "Data B",
                    "value" => "Value B",
                    "inline" => true
                ],
                // Field 3
                [
                    "name" => "Data C",
                    "value" => "Value C",
                    "inline" => true
                ]
            ]
        ]
    ]

], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );

$ch = curl_init();

curl_setopt_array( $ch, [
    CURLOPT_URL => $url,
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => $hookObject,
    CURLOPT_HTTPHEADER => [
        "Length" => strlen( $hookObject ),
        "Content-Type" => "application/json"
    ]
]);

$response = curl_exec( $ch );
curl_close( $ch );

?>

我的预期结果是嵌入我已设置的字段。在我将它实现到另一个页面并更改了一些内容之前,我已经有了它,然后它就坏了。实际结果是错误{"embeds": ["0"]}

4 个答案:

答案 0 :(得分:4)

您的代码的主要问题似乎是 $rank 未定义,因为其中没有值,因此 "value" => "$rank", 导致问题,因为 Discord 期望其中有值。

使用您编写的代码(并将 webhook 链接替换为用于测试的功能链接)最初会出现不同的错误:

{"message": "Cannot send an empty message", "code": 50006}

通过改变这个位可能是fixed

      CURLOPT_HTTPHEADER => [
          "Length" => strlen( $hookObject ),
          "Content-Type" => "application/json"
      ]

进入这个:

      CURLOPT_HTTPHEADER => [
          "Length" => strlen( $hookObject ),
          "Content-Type: application/json"
      ]

更改该位后,现在确实如您所描述的那样给出错误 {"embeds": ["0"]}。在我为顶部的 $rank 定义一些值后,您的代码又可以工作了。所以实际的问题是这个值是未定义的。

在我的 Gist 上查看原始代码和编辑后的代码之间的完整文件比较: https://gist.github.com/mnh48/a707aec600ac74f4e5d50875fcab5d7c

我以34.2为例$rank = 34.2;,结果:

result


对于来自搜索引擎的其他人

我最初遇到了同样的错误,我已经找到了原因,但我这边的原因与 OP 遇到的不同。

每当有人从 Discord webhook 收到此回复时:

{"embeds": ["0"]}

这可能意味着两种不同的东西:

  1. 正文中发送的字段无效
  2. 某些元素已超出限制

对于第一种情况,可以是:

  1. 格式错误:

    • timestamp 格式错误,必须是 ISO8601 格式
    • color 格式错误,必须是十进制,不接受其他任何内容(是的,甚至不接受像 "red" 这样的颜色名称字符串,将它们转换为十进制 -- 红色是 {{ 1}},您可以使用像 SpyColor 这样的网站来检查十进制值)
    • 16711680icon_urlavatar_url 中的格式错误,所有这些都必须在 urlhttp:// 中(另外对于 {{1} } 期望图像,它可以是 https://,只要图像文件也附加到请求中)
  2. 无效值:

    • 使用意外值,例如为 url 传递字符串而不是布尔值(接受两个字符串,可能是出于兼容性原因:attachment://inline,但不接受任何其他字符串,它期待布尔值)
    • 空值或空值,如果你想让它为空,那么首先不要包含它,或者使用 "true" 作为值
    • 未定义值,检查所有变量是否实际定义

对于第二种情况,限制如下:

  1. 嵌入标题的限制为 256 个字符
  2. 嵌入描述的限制为 2048 个字符
  3. 每个字段的名称不得超过 256 个字符
  4. 每个字段的值限制为 1024 个字符
  5. 页脚文本限制为 2048 个字符
  6. 作者姓名不得超过 256 个字符
  7. 每条消息最多可以有 25 个字段
  8. 每条消息最多可以嵌入 10 个
  9. 总字符数不得超过 6000 个字符

就我而言,是后者。我将电子邮件客户端设置为在 "false" > _ _ > embeds 中将电子邮件内容作为 Discord Webhook 发送,并且它传递了整个长电子邮件。 fields 字段的最大字符数限制为 1024,但我的电子邮件内容消息有 1565 个字符。

答案 1 :(得分:0)

对于其他任何从Google遇到的人:

对我来说,问题是我在JSON中使用env vars。 如果网址字段不是有效的网址,则会返回错误。

例如"url":"$SOME_VAR"

我确定这里可能还会进行其他检查。

答案 2 :(得分:0)

对于从 google 遇到此问题的其他人:

来自 API 的响应:

{"embeds": ["0"]}

我的问题是我在 JSON 中使用了浮点数变量。

错误的json:

{
    "allowed_mentions": {
        "parse": [
            "everyone"
        ]
    },
    "embeds": [
        {
            "author": {
                "name": "Name"
            },
            "title": "Title",
            "color": "51281",
            "fields": [
                {
                    "name": "Label",
                    "value": 648.2,
                    "inline": true
                }
            ]
        }
    ]
}

正确的json

{
    "allowed_mentions": {
        "parse": [
            "everyone"
        ]
    },
    "embeds": [
        {
            "author": {
                "name": "Name"
            },
            "title": "Title",
            "color": "51281",
            "fields": [
                {
                    "name": "Label",
                    "value": 648,
                    "inline": true
                }
            ]
        }
    ]
}

答案 3 :(得分:0)

对于来自 Google 的任何人,当 url 参数无效时,我遇到了同样的错误。

我在测试时将 url 设为 file:///Users/myUser/Downloads/test.html,但它不起作用。网络钩子似乎只接受有效的 URL,这是有道理的。