Last active
February 25, 2017 12:06
-
-
Save jeystaats/9d45fb2dd4b6a1aaf0f3c791551e242a to your computer and use it in GitHub Desktop.
Laravel send slack notification
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 | |
$this->notification('Nieuw bericht', 'Er is een nieuw bericht :ghost:', config('slack.color'), | |
[ | |
'title' => "Veld 1", | |
'value' => 'Dingen', | |
'short' => true, | |
], [ | |
'title' => "Veld 2", | |
'value' => 'Nog meer dingen', | |
'short' => true, | |
]); | |
function notification($title, $text = null, $color, ...$fields) { | |
foreach ($fields as $field) { | |
$attachedFields[] = $field; | |
} | |
$attachment["fields"] = $attachedFields; | |
$attachment["title"] = $title; | |
if ($text != null) { | |
$attachment['text'] = $text; | |
} | |
$attachment["color"] = $color; | |
sendSlack($attachment); | |
} |
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 | |
function sendSlack($attachment) { | |
$webhook = config('slack.webhook'); | |
// Construct & Encode | |
$attachments = array($attachment); | |
$payload["attachments"] = $attachments; | |
$payload['username'] = config('slack.user'); | |
$payload['channel'] = config('slack.channel'); | |
$payload['icon_url'] = config('slack.img'); | |
$data = json_encode($payload); | |
// You can get your webhook endpoint from your Slack settings | |
$ch = curl_init($webhook); | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); | |
$result = curl_exec($ch); | |
curl_close($ch); | |
} |
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
SLACK_COLOR = #ffde00 | |
SLACK_WEBHOOK = http://slackwebhook.com/webhook | |
SLACK_CHANNEL = #general | |
SLACK_USER = hotBot | |
SLACK_LOGO_PATH = /img/logo.png |
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 | |
// Put this file in your config folder in a Laravle project | |
return [ | |
'color' => env('SLACK_COLOR'), | |
'webhook' => env('SLACK_WEBHOOK'), | |
'channel' => env('SLACK_CHANNEL'), | |
'user' => env('SLACK_USER'), | |
'img' => env('SLACK_LOGO_PATH'), | |
]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment