Asterisk Telegram -

FreePBX Asterisk

Daily Asterisk reports in Telegram

Phone statistics into messenger

Merion Networks

Nowadays people use messengers to help their business quite often, so we also decided to follow a trend and teach our Asterisk IP - PBX how to send daily call reports to a Telegram chat. Call reports include answered and unanswered calls. So, let's start.

Telegram bot creation for Asterisk

First of all, let's create a Telegram bot. You can follow this guide on the link below.

After you completed all the steps to initially create the bot, which were required above you can move to the next step.

PHP - script

The script which will be responsible for report sending in Telegram we will write on PHP. The code is bellow, with all the comments after a # sign. #:

#creating the variables: 
$hostname = "localhost"; #on which host Asterisk is located; 
$username = "username"; #name of the created user whih has an access to the DB;
$password = "password"; #password for the user stated above;
$dbName = "asteriskcdrdb"; #leave it as it is, without changing;
$token = "Telegram_API"; #your API  token for Telegram bot;
$chat_id = "chat_ID"; #chat identifier;
$today = date('y-m-d'); #actual date in (y-m-d) format;
$did = "number_DID"; #you inbound number (DID). For example, 74996491913;
$noanswer = 0; #variable, in which well store the amount of unanswered calls;
#parse call data from SQL
mysql_connect($hostname,$username,$password) OR DIE("Cant 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; }
#send to  Telegram
if ($noanswer != 0) {$percentage = round(100 * ($noanswer / $number));} else #you cant divide by zero, so we need to check it;
$percentage = 0;
$percentage = ''.$percentage.'%';
$hello = "<b>Good evening</b>, dear colleagues! Today, <b>$today</b>, we had   unique calls";
$missed = "Missed - ";
$missed_percent = "Percent of all missed calls  - ";
$arr = array(
$hello => $number,
$missed => $noanswer,
$missed_percent => $percentage,
foreach($arr as $key => $value) {
$txt .= "".$key." <b>".$value."</b>%0A";

Download the script

Put your data into according variables and save the file as asterisktelegram.php. Then, save the file into /home directory.

After this action, execute following commands in the server consile:

chmod 755 /home/asterisktelegram.php 
dos2unix /home/asterisktelegram.php

The last step is to launch script autoexecution via cron. We want to execute this script on weekdays, from Monday till Friday on 19:00. Open cron with following command:

crontab e

Add there the following string:

00 19 * * 1-5 /usr/bin/php /home/asterisktelegram.php

Wait for the end of business day and check the result :)

 Daily Asterisk reports in Telegram