-
-
Save matsubo/a5f8eb01057dbf86cd3fc9cb2d116ebc to your computer and use it in GitHub Desktop.
Send a notification to slack when a visitor is detected on Envoy.
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 | |
if ($_SERVER['REQUEST_METHOD'] != 'POST') { | |
print 'This script should be requested by POST method.'; | |
exit; | |
} | |
$LOG_FILE = dirname(__FILE__).'/hook.txt'; | |
$body = file_get_contents('php://input'); | |
if (!$body) { throw new Exception('input is not found'); } | |
$payload = json_decode($body, true); | |
# log | |
file_put_contents($LOG_FILE, date("[Y-m-d H:i:s]")." ".print_r($payload,1)."\n", FILE_APPEND|LOCK_EX); | |
$url = $_GET['slack_webhook_url']; | |
$rows = array(); | |
foreach ($payload['payload']['attributes']['user-data'] as $array) { | |
$rows[] = (array('title' => $array['field'], 'value' => $array['value'])); | |
} | |
$rows[] = (array('title' => '訪問時間', 'value' => $payload['payload']['attributes']['signed-in-at'])); | |
$message = [ | |
'username' => 'neo-envoy', | |
'text' => sprintf('%s 様が%s %s の受付でお待ちです。', | |
$payload['payload']['attributes']['full-name'], | |
$payload['meta']['company']['attributes']['name'], | |
$payload['meta']['location']['attributes']['name'] | |
), | |
'attachments' => [ | |
[ | |
'text' => $payload['payload']['attributes']['full-name'], | |
'color' => 'good', | |
'fields' => $rows | |
], | |
] | |
]; | |
if ($payload['payload']['attributes']['host']) { | |
$message['attachments'][] = | |
[ | |
'text' => '訪問先', | |
'color' => 'warning', | |
'fields' => [ | |
[ | |
'title' => $payload['payload']['attributes']['host'], | |
'value' => $payload['payload']['attributes']['host-email'], | |
], | |
] | |
]; | |
} | |
$ch = curl_init(); | |
$options = [ | |
CURLOPT_URL => $url, | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_SSL_VERIFYPEER => false, | |
CURLOPT_POST => true, | |
CURLOPT_POSTFIELDS => http_build_query([ | |
'payload' => json_encode($message) | |
]) | |
]; | |
curl_setopt_array($ch, $options); | |
curl_exec($ch); | |
curl_close($ch); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment