#, #FreePBX Asterisk, # ? Telegram!

FreePBX Asterisk

Asterisk and Telegram Integration

Asterisk is writing to your messenger

Merion Networks

We all are witnessing the fact that lots of different companies are moving all the communication into IM (instant messaging), which is helping them to process tickets, client requests and notify all the employees about different events through usual and habitual tools like messengers. Today we are gonna show simple way of Asterisk IP-PBX integration with Telegram messenger and as the result, Asterisk gonna send the messages to the Telegram group-chat.


Step-by-step video


Creating the bot in Telegram

So, let`s start with bot creation. Firstly, we need to find it through the bot search, which is called @BotFather.

  • /newbot - executing the command for new bot creation;
  • Merion Knowledge Base - putting name for our bot;
  • merionkbbot - putting the username for our bot;
  • /setuserpic - this is optional, but you can apply some kind of a profile picture for your bot;
Bot creation in Telegram for Asterisk

On the screenshot above you can see highlighted API token, (331754110:AAHkMNalOz5I_Schh2kvj7ONhRcE8HuKV-c), which will be useful during the next stage. Remember the key.

Open the dialog with your bot and execute the /start command it`s required for bot activation.

Telegram bot activation for Asterisk

Now, add your bot to the group chat. You can do it the same way as you are inviting new participants to the group chat just search for your bot`s username in search and add to the group. Now in your group chat execute the following command:

/join @merionkbbot

In your case, it will be username of your bot after @ symbol. Then you need to open following URL in web-browser:

https://api.telegram.org/botXXXXXXXXXXXXXXXXXXXXXXX/getUpdates

XXXXXXXXXXXXXXXXXXXXXX - your token. In our case it looks like this:

Get the chat_id for Telegram bot

Lets get the id of your group chat in our case its b>-4455667. Lets start Asterisk script creation.


Script creation om Asterisk

Script is written on PHP and itll be called from Asterisk dialplan through AGI. Script content is below:

#!/usr/bin/php -q
<?php
require('phpagi.php'); 
$agi = new AGI(); 
$cid = $agi->request['agi_callerid'];
$phoneFieldset = "Incoming call from number: ";
$token = "331754110:AAHkMNalOz5I_Schh2kvj7ONhRcE8HuKV-c";
$chat_id = "-4455667";
$arr = array(
$phoneFieldset => $cid,
);
foreach($arr as $key => $value) {
$txt .= "".$key." ".$value."%0A";
};
fopen("https://api.telegram.org/bot{$token}/sendMessage?chat_id={$chat_id}&parse_mode=html&text={$txt}","r");
?>

You need to add your own data in $token and $chat_id variables. Save the script with telegram.php name and put it into /var/lib/asterisk/agi-bin/ directory. Then, you need to give it proper execution rights and convert it into unix format:

chmod 755 /var/lib/asterisk/agi-bin/telegram.php
chown asterisk:asterisk /var/lib/asterisk/agi-bin/telegram.php
dos2unix /var/lib/asterisk/agi-bin/telegram.php
In advance, please save your PHP-file in UTF-8(without BOM)

Now we need to adapt the script in dialplan. We are gonna send notifications about calls on entry point on SIP-trunk. So, you need to add following string to its context:

exten => _.,n,AGI(telegram.php)

Save, restart dialplan, call and check:

Asterisk and Telegram integration is working!

?