-
-
Save ncou/888e2ac090e308c42d52c5a76c43ca65 to your computer and use it in GitHub Desktop.
Slack.com Webhook Integration (PHP) - Simple snippet which tells you, how to build your payload array.
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 | |
//Options | |
$token = 'YOUR_TOKEN_HERE'; | |
$domain = 'YOUR_SLACK_DOMAIN_GOES_HERE'; | |
$channel = '#general'; | |
$bot_name = 'Webhook'; | |
$icon = ':alien:'; | |
$message = 'Your message'; | |
$attachments = array([ | |
'fallback' => 'Lorem ipsum', | |
'pretext' => 'Lorem ipsum', | |
'color' => '#ff6600', | |
'fields' => array( | |
[ | |
'title' => 'Title', | |
'value' => 'Lorem ipsum', | |
'short' => true | |
], | |
[ | |
'title' => 'Notes', | |
'value' => 'Lorem ipsum', | |
'short' => true | |
] | |
) | |
]); | |
$data = array( | |
'channel' => $channel, | |
'username' => $bot_name, | |
'text' => $message, | |
'icon_emoji' => $icon, | |
'attachments' => $attachments | |
); | |
$data_string = json_encode($data); | |
$ch = curl_init('https://'.$domain.'.slack.com/services/hooks/incoming-webhook?token='.$token); | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, array( | |
'Content-Type: application/json', | |
'Content-Length: ' . strlen($data_string)) | |
); | |
//Execute CURL | |
$result = curl_exec($ch); | |
return $result; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Look here for Monolog example : https://github.com/Seldaek/monolog/blob/master/src/Monolog/Handler/SlackWebhookHandler.php