Skip to content

Instantly share code, notes, and snippets.

@jeystaats
Last active January 13, 2021 23:19
Show Gist options
  • Save jeystaats/844825a5cf514d487ffb to your computer and use it in GitHub Desktop.
Save jeystaats/844825a5cf514d487ffb to your computer and use it in GitHub Desktop.
Sends a party gif to your Slack Channel!
<?php
public function _sendSlack($data)
{
$webhook = 'INSERT WEBHOOK TOKEN HERE';
// get celebrate gif
// http://api.giphy.com/v1/gifs/random?api_key=KEY&tag=party&limit=1
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "http://api.giphy.com/v1/gifs/random?api_key=KEY&tag=party&limit=1",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => "GET",
));
$response = curl_exec($curl);
curl_close($curl);
if($response) {
// Post to slack
$json_response = json_decode($response);
$imageUrl = "" . $json_response->data->fixed_height_small_url . "";
}
else {
$imageUrl = '';
}
$fieldRepo["title"] = "Company";
$fieldRepo["value"] = $data->companyName;
$fieldRepo["short"] = true;
$fieldHash["title"] = "Username";
$fieldHash["value"] = $data->userName;
$fieldHash["short"] = true;
$fields = array($fieldRepo, $fieldHash);
$attachment["fields"] = $fields;
// Build the basic metadata
$attachment["text"] = 'We've just got a new customer!;
$attachment["title"] = 'New Customer!';
$attachment["color"] = "#52AADD";
$attachment["image_url"] = $imageUrl;
// Construct & Encode
$attachments = array($attachment);
$payload["attachments"] = $attachments;
$data = json_encode($payload);
// You can get your webhook endpoint from your Slack settings
$ch = curl_init("https://hooks.slack.com/services/$webhook");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment