Mattermost,斜线命令响应作为消息附件

Mattermost, slash command response as message attachment

我有这个代码块可以构建消息附件 and/or table 用于斜杠命令的响应;

$attachments = array();
if (!count($whereClause)) {
    $data .= "**Can Not Build Query**\n";
}
else {
    if ($data = $db->getResult($sql)) {
        $table = "| PRC | Part Number | BIN | WH | Last Edit | Quotes | Last Quoted | Orders | Units Sold | Total Sales | Last Sold |\n";
        $table .= "|----:|----------:|----:|----:|--------:|----:|----:|----:|----:|----:|----:|----:|\n";

        foreach ($data as $part => $value) {
            $res_prc = $value['PRC'];
            $res_pn = trim($value['Part_Number']);
            $res_bin = $value['Bin'];
            $res_wh = $value['WH'] ?: 'N/A';

            if (isset($value['Last_Edit'])) {
                $res_le = date_format(date_create($value['Last_Edit']), 'm/d/Y');
            }
            else {
                $resl_le = 'N/A';
            }

            if (isset($value['By'])) {
                $res_le = $value['By']." @ $res_le";
            }

            $res_qts = $value['Quotes'] ?: 'N/A';

            if (isset($value['Last_Quoted'])) {
                $res_lq = date_format(date_create($value['Last_Quoted']), 'm/d/Y');
            }
            else {
                $res_lq = 'N/A';
            }

            $res_odr = $value['Orders'] ?: 'N/A';
            $res_us = $value['Units_Sold'] ?: 'N/A';
            $res_ts = $value['Total_Sales'] ?: 'N/A';

            if (isset($value['Last_Sold'])) {
                $res_ls = date_format(date_create($value['Last_Sold']), 'm/d/Y');
            }
            else {
                $res_ls = 'N/A';
            }

            $attachment = array(
                "fallback" => "PRC: $res_prc Part Number: $res_pn Location: $res_bin",
                "text" => "PRC: $res_prc Part Number: $res_pn Location: $res_bin",
                "color" => "#3fdbbc",
                "author_name" => "PRC: $res_prc Part Number: $res_pn",
                "title" => "$res_bin",
                "title_link" => "http://http://devbox/vrf/binlist.php?binLoc=$res_bin",
                "title" => "$res_bin",
                "fields" => array()
            );

            $warehouse = array(
                "short" => "true",
                "title" => "Warehouse",
                "value" => "$res_wh"
            );
            array_push($attachment['fields'], $warehouse);

            $last_edit = array(
                "short" => "true",
                "title" => "Last Edit",
                "value" => "$res_le"
            );
            array_push($attachment['fields'], $last_edit);

            $table .= "|$res_prc|$res_pn|$res_bin|$res_wh|$res_le|$res_qts|$res_lq|$res_odr|$res_us|$res_ts|$res_ls|\n";
            array_push($attachments, $attachment);
        }  
        $attachments = json_encode($attachments);
    }
    else {
        if ($db->lastError) {
            $data = "Error {$db->lastError} in:\n$sql";
        }
        else {
            $data .= " __No results__ \n";
        }
        $table = $data;
    }
}

$response = array(
    'response_type' => 'ephemeral',
    // 'text' => "$table",
    'username' => "Woodhouse",
    'icon_url' => 'http://linux3/mc-dev/img/woodhouse.png',
    'attachments' => "$attachments",
);
header('Content-type: application/json');
echo json_encode($response);

如果我按原样使用代码,则 mattermost 会记录并报告斜线命令返回空响应。如果我取消注释响应数组中的文本节点,我会按预期得到 table。如果我从响应数组中的文本节点中删除 $table 变量并将其替换为 $attachment 变量,我会在 mattermost 的响应中打印以下内容;

[
    {
        "fallback": "PRC: TI  Part Number: MC1489N Location: GG-68-06",
        "text": "PRC: TI  Part Number: MC1489N Location: GG-68-06",
        "color": "#3fdbbc",
        "author_name": "PRC: TI  Part Number: MC1489N",
        "title": "GG-68-06",
        "title_link": "http://http://devbox/vrf/binlist.php?binLoc=GG-68-06",
        "fields": [
            {
                "short": "true",
                "title": "Warehouse",
                "value": "W1"
            },
            {
                "short": "true",
                "title": "Last Edit",
                "value": "jlapera @ 09/11/2006"
            }
        ]
    }
]

这是意料之中的,因为它是为附件汇总的数据。

此外,我在回显响应之前注释掉了设置内容类型,当命令为 运行.

时,我得到了整个有效负载的 JSON 作为响应

我的格式是否遗漏了什么?还是什么?


编辑:2018 年 4 月 11 日

我 运行 再次陷入这个问题,最终我为保持代码尽可能干净所做的是;

$mmst_attach_raw = (object) [
      "fallback" => "Discrepancy Update or Recorded",
      "color" => "#BA1200",
      "author_name" => $attachment['created_by'],
      "title" => "Discrepancy Alert",
      "title_link" => $attachment['links']['edit'],
      "fields" => [
        [
          "short" => true,
          "title" => $attachment['order_type'],
          "value" => "[".$attachment['order_num']."](".$attachment['links']['tracker'].")"
        ], [
          "short" => true,
          "title" => "Department: $attachment[department]",
          "value" => "Created By: $attachment[created_by]\nLast Save: $attachment[last_action_by]"
        ], [
          "short" => false,
          "title" => "Vendor: $attachment[vendor_name]",
          "value" => "Vendor Number: $attachment[vendor_num]\nTerms: $attachment[vendor_terms]\nWarehouse: $attachment[warehouse]"
        ], [
          "short" => true,
          "title" => "Part Number: $attachment[part_num]",
          "value" => "Manufacturer: $attachment[prt_mfg]"
        ], [
          "short" => true,
          "title" => "$attachment[issues]",
          "value" => "$attachment[description]"
        ]
      ]
    ];
    $new_mmst_msg = array(
      'text' => "Incoming Alert",
      'channel' => '@mcarpenter',
      'attachments' => array($mmst_attach_raw)
    );

    send_msg_mmst($new_mmst_msg);

我必须先创建一个附件对象,然后通过将对象放入数组中将其添加到附件键,然后将其发送到我的发送消息函数。

我不是特别熟悉 PHP,但看起来您可能将附件字段作为字符串而不是对象数组发送,因为那里有 'attachments' => "$attachments"。响应的最终 json 负载应该类似于

{
    "response_type": "ephemeral",
    "text": "<text>",
    "username": "Woodhouse",
    "icon_url": "http://linux3/mc-dev/img/woodhouse.png",
    "attachments": [
        {
            "text": "<attachment text>"
        }
    ]
}