Linux: Call a phone and play a message with asterisk

Hi,

asterisk is a complete VoIP/SIP solution but can also be used as a SIP client to send a prerecorded message. In this example I will use a Fritz!box as the upstream SIP Server for asterisk.

First create an IP Phone and an corrosponding User Account at the Fritz!box.
Open the Fritz!Box GUI, then open the Phonelist

New Phone
Phonelist

New Phone

New Phone
New Phone

LAN IP phone and define a name

Telephonename
phonename

New SIP User

New SIP User for Asterisk
New SIP User for Asterisk

Deselect Phone numbers for which asterisk should not receive incoming calls (this would also work).

Deselect Numbers asterisk shouldn't answer
Deselect Numbers asterisk shouldn’t answer

The new phone in Phonelist

New Phone overview
New Phone overview

Note the username and the internal phone number (here 623).
Then install asterisk

pi@devdeb ~ # sudo su
root@devdeb ~ #apt-get install asterik ffmpeg

Rename the default /etc/asterisk/sip.conf file an create a new one

[general]
bindport=5060
bindaddr=0.0.0.0
srvlookup=yes
language=de
pedantic=yes
allowsubscribe=yes
subscribecontext=default_1
notifyringing=yes
notifyhold=yes
limitonpeers=yes
useclientcode=no
sendcallinfo=yes
dtmfmode=auto
rtpkeepalive=5
permit=192.168.254.0/255.255.255.0
deny=0.0.0.0/0.0.0.0
alwaysauthreject=yes
allowguest=no
localnet=192.168.254.0/255.255.255.0
allow=alaw
sendrpid=yes
trustrpid=no
registertimeout=60
checkmwi=yes
nat=force_rport

register => myAsteriskPhone:MY!!Password@192.168.254.1/623
; virt asterisk SIP phone for outgoing calls
[2000]
type=friend
context=asterisk-phones
secret=SIPPel!
host=dynamic

; For incoming calls, Sectionname must match the register directive
[myAsteriskPhone]
type=friend
context=sip-asterisk
defaultuser=myAsteriskPhone
fromuser=myAsteriskPhone
secret=MY!!Password
host=192.168.254.1
fromdomain=192.168.254.1
qualify=yes

Adjust the host and fromdomain variables with the IP Address of your Fritzbox. And change the localnet and permit properties to your subnet. At the register directive adjust the internal phone number (here 623) noted previously.

Then create a call file /root/mycall.call. Replace 012345678with the number you want to call.

Channel: SIP/012345678@myAsteriskPhone
MaxRetries: 2
RetryTime: 60
WaitTime: 30
Context: asterisk-phones
Extension: 10
Callerid:2000

Move all default extension files

root@devdeb ~ # mkdir /etc/asterisk/extensions_org
root@devdeb ~ # mv /etc/asterisk/extensions.* /etc/asterisk/extensions_org

And create a /etc/asterisk/extensions.conf with sequences what to do if the mycall.call file is used. asterisk-phones is the context and 10 is the extension refered from the mycall.call file.

[others]

[asterisk-phones]
exten => 10,1,Answer()
exten => 10,n,Wait(2)
exten => 10,n,Playback(hello-world)
exten => 10,n,Wait(2)
exten => 10,n,Hangup()

For test purposes start asterisk in foreground. Ignore the load_modules warnings. Importend is the Asterisk Ready prompt.

root@devdeb ~ # asterisk -c
Asterisk Ready.
*CLI>

To test the setup, copy the mycall.call file to /var/spool/asterisk/outgoing

root@devdeb ~ cp /root/mycall.call /var/spool/asterisk/outgoing

This should end up in a call. If the call would accepted a hello world greeting is spoken

root@devdeb ~ # asterisk -c
Asterisk Ready.
*CLI> [Oct 26 16:00:12] NOTICE[2682][C-00000001]: pbx_spool.c:463 attempt_thread: Call completed to SIP/012345678@myAsteriskPhone

You can also use your own speech files. It has to convert in the gsm specific format. ffmpeg creates a WAV and a PCM u-Law. Use one of them. I recommend an seperate folder under

root@devdeb ~ #  mkdir /var/lib/asterisk/sounds/my
root@devdeb ~ #  ffmpeg -i /tmp/mySpeech.mp3 -ar 8000 -ac 1 -ab 64 /var/lib/asterisk/sounds/my/mySpeech.wav
root@devdeb ~ #  ffmpeg -i /tmp/mySpeech.mp3 -ar 8000 -ac 1 -ab 64 -f mulaw /var/lib/asterisk/sounds/my/mySpeech.pcm -map 0:0 -map 0:0

Open /etc/asterisk/extensions.conf and adjust the Playback directive, omit the file extension!

[others]

[asterisk-phones]
exten => 10,1,Answer()
exten => 10,n,Wait(2)
exten => 10,n,Playback(/var/lib/asterisk/sounds/my/mySpeech)
exten => 10,n,Wait(2)
exten => 10,n,Hangup()

Note: When you change a config file you have to reload asterisk

root@devdeb ~ # /usr/sbin/asterisk -rx 'core reload'

Or

root@devdeb ~ # systemctl reload asterisk

If all is running as expected start asterisk as daemon

root@devdeb ~ # killall asterisk
root@devdeb ~ # systemctl enable asterisk
root@devdeb ~ # systemctl start asterisk

Michael

One thought on “Linux: Call a phone and play a message with asterisk”

Leave a Reply