Asterisk IVR Based Text Messaging

Asterisk IVR Based Text Messaging

texting phone woman 27a53

Hi, I wish to tell little about why I took to offering a voice-activated text messaging system with Asterisk. It is simply because I build telephone systems and calling cards that cater to prisons and mental institutions. These Secure locations do not allow cell phones and the internet. So, I build IVR texting to let authorized customers send and receive text messages. My unlimited monthly customers will have an option of shared texting, where they all share the same number and can send and receive messages from their loved ones.
In my setup, I use asterisk 11 A2billing 2.x and FreePBX on one server, but I will try to recreate and keep my instructions as basic as possible.

Install google tts and speach to text:

cd /usr/src
wget https://codeload.github.com/zaf/asterisk-speech-recog/legacy.tar.gz/master ; tar -zxvf master ; cd zaf* ; cp
speech-recog.agi /var/lib/asterisk/agi-bin/speech-recog.agi ; chown asterisk:asterisk /var/lib/asterisk/agi-bin/speech-
recog.agi
wget http://sourceforge.net/projects/mpg123/files/mpg123/1.14.2/mpg123-1.14.2.tar.bz2/download  \; tar -xjvf download \; cd mpg123* \; ./configure \; make \; make install \;  rm -rf  mpg123* \; ln -s /usr/local/bin/mpg123 /usr/bin/mpg123 \; wget https://github.com/downloads/zaf/asterisk-googletts/asterisk-googletts-0.6.tar.gz / \; tar xvfz asterisk-googletts-0.6.tar.gz \; cd asterisk-googletts* \; cp googletts.agi  /var/lib/asterisk/agi-bin/googletts.agi \; wget https://github.com/zaf/asterisk-speech-recog/tarball/master \; tar xvfz master \; cd zaf* \; cp -R * /var/lib/asterisk/agi-bin/ \;  chown -R asterisk:asterisk  /var/lib/asterisk/agi-bin/ \; cpan JSON -y

You would still need to do the following:

  1. load asterisk module format_sln
  2. get google api for speech to text and inter it in /var/lib/asterisk/agi-bin/speech-recog.agi
  3. I had created a folder  /etc/asterisk/MSG/ –  where everything for texting is going

In the MSG folder  i have an VIP file which is used for Authentication.  The file is formated as below:

13564671535353536|Allen Ford|15593549899|
245646788|Alvin Quarles|15593549899|
217743567|William Officer|15593549899|

Now it is dialplan time:

; first need handle recieveing incomming text.  (they come in the form as sip messages on my example)
; sip setting also need to have


[from-incomming-text]
exten => _+X.,1,Goto(${EXTEN:1},100)
exten => _X.,1,Goto(${EXTEN},100)
exten => _X.,100(sms),Verbose(2,Incoming SMS from ${MESSAGE(from):5:11}. Message is "${MESSAGE(body)}".) ; Get Data
exten => _X.,n,MessageSend(sip:${EXTEN},"${CALLERID(name)}"${MESSAGE(from)})   ;  if you wanted to send to a phone ,  i suppose this would be helpfull
exten => _X.,n,System(echo "${MESSAGE(body)}" >> /etc/asterisk/MSG/'${EXTEN}')   ;  in my shared text hosting i save all text to a file nambed by the number it came in on.


[Main Calling Card  IVR] 
include => General-text-message
exten => _X.,1,SIPDtmfMode(RFC2833) ; forcing dtmf
exten => _X.,n,agi(googletts.agi," Press 1 To Authenticate,  Press 2 For Text Messages,  ",en,any) ; made it simple
exten => _X.,n,WaitExten()
exten => i,1,agi(googletts.agi,"Invalid extension.",en)
exten => i,n,goto(18775743418,1)
exten => t,1,agi(googletts.agi,"Request timed out.",en)
exten => t,n,goto(18775743418,1)
 ; Option 1
exten => 1,1,Wait(2)
exten => 1,n,agi(googletts.agi," Please enter your 10 digit Pin number followed by the Pound button. ",en,)
exten => 1,n,Read(AccessCode,"silence/1",10,)
exten => 1,n,agi(googletts.agi," You entered ${AccessCode} .",en,) 
exten => 1,n,GotoIf($[${ISNULL(${SHELL(cat /etc/asterisk/MSG/VIP | grep  "${AccessCode}")})}]?:authenticated)
exten => 1,n,agi(googletts.agi,"Your Authentication has Failed Please try again.",en,)
exten => 1,n,goto(1,1)
exten => 1,n(authenticated),agi(googletts.agi,"Authentication was successful and we will save it for the duration of this call",en,)
exten => 1,n,goto(a2billing-callingcard-options2,18775743418,1)
; Option 2
exten => 2,1,Wait(2)
exten => 2,n,GotoIf($["${AccessCode}" = ""]?Alarm:start)
exten => 2,n(start),agi(googletts.agi," Texting is under Construction and testing - but only available for unlimited customers and customers with paid option ",en,any)
exten => 2,n,goto(General-text-message,18775743418,1)
exten => 2,n(Alarm),agi(googletts.agi," You must be authenticated to come here",en,any)
exten => 2,n,goto(a2billing-callingcard-options2,18775743418,1)

[General-text-message]
exten => _X.,1,SIPDtmfMode(RFC2833)

exten => _X.,n,Set(msg="Press Star to send a Text Message.  Press 1 to hear the newest message and 2 though 9 to hear the older messages.  ",en,any)
exten => _X.,n(startmsg),agi(googletts.agi,"${msg} , End of message .. ",en,any)
exten => _X.,n,agi(googletts.agi,"Press your Option Now",en,any)
exten => _X.,n,WaitExten()
exten => i,1,agi(googletts.agi,"Invalid Option.",en)
exten => i,n,goto(18775743418,1)
exten => t,1,agi(googletts.agi,"Request timed out.",en)
exten => t,n,goto(18775743418,1)
exten => 1,1,Wait(2)  
exten => 1,n,Set(text='${SHELL(tail -1 /etc/asterisk/MSG/15593549868 | head -1)}')
exten => 1,n,Set(msg=${text})
exten => 1,n,goto(18775743418,startmsg)
exten => 2,1,Wait(2)
exten => 2,n,Set(text='${SHELL(tail -2 /etc/asterisk/MSG/15593549868 | head -1)}')
exten => 2,n,Set(msg=${text})
exten => 2,n,goto(18775743418,startmsg)
exten => 3,1,Wait(2)
exten => 3,n,Set(text='${SHELL(tail -3 /etc/asterisk/MSG/15593549868 | head -1)}')
exten => 3,n,Set(msg=${text})
exten => 3,n,goto(18775743418,startmsg)
exten => 4,1,Wait(2)
exten => 4,n,Set(text='${SHELL(tail -4 /etc/asterisk/MSG/15593549868 | head -1)}')
exten => 4,n,Set(msg=${text})
exten => 4,n,goto(18775743418,startmsg)
exten => 5,1,Wait(2)
exten => 5,n,Set(text='${SHELL(tail -5 /etc/asterisk/MSG/15593549868 | head -1)}')
exten => 5,n,Set(msg=${text})
exten => 5,n,goto(18775743418,startmsg)
exten => 6,1,Wait(2)
exten => 6,n,Set(text='${SHELL(tail -6 /etc/asterisk/MSG/15593549868 | head -1)}')
exten => 6,n,Set(msg=${text})
exten => 6,n,goto(18775743418,startmsg)
exten => 7,1,Wait(2)
exten => 7,n,Set(text='${SHELL(tail -7 /etc/asterisk/MSG/15593549868 | head -1)}')
exten => 7,n,Set(msg=${text})
exten => 7,n,goto(18775743418,startmsg)
exten => 8,1,Wait(2)
exten => 8,n,Set(text='${SHELL(tail -8 /etc/asterisk/MSG/15593549868 | head -1)}')
exten => 8,n,Set(msg=${text})
exten => 8,n,goto(18775743418,startmsg)
exten => 9,1,Wait(2)
exten => 9,n,Set(text='${SHELL(tail -9 /etc/asterisk/MSG/15593549868 | head -1)}')
exten => 9,n,Set(msg=${text})
exten => 9,n,goto(18775743418,startmsg)
exten => *,1,Wait(2)
exten => *,n,Set(msg='Sending Text Messages is currentlly being worked on..  try again later')
exten => *,n,goto(Sending-text-message,18775743418,1)



 ;  This is the main ivr for sending messages
[Sending-text-message]
exten => _X.,1,SIPDtmfMode(RFC2833)
exten => _X.,n,agi(googletts.agi,"Please press 1 to enter the number you would like to send the text too.  Press 2  to record the message.  Press 3 to send the message",en,any)
exten => _X.,n,WaitExten()
exten => i,1,agi(googletts.agi,"Invalid Option.",en)
exten => i,n,goto(18775743418,1)
exten => t,1,agi(googletts.agi,"Request timed out.",en)
exten => t,n,goto(18775743418,1)
exten => 1,1,Wait(2) 
exten => 1,n,goto(Sending-text-Get-number,18775743418,1)
exten => 2,1,Wait(2) 
exten => 2,n,goto(Sending-text-Get-message,18775743418,1)
[Sending-text-Get-number]
exten => _X.,1,Wait(2) 
exten => _X.,n,agi(googletts.agi,"Dial the 10 digit number now",en,any)
exten => _X.,n,Read(target-sms-number,"silence/1",10,)
exten => _X.,n,agi(googletts.agi,"You entered ${target-sms-number}.",en,) 
exten => _X.,n,agi(googletts.agi,"Press 2 to continue or 1 to re-enter",en,any)
exten => _X.,n,WaitExten()
exten => i,1,agi(googletts.agi,"Invalid Option.",en)
exten => i,n,goto(18775743418,1)
exten => t,1,agi(googletts.agi,"Request timed out.",en)
exten => t,n,goto(18775743418,1)
exten => 1,1,Wait(2) 
exten => 1,n,goto(18775743418,1)
exten => 2,1,Wait(2) 
exten => 2,n,goto(Sending-text-Get-message,18775743418,1)
[Sending-text-Get-message]
exten => _X.,1,agi(googletts.agi,"Please say the text you would like me to send",en,any)
exten => _X.,n(record),agi(speech-recog.agi,en-US)
exten => _X.,n,Verbose(1,Script returned: ${confidence} , ${utterance})
;Check the probability of a successful recognition:
exten => _X.,n(success),GotoIf($["${confidence}" > "0.8"]?playback:retry)
;Retry in case speech recognition wasn't successful:
exten => _X.,n(retry),agi(googletts.agi,"Can you please repeat more clearly?",en)
exten => _X.,n,goto(record)
exten => _X.,n(fail),agi(googletts.agi,"Failed to get speech data.",en)
exten => _X.,n(end),Hangup()
;Playback the text:
exten => _X.,n(playback),agi(googletts.agi,"The text you just said was...",en)
exten => _X.,n,agi(googletts.agi,"${utterance}",en)
exten => _X.,n,agi(googletts.agi,"Press 1 to send and 2 to re-record",en,any)
exten => _X.,n,WaitExten()
exten => i,1,agi(googletts.agi,"Invalid Option.",en)
exten => i,n,goto(18775743418,1)
exten => t,1,agi(googletts.agi,"Request timed out.",en)
exten => t,n,goto(18775743418,startmsg)
exten => 1,1,Wait(2) 
exten => 1,n,goto(Sending-text-Get-sending,18775743418,1)
exten => 2,1,Wait(2) 
exten => 2,n,goto(Sending-text-Get-message,18775743418,1)
[Sending-text-Get-sending]
exten => _X.,1,Wait(2) 
exten => _X.,n,Set(name=${SHELL(cat /etc/asterisk/MSG/VIP | grep -R '${AccessCode}' | cut -d "|" -f2 | tr -d '\n')})
exten => _X.,n,Set(myBandwidthANI=${SHELL(cat /etc/asterisk/MSG/VIP | grep -R '${AccessCode}' | cut -d "|" -f3 | tr -d '\n')})
exten => _X.,n,Set(MESSAGE(from)=15593549869)
exten => _X.,n,NoOp(Message from: ${MESSAGE(from)})
exten => _X.,n,Set(MESSAGE(to)=${target-sms-number})
exten => _X.,n,NoOp(Message to: ${MESSAGE(to)})
exten => _X.,n,Set(MESSAGE(body)="from ${name} @ BMP ${utterance}")
exten => _X.,n,NoOp(Message body: ${MESSAGE(body)})
exten => _X.,n,agi(googletts.agi,"Your Text Message will read as follows",en)
exten => _X.,n,agi(googletts.agi,"from ${name} @ BMP . ${utterance}",en)
exten => _X.,n,agi(googletts.agi,"and be sent too",en)
exten => _X.,n,agi(googletts.agi,"${target-sms-number}",en)
exten => _X.,n,agi(googletts.agi,"press 1 to send now. press 2 to go to the main menu.",en,any)
exten => _X..,n,WaitExten()
exten => i,1,agi(googletts.agi,"Invalid Option.",en)
exten => i,n,goto(18775743418,1)
exten => t,1,agi(googletts.agi,"Request timed out.",en)
exten => t,n,goto(18775743418,1)
exten => 1,1,Wait(2) 
exten => 1,n,MessageSend(sip:1${target-sms-number}@64.136.174.30:5060,<sip:${myBandwidthANI}@166.78.4.183>;)
exten => 1,n,agi(googletts.agi,"Message send status: ${MESSAGE_SEND_STATUS}",en)
exten => 1,n,System(echo "From: ${AccessCode} - ${myBandwidthANI} ${name} , TO: ${target-sms-number} MESSAGE:  ${MESSAGE(body)}" >> /etc/asterisk/MSG/sent-messages)
exten => 1,n,Wait(5) 
exten => 1,n,goto(General-text-message,18775743418,1)


Published on: May 04, 2015

Categories: Asterisk