Telegram - ?

Slack - , . , , .

API. IP Asterisk Slack, .


Slack API

, Webhook URL. : , Apps, :

Apps   Slack

, webhook. , Incoming WebHooks Install:

Slack Incoming Webhook

Add Configuration:

Slack Incoming Webhook Add Configuration

, , , Webhook ( Asterisk). #general:

Slack Incoming Webhook Post to Channel

, Webhook URL:

Slack Webhook URL

URL. .


Slack

, : , , Announcement ( ), 15 . , , Asterisk . PHP ( ):

#!/usr/bin/php -q
<?php
#   
error_reporting(0);
require('phpagi.php'); 
$agi = new AGI(); 
$cid = $agi->request['agi_callerid']; 
#   (  )
$webhook_url = 'https://hooks.slack.com/services/_webhook_url';
$channel = "#general";
$username = "Merion Networks";
$icon_url = "http://merionet.ru/logo.png";
#      JSON
$fields = "payload=" . json_encode(array( 
"channel" =>  $channel,
"text" => ",  ! \n - $cid",
"username" => $username,
"icon_url" => $icon_url
));
#  CURL    API Slack
$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);
?>
.txt .php!

, , :

  1. slack.php;
  2. ;
    • $webhook_url - Webhook URL , ;
    • $channel - , ;
    • $username - , ;
    • $icon_url - URL , ;
  3. ;
  4. PHP /var/lib/asterisk/agi-bin slack.php;
  5. ( SSH ) :
  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. Asterisk:
  8. exten => _.,n,AGI(slack.php)

, :

 Asterisk   Slack

! .


Slack

Asterisk Slack. :

<?php
#    ;
function rus_date() {
    $translate = array(
    "am" => "",
    "pm" => "",
    "AM" => "",
    "PM" => "",
    "Monday" => "",
    "Mon" => "",
    "Tuesday" => "",
    "Tue" => "",
    "Wednesday" => "",
    "Wed" => "",
    "Thursday" => "",
    "Thu" => "",
    "Friday" => "",
    "Fri" => "",
    "Saturday" => "",
    "Sat" => "",
    "Sunday" => "",
    "Sun" => "",
    "January" => "",
    "Jan" => "",
    "February" => "",
    "Feb" => "",
    "March" => "",
    "Mar" => "",
    "April" => "",
    "Apr" => "",
    "May" => "",
    "May" => "",
    "June" => "",
    "Jun" => "",
    "July" => "",
    "Jul" => "",
    "August" => "",
    "Aug" => "",
    "September" => "",
    "Sep" => "",
    "October" => "",
    "Oct" => "",
    "November" => "",
    "Nov" => "",
    "December" => "",
    "Dec" => "",
    "st" => "",
    "nd" => "",
    "rd" => "",
    "th" => ""
    );
    
    if (func_num_args() > 1) {
        $timestamp = func_get_arg(1);
        return strtr(date(func_get_arg(0), $timestamp), $translate);
    } else {
        return strtr(date(func_get_arg(0)), $translate);
    }
}
# : 
$hostname = "localhost"; #    Asterisk; 
$username = ""; #  SQL;
$password = ""; #,  ;
$dbName = "asteriskcdrdb"; #  ;
$today = date('y-m-d'); #   SQL - ;
$did = "74996491913"; #   (DID),     ;
$date_rus = rus_date("j F"); #   ;
#   SQL  
mysql_connect($hostname,$username,$password) OR DIE("    ");
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;} #    ;
if ($noanswer != 0) {$percentage = round(100 * ($noanswer / $number));} else {$percentage = 0;}; #  ;
#  Slack
$webhook_url = 'https://hooks.slack.com/services/_webhook_url';
$channel = "#general";
$username = "Merion Networks";
$icon_url = "http://merionet.ru/logo.png";
$fields = "payload=" . json_encode(array(
"channel" =>  $channel,
"text" => "!  , $date_rus,    - $number\n   - $noanswer\n     - $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);
?>
.txt .php!

. SQL , webhook URL , . :

  1. /home/admin/slack_calls.php
  2. :
  3. chmod 755 /home/slack_calls.php
    dos2unix /home/slack_calls.php
  4. cron ( 19:00):
  5. crontab e
    
    00 19 * * 1-5 /usr/bin/php /home/slack_calls.php
    Asterisk  Slack

:)


?