Connect your Asterisk telephony system to your Telegram account

Telegramify you telephony system
Telegram is very popular in my country and Im usually using it to contact my friends or to take part in discussions about my hobbies usually asterisk and linux in its conference rooms .
Last night I thought it may be very exciting to connect my asterisk telephony system to telegram . Now Im Using Gtalk to get notified when some one calls me and I can access the recorded sound files via URL in my server .


So I want this feature to be running with my Telegram account and of course I want to send my conversations recorded files to Telegram instead of viewing via http URL . Maybe I send my voicemail files as well :) So Lets Start by installing amazing telegram-cli package in CENTOS 6 to access my telegram account from linux CLI .

1- Installing Telegram-cli  in CENTOS 6.X
Telegram-cli is using Python version 2.7 and above and CENTOS 6.X by default uses Python 2.6 so First Step is to install Python 2.7 . you can check the current installed python version by Python -V command .

1.1. Installing Python 2.7

yum groupinstall "Development tools"
yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel
cd /usr/local/src/
wget --no-check-certificate https://www.python.org/ftp/python/2.7.6/Python-2.7.6.tar.xz
tar xf Python-2.7.6.tar.xz
cd Python-2.7.6
./configure --prefix=/usr/local
make && make alt install
( you may need to create a symbolic link to new version of Python ) 

1.2. Installing Jansson : 
cd /usr/local/src/ && wget http://www.digip.org/jansson/releases/jansson-2.5.tar.gz
tar -zxf jansson-2.5.tar.gz
cd jansson*
./configure --prefix=/usr/ && make clean && make && make install && ldconfig 

1.3. Installing other perquisites for telegram-cli package 
yum install lua-devel openssl-devel libconfig-devel readline-devel libevent-devel expect git

1.4. Downloding and Installing telegram-cli package :
git clone --recursive https://github.com/vysheng/tg.git && cd tg
./configure
make

2 - Setting up the account in telegram-cli
/usr/local/src/tg/bin/telegram-cli -k /usr/local/src/tg/tg-server.pub  -W


Now you can send a message with msg command : 
msg Omid_Mohajerani This is test message

But we need to send message from Linux CLI so we use Bash 

3 - Bash Script to send message/file to telegram via Linux CLI

3-1 . Send telegram message script ( sendmsg.sh ) 

#!/bin/bash
to=$1
msg=$2
tgpath=/usr/local/src/tg
cd ${tgpath}
(echo "safe_quit") | ${tgpath}/bin/telegram-cli -k ${topath}/tg-server.pub -U root -W -e "msg $to $msg"

3-2 . Send file to telegram script ( sendfile.sh)

#!/usr/bin/expect
set timeout 2
set msg [lindex $argv 1]
set dest [lindex $argv 0]
spawn /usr/local/src/tg/bin/telegram-cli -k /usr/local/src/tg/tg-server.pub  -W
expect "> "
sleep 10
send "\rsend_file $dest $msg\r"
expect "> "
expect eof

3.3 - Set permission for scripts
chmod +x sendmsg.sh
chmod +x sendfile.sh

3.4 - Test it Please ...
./sendmsg Omid_Mohajerani "Hello my telephony Telegram "
./sendfile Omid_Mohajerani record.wav 

4 - Wow its Asterisk time :) We can simply use system application in asterisk dialplan to run our shell scripts . Its better to use script in h extension because it takes some time to send message and of course conversions recorded files are generating at the end of call . 

exten => h,1,System(/usr/local/src/sendmsg.sh "Omid_Mohajerani" "'${CALLERID(num)}is calling Miniatel on ${STRFTIME(${EPOCH},,%Y/%m/%d)}-${STRFTIME(${EPOCH},,%H:%M:%S)}'" )

And This is the result . To be honest Now I prefer to use gtalk as it is native support in asterisk and seems more stable . 





Resources : 






Comments