PHPからPOSTで投稿してみた。
rsyslogと連携して、sshdのログイン/ログアウトを通知するようにしてみた。
#attachmentsの入れ方がわからなくて、ものすごく苦労したOrz
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
echo "sndmsg_slack.php run...".PHP_EOL; | |
$slackApiKey = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; | |
$url = "https://slack.com/api/chat.postMessage"; | |
$argchk = false; | |
if(count($argv) == 4){ | |
if($argv[1]=="GET"){ | |
$argchk = true; | |
/// GET /////////////////////////////////////////////////////////// | |
$query = [ 'token' => $slackApiKey, | |
'channel' => '#'.$argv[2], | |
'text' => str_replace("\\n",PHP_EOL,$argv[3]), | |
'as_user' => 'true' | |
]; | |
echo "[GET]URL=".$url.http_build_query($query).PHP_EOL; | |
$result = file_get_contents($url."?".http_build_query($query)); | |
echo $result.PHP_EOL; | |
/////////////////////////////////////////////////////////////////// | |
} | |
} | |
if(count($argv) == 7){ | |
if($argv[1]=="POST"){ | |
$argchk = true; | |
/// PST /////////////////////////////////////////////////////////// | |
$attachment = ['pretext' => $argv[3], | |
'color' => $argv[4], | |
'title' => $argv[5], | |
'text' => $argv[6]]; | |
$query = [ 'token' => $slackApiKey, | |
'channel' => '#'.$argv[2], | |
'attachments' => json_encode(Array($attachment)), | |
'as_user' => 'true' | |
]; | |
$options = array( | |
'http' => array( | |
'header' => "Content-Type: application/x-www-form-urlencoded\r\n", | |
'method' => 'POST', | |
'content' => http_build_query($query) | |
) | |
); | |
$context = stream_context_create($options); | |
echo "[POST]QUERY=".json_encode($query).PHP_EOL; | |
$result = file_get_contents($url, false, $context); | |
echo $result.PHP_EOL; | |
} | |
// $result = json_decode($result); | |
// $obj = json_decode(json_encode($query)); | |
// var_dump($obj); | |
} | |
if(!$argchk){ | |
echo "[mode] [channel] [text]".PHP_EOL; | |
echo "\tmode\tGET or POST".PHP_EOL; | |
echo "GET [channel] [message]".PHP_EOL; | |
echo "POST [channel] [pretext] [color] [title] [text]".PHP_EOL; | |
} | |
?> |