Telegram - . ? :)

FreePBX Asterisk

How integrate Asterisk and Slack

High level of corporate culture

Merion Networks

Slack - is really cool corporate messenger, which can broad and increase level of collaboration in your company. When you are creating teams, channels or exchanging private messages - all users are collaborating in a single company information space.

This messenger has really good and convenient API. In this article well tell you how to easily integrate Asterisk PBX and Slack, show how to send notification about incoming call and notification with daily call report.


Slack API

To set up integration we need to use so-called Webhook URL. It is relatively easy to get it - on main interface in Slack press on Apps, as shown on screenshot below.

Apps button in Slack

In new window enter webhook in search line. In search results you will need the application called Incoming WebHooks - click on Install:

Slack Incoming Webhook installation

Click on Add Configuration in information about application.:

Slack Incoming Webhook Add Configuration

During the implementation of your corporate messenger, it will ask where you want to post your messages from Webhook (in our case - from Asterisk). We chose #general channel.:

Slack Incoming Webhook Post to Channel

After making a choice,application will generate Webhook URL for you:

Slack Webhook URL

Copy this URL. Now, moving to notification setup.


Incoming call notification in Slack

Lets assume that call routing is following: before outside caller will be connected to the manager in our company, he is listening to a 15 second announcement. So, when we just received the call from ISP on our Asterisk we will send notifications. Below is PHP-script code, with comments.:

#!/usr/bin/php -q
<?php
# parsing Caller-ID
error_reporting(0);
require('phpagi.php'); 
$agi = new AGI(); 
$cid = $agi->request['agi_callerid']; 
#fill required variables (more details are below)
$webhook_url = 'https://hooks.slack.com/services/your_webhook_url';
$channel = "#general";
$username = "Merion Networks";
$icon_url = "http://merionet.ru/logo.png";
#create an array and convert it to JSON
$fields = "payload=" . json_encode(array( 
"channel" =>  $channel,
"text" => "Colleagues, incoming call! \nCaller - $cid",
"username" => $username,
"icon_url" => $icon_url
));
#performing CURL request to Slack API
$ch = curl_init($webhook_url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
?>
Dont forget to change file extension from .txt to .php!
Download script

And now we will describe step by step what must be done in order to make this script work as it should:

  1. Save script to separate file with the name slack.php;
  2. In script change following variables according to your own data;
    • $webhook_url - state Webhook URL address, which you received before;
    • $channel - channel, where the message will be sent;
    • $username - name, which everyone will see in the chat as a sender name;
    • $icon_url - state picture URL, which will serve as sender icon;
  3. Save the changes;
  4. Upload this PHP script to the following directory /var/lib/asterisk/agi-bin with slack.php file name;
  5. In server console (via SSH) execute following commands::
  6. chmod 755 /var/lib/asterisk/agi-bin/slack.php 
    chown asterisk:asterisk /var/lib/asterisk/agi-bin/slack.php 
    dos2unix /var/lib/asterisk/agi-bin/slack.php
    
  7. Now we need to make changes to Asterisk dialplan:
  8. exten => _.,n,AGI(slack.php)

Now we need to check to see what we've got:

Asterisk and Slack integration

Its working! Now we are moving to call statistics..


Call report in Slack

Lets teach Asterisk how to send daily call reports to Slack. To do so, we need to create another script.:

<?php
#displaying the data in a convenient way ;
#create the variables: 
$hostname = "localhost"; #host with your Asterisk; 
$username = "login"; #SQL username;
$password = "password"; #password for SQL user;
$dbName = "asteriskcdrdb"; #leave without changes;
$today = date('y-m-d'); #create date for SQL-request;
$did = "74996491913"; #put here DID number, which is used on your inbound route;
#parse the call data via SQL
mysql_connect($hostname,$username,$password) OR DIE("Couldnt create the connection");
mysql_select_db($dbName) or die(mysql_error());
$query = "SELECT DISTINCT `clid` ,`dst` ,`duration` , `dstchannel` , `calldate` , `disposition` FROM `cdr` WHERE `calldate` >='$today 00:00:00' AND `calldate` <='$today 23:59:59' AND `did` = '$did' GROUP BY `clid`;" ; 
$res=mysql_query($query) or die(mysql_error());
$number = mysql_num_rows($res);
while ($row=mysql_fetch_array($res)) {
if ($row['disposition'] == 'NO ANSWER') $noanswer = $noanswer + 1;} # count number of unanswered calls;
if ($noanswer != 0) {$percentage = round(100 * ($noanswer / $number));} else {$percentage = 0;}; #calculate the percentage;
#Send to Slack
$webhook_url = 'https://hooks.slack.com/services/your_webhook_url';
$channel = "#general";
$username = "Merion Networks";
$icon_url = "http://merionet.ru/logo.png";
$fields = "payload=" . json_encode(array(
"channel" =>  $channel,
"text" => "Dear colleagues! Today there were $number\n unique calls. \n $noanswer were unanswered. Percentage of missed calles - $percentage%",
"username" => $username,
"icon_url" => $icon_url
));
$ch = curl_init($webhook_url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
?>
Dont forget to change file extension from .txt to .php!
Download the script

All of the comments are inside the code. You only need to state SQL-user and webhook URL and few other parameters, which we described above. After, you will need::

  1. Upload script into /home/admin/slack_calls.php directory
  2. Execute the commands:
  3. chmod 755 /home/slack_calls.php
    dos2unix /home/slack_calls.php
  4. Execute the command for cron (Script will be launched every day at 19:00):
  5. crontab e
    
    00 19 * * 1-5 /usr/bin/php /home/slack_calls.php
Daily call report from Asterisk to Slack

Have a nice time :)


?

 .

P.S. , :)

"",