📜 ⬆️ ⬇️

SMS and SIP gateway - Multiphone, Asterisk, Kannel

This article describes how you can combine the advantages of Asterisk as a PBX or IVR , Kannel as an SMS gateway using one phone number.

For this, I used Freebsd 8.2 on the server, Huawei E171 modem, but you can use any MTS modem or Megafon made by huawei (it was also tested on the E1551 modem), MegaFon's SIM card. Telephone and SJphone were used as softphones.

As already mentioned, Multifon is a SIP connection service to your megaphone number, which allows you to hold several independent lines and settle with IP PBX. Since it has already been said about connecting the service and working with Asterisk, I will give it in brief.

We insert the SIM card into the phone, download the Multifone client , for the beta version distributions are available for linux and Mac OS X.
')
We start the client, register, receive SMS with the code, enter the code, set the password, make sure that everything works with the client.

We turn off the Multifon client (important, since you cannot connect 2 clients to the multifon simultaneously).

We send a USSD command to connect all the capabilities of the Multiphone * 137 # , we confirm the inclusion of the service.

Add to /etc/rc.conf or / etc / default / kannel :
kannel_bearerbox_enable="YES" kannel_smsbox_enable="YES" asterisk_enable="YES" 

Install and configure Asterisk


 cd /usr/ports/net/asterisk make install clean service asterisk start 

There are many descriptions on the Internet, so I’ll only give you the config, based on the official megaphone config with some corrections and explanations.

Asterisk configuration:
 ;#/usr/local/etc/asterisk/sip.conf [general] tcpenable=yes allow=all register => 79271111111@multifon.ru:paSSwoRD:79271111111@193.201.229.35:5060/79271111111 [multifon-out] dtmfmode=inband username=79271111111 type=peer secret=paSSwoRD host=sbc.megafon.ru fromuser=79271111111 fromdomain = multifon.ru port=5060 context=multifon-in insecure=port,invite canreinvite=no ;#           [100];#   sip-       type=friend host=dynamic secret=pass100 context=phones username=100 nat=rfc3581 ;#         [101] type=friend host=dynamic secret=pass101 context=phones username=101 nat=rfc3581 

 ;#/usr/local/etc/asterisk/extensions.conf ;#      [multifon-in] exten=> 79271111111,1,Dial(SIP/100,60,t) ;#  ,   [out] exten=>_7XXXXXXXXXX,1,Dial(SIP/multifon-out/${EXTEN}) exten=>_8XXXXXXXXXX,1,Dial(SIP/multifon-out/${EXTEN}) ;#   8 exten=>_+7XXXXXXXXXX,1,Dial(SIP/multifon-out/${EXTEN}) ;#   +7 ;#exten=>_7XXXXXXXXXX,1,Dial(SIP/${EXTEN}@multifon-out,30,r) ;#  ;#    SIP  [local] exten=>_1XX,1,Dial(SIP/${EXTEN},60,rt) [phones] include => multifon-in ;# ,         include => local include => out 

 ;#/usr/local/etc/asterisk/rtp.conf [general] rtpstart=6001 ;# ,   RTP   6001  rtpend=20000 

Start the console:
 asterisk -vvvvr 

reload configuration using the console:
 reload 

Check the connection to the multi-phone and softphones:
 sip show registry sip show peers 


Now you should enable the phone to work with SIP by requesting the following URL:
https://sm.megafon.ru/sm/client/routing/set?login=79XXXXXXXXXX@multifon.ru&password=PASSWORD&routing=2

We check that inside and outside the calls go through.

Modem preparation


We take out the SIM card from the phone, insert it into the modem.
We insert the modem into the server, check that the devices are found:
 ls /dev | grep cuaU ls /dev | grep ttyU 

If our devices are not visible, then reassemble the kernel, adding
 device u3g 

Install and configure Kannel


It is recommended to install kannel from SVN, however I did not experience any problems with this version, so we will install it from the ports.
 cd /usr/ports/www/kannel make install clean service kannel start 

It is worth noting that Kannel does not create the user himself, so we will do it ourselves:
 adduser 

And he does not care about creating a directory for PID files, we will do it ourselves:
 mkdir /var/run/kannel chown kannel /var/run/kannel 

Kannel configuration:
 #/usr/local/etc/kannel.conf group = core admin-port = 13000 admin-password = " paSsWoRd " log-file = "/var/log/kannel/bearerbox.log" log-level = 1 access-log = "/var/log/kannel/access_kannel.log" store-file = "/var/log/kannel/store_sms" smsbox-port = 13001 dlr-storage = internal sms-resend-retry = 1 group = smsc smsc = at smsc-id = huawei0 modemtype = huawei0 device=/dev/ttyU0.2 #  /dev/ttyU0.3,    sms-center="+79271111111" my-number="+79271111111" keepalive=5 group=modems id=huawei0 name="huawei0" group = smsbox bearerbox-host = localhost sendsms-port = 13003 global-sender = "+79271111111" log-file = /var/log/kannel/smsbox.log log-level = 0 access-log = /var/log/kannel/access_smsbox.log group = sendsms-user username = "user" password = "paSsWoRd" concatenation = true max-messages = 20 # sms  php  group = sms-service keyword = default post-url = "http://localhost/service.php?tel=%p&time=%t&coding=%c&smsc=%i" concatenation = true max-messages = 0 #    sms    smssend 


Restart kannel again
 service kannel restart 

We are convinced that the connection to the SMSC is online at
localhost : 13000 / status

Russian-language sms come and go in the ucs-2be encoding, so we convert them. For example in PHP:
 $text=urlencode(iconv("utf-8","ucs-2be",$text));# 

Sending SMS:
 file_get_contents("http://localhost:13003/cgi-bin/sendsms?smsc=$smsc&username=user&password=paSsWoRd&coding=2&to=$to&text=$text");# 

coding = 2 corresponds again to the ucs-2be encoding.

Do not forget to open ports (including ports for RTP, otherwise there will be no audio / video), if ipfw is enabled, add to the script or config:
 ipfw add allow tcp from any to ${IpOut} 5060 via ${LanOut} #SIP ipfw add allow udp from any to ${IpOut} 5060 via ${LanOut} #SIP ipfw add allow udp from any to ${IpOut} 6000-20000 via ${LanOut} #RTP 


Thus, we have a working SMS gateway and a call center on the same number. In addition, you can organize IVR using Asterisk to receive information in digital form not only via SMS, but also using interactive voice interaction with the end user.

Direct connection of the SIM card to Asterisk is described here , but it is worth noting that, when you first connect, the modem switches to the “only modem” mode and when reconnected, the devices disappear, so we distort the modem and change the settings to
 audio=/dev/cuaU0.1 ;#    data=/dev/cuaU0.2 ;#    

If it is too loud, then we change the parameters to
 rxgain=0 txgain=0 

Good book about Asterisk
Excellent official Kannel manual
Instructions for connecting a megaphone
Config-files from the megaphone (with them I had problems with calls outside)
Kernel config files if device is not detected in FreeBSD

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


All Articles