2017-10-20 33 views
0

我有这个代码块建立消息附件和/或为一斜线命令的响应的表。Mattermost,斜线命令响应如邮件附件

$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变量并将其替换为$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运行命令时。

我在格式中丢失了什么?或者其他的东西?

回答

1

我不是特别熟悉PHP,但它看起来像你可能会被发送附件字段作为一个字符串,而不是对象的数组,因为你有'attachments' => "$attachments"那里。为响应最终的JSON有效载荷应该是这个样子

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