我有这个代码块,它为斜杠命令的响应构建了一个消息附件和/或一个表;
$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);
如果我按原样使用代码,最重要的日志并报告斜杠命令返回了一个空响应。如果我取消注释响应数组中的文本节点,我会按预期得到一个表。如果我$table
从响应数组中的文本节点中删除变量并将其替换为$attachment
变量,我会在最重要的响应中打印以下内容;
[
{
"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);
我必须先创建一个附件对象,然后通过将该对象放入一个数组中,将其添加到附件键中,然后将其发送到我的发送消息函数。