📜 ⬆️ ⬇️

Yet another option to send notifications from Asterisk to Telegram

image

Good afternoon, dear habrazhiteli. Recently, several articles on the integration of Asterisk and Telegram have appeared in Habré: one , two .

I propose to consider another option.

For some reason, these solutions did not suit me.

Something for objective reasons: the use of telegram-cli, according to reviews not too stable, and to use it, you need to log in to the server under your telegram-account, it seemed to me not too convenient and correct.
')
Something subjective: I didn’t want to use php as in option 1 and force employees to write their numbers to the bot.

And, of course, it’s much more interesting to reinvent the wheel yourself, nobody canceled the pleasure of the implementation of their ideas :)

Initial data:


The client has several remote managers with mobile phones, the call goes to Asterisk to the common SIP number, after which it is forwarded via sip trunk to the mobile managers. With this scheme, there are quite a lot of amenities - managers are not obliged to sit in the office, but they can work what is called “in the field” (@boffart sorry for plagiarism :). However, there is one disadvantage that outweighs all the advantages - the inability to see the original Callerid client.

In order to implement the circumvention of this inconvenience, a decision was made to send a message “Incoming call from $ {CALLERID (num)} to $ {EXTEN}” to the general manager group in the telegram.

So let's get started:


To send notifications about a call to a group, a telegram bot is required. Registering a new bot is a rather trivial process and is very well described here.

After registering the bot, we will write a small python script that will be called from Asterisk and send us telegram notifications.

Used version of python - 2.7

Since I implemented everything on Centos 6.6, in which python 2.6 is used out of the box (you can check your version of python by typing in the python console -V), we first need to install python 2.7. There are two options: installation from rpm-packages and from sources. Consider both.

Installation from source


We will update the system and install the necessary packages:
yum -y update yum groupinstall -y 'development tools' yum install -y zlib-dev openssl-devel sqlite-devel bzip2-devel yum install xz-libs 


Downloading python 2.7 sources:
 wget http://www.python.org/ftp/python/2.7.6/Python-2.7.6.tar.xz xz -d Python-2.7.6.tar.xz tar -xvf Python-2.7.6.tar 


Configuration and installation of python 2.7:
 cd Python-2.7.6 ./configure --prefix=/usr/local/bin (     ,       $HOME) make make altinstall 


Install pip 2.7:
       setuptools wget --no-check-certificate https://pypi.python.org/packages/source/s/setuptools/setuptools-1.4.2.tar.gz tar -xvf setuptools-1.4.2.tar.gz cd setuptools-1.4.2 python2.7 setup.py install   pip curl https://bootstrap.pypa.io/get-pip.py | python2.7 - 


Install from rpm


Add rpm packages, install python, pip:
 rpm -ivh http://dl.iuscommunity.org/pub/ius/stable/Redhat/6/x86_64/epel-release-6-5.noarch.rpm rpm -ivh http://dl.iuscommunity.org/pub/ius/stable/Redhat/6/x86_64/ius-release-1.0-14.ius.el6.noarch.rpm yum clean all yum install python27 yum install python27-pip 


Python of the required version is installed, install the library to work with the Telegram API:

 pip2.7 install pyTelegramBotAPI==2.3.1 

And the script code itself:
 #!/usr/local/bin/python2.7 # -*- coding: utf-8 -*- import telebot import sys token = 'INSERT_YOUR_TOKEN' #    API  group_id = -123456789 #  id ,     ( ,  id  -   ) bot = telebot.TeleBot(token, skip_pending=True) #        ( ) @bot.message_handler(func=lambda message: True, commands=['start']) def start(message): if len(sys.argv) != 1: return bot.send_message(message.chat.id, "ID : " + str(message.chat.id)) print message.chat.id sys.exit() #     ,      if len(sys.argv) == 4: callerid = str(sys.argv[1]) exten = str(sys.argv[2]) redirectnum = str(sys.argv[3]) bot.send_message(group_id, "   " + callerid + "\n  " + exten + "\n   " + redirectnum) #   ,          if len(sys.argv) == 1: bot.polling(none_stop=True) 


The case remains for the small: before receiving an incoming call, call the script from the Asterisk dialplan. I have it, for example, this:

vim /etc/asterisk/extensions.conf:
exten => 84951234567,1,Set(CALLERID(num)=+7${CALLERID(num)})
same => n,Answer()
same => n,Playback(hello)
same => n,Set(REDIRECTNUM=+79261234567)
same => n,System(/etc/asterisk/redirector/redirector.py ${CALLERID(num)} +7${EXTEN:1} ${REDIRECTNUM})
same =>n,Dial(SIP/mytrunk/mymobilenumer&SIP/mytrunk/mymobilenumber2,40,tTm(default))
same =>n,Hangup()


With an incoming call, managers receive a similar message to the telegram group: image

For convenience, habrovchan designed everything in github .

In conclusion, I would like to note that implementing this project was very entertaining, and the result was quite good. Maybe this implementation will be useful to someone.

Author: Southbridge Asterisk'er Mikhail Komov.

Source: https://habr.com/ru/post/329668/


All Articles