2016-08-18 1 views
0

のチャネルにincoming webhooksがインストールされています。 Attachmentsはメッセージとともに送信する必要があり、PHP変数にはこれらのURLが保持されます。同様に、私はID'sをいくつか送って、いくつかのPHP変数で再び保持したいと思います。あなたは上記のattachments変数に表示されている場合は先頭で宣言${testplan_name}の値を印刷しようとするpretextの変数内部があり、PHPは添付ファイルと変数の値を持つスラックメッセージを送信します

<?php 

$testplan_name = $_POST[plan]; //test plan name coming from the client 
$url1 = $_POST[run_url]; //run url coming from the client 
$url2 = $_POST[plan_url]; //plan url coming from the client 
$room = "random"; 
$icon_url = ":ghost:"; 
$username = "Test"; 
$attachments = array([ 
      'fallback' => 'Hey! See this message', 
      'pretext' => 'Here is the plan name ${testplan_name}', 
      'color' => '#ff6600', 
      'fields' => array(
       [ 
        'title' => 'Run URL', 
        'value' => 'url1', 
        'short' => true 
       ], 
       [ 
        'title' => 'Build URL', 
        'value' => 'url2', 
        'short' => true 
       ] 
      ) 
     ]); 

$data = "payload=" . json_encode(array(   
     "channel"  => "#{$room}", 
     "icon_emoji" => $icon_url, 
     "username"  => $username, 
     "attachments" => $attachments 

    )); 

$url = "https://hooks.slack.com/services/XXXX/XXX/XXXXXXXXXXXXX"; //got from slack as a webhook URL 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
$result = curl_exec($ch); 
echo var_dump($result); 
if($result === false) 
{ 
    echo 'Curl error: ' . curl_error($ch); 
} 

curl_close($ch); 

:ここに私のserver side PHPコードです。しかし、それは動作していないようで、プログラムは余裕のあるチャンネルにメッセージを投稿できません。同様に、url1url2の値をattachments -> fieldsの値(上記の印刷方法)で印刷したいとします。私が変数を使ってメッセージを投稿しているときに値を取得しようとしないと、プログラムはうまく動作します。これらの変数の値をメッセージにどのように出力するのですか?

(slack is a messaging platform for teams, if you don't know)

答えて

0

>代わりに

$attachments = array([ 
     'fallback' => 'Hey! See this message', 
     'pretext' => 'Here is the plan name '.$testplan_name, 
     'color' => '#ff6600', 
     'fields' => array(
      [ 
       'title' => 'Run URL', 
       'value' => $url1, 
       'short' => true 
      ], 
      [ 
       'title' => 'Build URL', 
       'value' => $url2, 
       'short' => true 
      ] 
     ) 
    ]); 
これを試してみてください
関連する問題