Skip to content

Instantly share code, notes, and snippets.

@fullo
Forked from alexstone/slack_notification.php
Created August 8, 2014 12:41
Show Gist options
  • Save fullo/1a366b6dc08691fa2925 to your computer and use it in GitHub Desktop.
Save fullo/1a366b6dc08691fa2925 to your computer and use it in GitHub Desktop.

Revisions

  1. fullo revised this gist Aug 8, 2014. 1 changed file with 36 additions and 12 deletions.
    48 changes: 36 additions & 12 deletions slack_notification.php
    Original file line number Diff line number Diff line change
    @@ -1,26 +1,50 @@
    <?php

    // (string) $message - message to be passed to Slack
    // (string) $room - room in which to write the message, too
    // (string) $icon - You can set up custom emoji icons to use with each message
    public static function slack($message, $room = "engineering", $icon = ":longbox:") {
    $room = ($room) ? $room : "engineering";
    class slack {

    /**
    * $message is the text (plus image link) you want to send to slack room
    * $room is the room where the message has to be sent
    * $icon is the icon
    * $username is the name of the bot
    */
    public static function send($message, $room = "abbracciatone", $icon = ":hugme:", $username = "love dispenser")
    {
    $room = ($room) ? $room : "abbracciatone";
    $data = "payload=" . json_encode(array(
    "channel" => "#{$room}",
    "text" => $message,
    "icon_emoji" => $icon
    "icon_emoji" => $icon,
    "unfurl_links" => true,
    "username" => $username
    ));

    // You can get your webhook endpoint from your Slack settings
    $ch = curl_init("WEBHOOK ENDPOINT GOES HERE");
    $ch = curl_init( YOUR_SLACK_URI );
    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);

    // Laravel-specific log writing method
    // Log::info("Sent to Slack: " . $message, array('context' => 'Notifications'));

    return $result;
    }
    }
    }

    $kimono_key = "PUT HERE YOUR KIMONO PRIVATE KEY"
    // the webservice written with kimonolabs retrieve all the images from google with the query "animal hug"
    $data = file_get_contents('https://www.kimonolabs.com/api/aj7qd51y?apikey='.$kimono_key);
    $response = json_decode($data);
    $images = $response->results->images;
    shuffle($images);
    // since the href attribute of google image search is pretty messy
    // i need to clean all the not needed information to retrieve only the image url
    $url = parse_url($images[0]->info->href);
    $query = explode('&', $url['query']);
    $image = explode('=', $query[0]);
    echo slack::send("Showing some love ". $image[1]);

    ?>
  2. Alex Stone created this gist Mar 3, 2014.
    26 changes: 26 additions & 0 deletions slack_notification.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    <?php

    // (string) $message - message to be passed to Slack
    // (string) $room - room in which to write the message, too
    // (string) $icon - You can set up custom emoji icons to use with each message
    public static function slack($message, $room = "engineering", $icon = ":longbox:") {
    $room = ($room) ? $room : "engineering";
    $data = "payload=" . json_encode(array(
    "channel" => "#{$room}",
    "text" => $message,
    "icon_emoji" => $icon
    ));

    // You can get your webhook endpoint from your Slack settings
    $ch = curl_init("WEBHOOK ENDPOINT GOES HERE");
    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);

    // Laravel-specific log writing method
    // Log::info("Sent to Slack: " . $message, array('context' => 'Notifications'));

    return $result;
    }