2017-10-20 4 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); 
. 응답 배열에서 텍스트 노드의 주석을 제거하면 예상대로 테이블이 생성됩니다. 응답 배열의 텍스트 노드에서 $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을 응답으로 받았습니다.

서식이 잘못 되었습니까? 또는 뭔가?

답변

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>" 
     } 
    ] 
}