I have this block of code that builds a message attachment and/or a table for the response of a slash command;
$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);
If I use the code as is mattermost logs and reports that the slash command has returned an empty response. If I uncomment the text node in the response array, I get a table as expected. If I remove the $table
variable from the text node in the response array and replace it with the $attachment
variable i get the following printed in the response inside 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"
}
]
}
]
Which would be expected since it's the data that is put together for the attachment.
Also, I commented out setting the content type before echoing the response and I get the JSON of the entire payload as a response when the command is run.
Am I missing something in my formatting? or something?
EDIT: 4/11/18
I ran into this issue again, and ultimately what I did to keep my code as clean as possible was;
$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);
I had to create an attachment object first, I then added it to the attachment key by putting the object in an array, and then emitting it to my send message function.
I'm not particularly familiar with PHP, but it looks like you might be sending the attachments field as a string instead of an array of objects since you have 'attachments' => "$attachments"
there. The final json payload for the response should look something like
{
"response_type": "ephemeral",
"text": "<text>",
"username": "Woodhouse",
"icon_url": "http://linux3/mc-dev/img/woodhouse.png",
"attachments": [
{
"text": "<attachment text>"
}
]
}