├── LICENSE ├── README.md ├── slack.json └── slack.php /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Ilham Rizqi Sasmita 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # whmcs-hook-slack 2 | https://ilhamrizqi.com 3 | 4 | WHMCS hook to Slack. Send notification to slack channel or user on ticket open and user reply. 5 | 6 | ## Installation 7 | 8 | Just copy `slack.php` and `slack.json` to `$WHMCS_ROOT/includes/hooks` directory. 9 | 10 | ## Configuration 11 | 12 | Edit file `slack.json` and change `hook_url` to your slack hook url. 13 | 14 | ```json 15 | { 16 | "hook_url" : "your_slack_hook_url", 17 | "username" : "WHMCS", 18 | "emoji" : ":loudspeaker", 19 | "channel" : "#whmcs", 20 | "adminuser" : "your_whmcs_admin_username" 21 | } 22 | ``` 23 | Explanation 24 | 25 | * `hook_url`: your slack's hook URL 26 | * `username`: whatever you want to display in slack channel 27 | * `emoji`: emoji icon 28 | * `channel`: Slack channel to receive the notification 29 | * `adminuser`: WHMCS admin username to call WHMCS API admin function 30 | 31 | ## Done 32 | 33 | Now, try open ticket in the client area. You should receive notification from WHMCS every ticket open and user reply. -------------------------------------------------------------------------------- /slack.json: -------------------------------------------------------------------------------- 1 | { 2 | "hook_url" : "your_slack_hook_url", 3 | "username" : "WHMCS", 4 | "emoji" : ":loudspeaker:", 5 | "channel" : "#whmcs", 6 | "adminuser" : "your_whmcs_admin_username" 7 | } -------------------------------------------------------------------------------- /slack.php: -------------------------------------------------------------------------------- 1 | 7 | * @copyright Copyright (c) Ilham Rizqi Sasmita 8 | * @license MIT License 9 | * @version $Id$ 10 | * @link https://github.com/ilhamrizqi/whmcs-hook-slack 11 | */ 12 | 13 | 14 | 15 | if (!defined("WHMCS")) 16 | die("This file cannot be accessed directly"); 17 | 18 | function get_client_name($clientid) 19 | { 20 | $json = file_get_contents(dirname(__FILE__) ."/slack.json"); 21 | $config = json_decode($json, true); 22 | $adminuser = $config['adminuser']; 23 | 24 | $client = ""; 25 | $command = "getclientsdetails"; 26 | $values["clientid"] = $clientid; 27 | $values["pid"] = $pid; 28 | 29 | $results = localAPI($command,$values,$adminuser); 30 | 31 | $parser = xml_parser_create(); 32 | xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0); 33 | xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1); 34 | xml_parse_into_struct($parser, $results, $values, $tags); 35 | xml_parser_free($parser); 36 | 37 | $data = array(); 38 | if($results["result"] == "success") 39 | { 40 | $client = $results["firstname"]." ".$results["lastname"]; 41 | $client = trim($client); 42 | $company = $results["companyname"]; 43 | if($company != "") 44 | { 45 | $client .= " (".$company.")"; 46 | } 47 | } 48 | else 49 | { 50 | $client = "Error"; 51 | } 52 | 53 | return $client; 54 | } 55 | 56 | function slack_post($text) 57 | { 58 | $json = file_get_contents(dirname(__FILE__) ."/slack.json"); 59 | $config = json_decode($json, true); 60 | $url = $config['hook_url']; 61 | $text = htmlspecialchars_decode($text, ENT_QUOTES | ENT_NOQUOTES); 62 | $payload = array 63 | ( 64 | "text" => htmlspecialchars($text), 65 | "username" => $config["username"], 66 | "icon_emoji" => $config["emoji"], 67 | "channel" => $config["channel"] 68 | ); 69 | 70 | $data = json_encode($payload); 71 | logActivity("Send slack notification:".$text); 72 | 73 | $ch = curl_init($url); 74 | curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); 75 | curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 76 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 77 | curl_setopt($ch, CURLOPT_HTTPHEADER, array( 78 | 'Content-Type: application/json', 79 | 'Content-Length: ' . strlen($data)) 80 | ); 81 | $result = curl_exec($ch); 82 | } 83 | 84 | function hook_slack_ticketopen($vars) 85 | { 86 | 87 | $ticketid = $vars['ticketid']; 88 | $userid = $vars['userid']; 89 | $deptid = $vars['deptid']; 90 | $deptname = $vars['deptname']; 91 | $subject = $vars['subject']; 92 | $message = $vars['message']; 93 | $priority = $vars['priority']; 94 | $name = get_client_name($userid); 95 | 96 | $text = "[ID: ".$ticketid."] ".$subject."\r\n"; 97 | $text .= "User: ".$name."\r\n"; 98 | $text .= "Department: ".$deptname."\r\n\r\n"; 99 | //$text .= "Priority: ".$priority."\r\n"; 100 | $text .= $message."\r\n"; 101 | 102 | slack_post($text); 103 | } 104 | 105 | function hook_slack_ticketuserreply($vars) 106 | { 107 | $ticketid = $vars['ticketid']; 108 | $userid = $vars['userid']; 109 | $deptid = $vars['deptid']; 110 | $deptname = $vars['deptname']; 111 | $subject = $vars['subject']; 112 | $message = $vars['message']; 113 | $priority = $vars['priority']; 114 | $name = get_client_name($userid); 115 | 116 | $text = "[ID: ".$ticketid."] ".$subject."\r\n"; 117 | $text .= "User: ".$name."\r\n"; 118 | $text .= "Department: ".$deptname."\r\n\r\n"; 119 | //$text .= "Priority: ".$priority."\r\n"; 120 | $text .= $message."\r\n"; 121 | 122 | slack_post($text); 123 | } 124 | 125 | function hook_slack_ticketadminreply($vars) 126 | { 127 | $ticketid = $vars['ticketid']; 128 | $admin = $vars['admin']; 129 | $deptid = $vars['deptid']; 130 | $deptname = $vars['deptname']; 131 | $subject = $vars['subject']; 132 | $message = $vars['message']; 133 | $priority = $vars['priority']; 134 | 135 | $text = "Admin Reply\r\n[ID: ".$ticketid."] ".$subject."\r\n"; 136 | $text .= "Admin: ".$admin."\r\n"; 137 | $text .= "Department: ".$deptname."\r\n\r\n"; 138 | //$text .= "Priority: ".$priority."\r\n"; 139 | $text .= $message."\r\n"; 140 | 141 | slack_post($text); 142 | } 143 | 144 | add_hook("TicketOpen", 1, "hook_slack_ticketopen"); 145 | add_hook("TicketUserReply", 1, "hook_slack_ticketuserreply"); 146 | add_hook("TicketAdminReply", 1, "hook_slack_ticketadminreply"); --------------------------------------------------------------------------------