[PHP] Slack Webhook Class

Slack Webhook 사용을 위한 PHP Class 코드이다.

<?php
/*
https://api.slack.com/incoming-webhooks
https://www.webpagefx.com/tools/emoji-cheat-sheet/
*/

class SLACK
{
    private $webHookUrl;
    private $channel;
    private $userName;
    private $message;
    private $iconEmoji;
    private $iconUrl;
    private $attachments;
    private $attachmentsText;
    private $attachmentsTitle;
    private $attachmentsPreText;
    private $attachmentsColor;
    private $attachmentsAuthor;
    private $attachmentsFields;
    private $attachmentsFooter;

    public function __construct($webHookUrl='', $userName='') {
        $this->webHookUrl        = $webHookUrl;
        $this->userName          = $userName;
        $this->iconEmoji         = '';
        $this->iconUrl           = '';
        $this->message           = '';
        $this->attachments       = array();
        $this->attachmentsFields = array();
    }

    public function setWebHookUrl($webHookUrl) {
        $this->webHookUrl = $webHookUrl;
    }

    public function setChannel($channel) {
        $this->channel = $channel;
    }

    public function setUserName($userName) {
        $this->userName = $userName;
    }

    public function setMessage($message) {
        $this->message = $message;
    }

    private function subString($str, $len=0, $suffix='…') {
        $_str = trim($str);

        if($len > 0) {
            $arrStr = preg_split("//u", $_str, -1, PREG_SPLIT_NO_EMPTY);
            $strLen = count($arrStr);

            if ($strLen >= $len) {
                $sliceStr = array_slice($arrStr, 0, $len);
                $str = join('', $sliceStr);

                $_str = $str . ($strLen > $len ? $suffix : '');
            } else {
                $_str = join('', $arrStr);
            }
        }

        return $_str;
    }

    public function setAttachmentsText($text, $len=0) {
        $this->attachmentsText = $this->subString($text, $len);
    }

    public function setAttachmentsTitle($title) {
        $this->attachmentsTitle = $title;
    }

    public function setAttachmentPreText($pretext) {
        $this->attachmentsPreText = $pretext;
    }

    public function setAttachmentsColor($color) {
        $this->attachmentsColor = $color;
    }

    public function setAttachmentsAuthor($author) {
        $this->attachmentsAuthor = $author;
    }

    public function setAttachmentsFields($title, $value, $short = false)
    {
        $this->attachmentsFields[] = array(
            'title' => $title,
            'value' => $value,
            'short' => $short
        );
    }

    public function setAttachmentsFooter($footer) {
        $this->attachmentsFooter = $footer;
    }

    private function setAttachments() {
        $this->attachments[] = array(
            'pretext'     => $this->attachmentsPreText,
            'title'       => $this->attachmentsTitle,
            'author_name' => $this->attachmentsAuthor,
            'text'        => $this->attachmentsText,
            'color'       => $this->attachmentsColor,
            'fields'      => $this->attachmentsFields,
            'footer'      => $this->attachmentsFooter,
            'mrkdwn_in'   => array('text', 'pretext')
        );
    }

    public function setIconEmoji($iconEmoji) {
        $iconEmoji = strip_tags(trim($iconEmoji));

        if($iconEmoji)
            $this->iconEmoji = $iconEmoji;
    }

    public function setIconUrl($iconUrl) {
        $iconUrl = strip_tags(trim($iconUrl));

        if($iconUrl)
            $this->iconUrl = $iconUrl;
    }

    public function send() {
        if($this->webHookUrl) {
            $this->setAttachments();

            $postData = array(
                'channel'     => $this->channel,
                'username'    => $this->userName,
                'icon_emoji'  => $this->iconEmoji,
                'icon_url'    => $this->iconUrl,
                'text'        => $this->message,
                'attachments' => $this->attachments,
                'mrkdwn'      => true,
                'link_names'  => 1
            );

            $ch = curl_init($this->webHookUrl);
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST,  'POST');
            curl_setopt($ch, CURLOPT_POSTFIELDS,     'payload='.json_encode($postData));
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            $result = curl_exec($ch);
            curl_close($ch);

            return $result;
        } else {
            return 'WebHook URL Error';
        }
    }
}

기존 코드에서 fields 등 몇 가지 메세지 포맷을 추가했다.

편리

PHP와 MariaDB, jQuery 등을 사용해 게시판, 쇼핑몰 솔루션을 개발합니다. 그누보드5와 영카트5 개발에 참여 했습니다. Linux와 Nginx는 물론 WordPress, Git 등에도 관심이 많습니다. 자전거 타기 및 사진 촬영을 취미로 하고 있습니다.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.