Thursday, June 2, 2016

Asterisk PBX and IBM Watson Text to Speech

Evaluating IBM Watson Text to Speech API with Asterisk PBX. Using a Bash script to create and format the recording. Script uses curl and avconv commands. This is running on Ubuntu 14.04 with Asterisk 11.7.0.

Bash script /usr/share/asterisk/agi-bin/tts_ibm.sh for Asterisk Gateway Interface (AGI)

#!/bin/bash
#
# Use IBM Watson text to speech service for Asterisk.
#
text=""
regex="200 result=1 \((.*)\)"
checkresults() {
  while read line
  do
    logger "$0 $line"
    case ${line:0:4} in
      "200 " ) echo $line >&2
               if [[ $line =~ $regex ]]
               then
                 text="${BASH_REMATCH[1]}"
               fi
               return;;
      "510 " ) echo $line >&2
               return;;
      "520 " ) echo $line >&2
              return;;
      *      ) echo $line >&2;;
    esac
  done
}
echo "GET VARIABLE text0"
checkresults
textHash="$(/bin/echo $text | md5sum | awk '{print $1}')"
originalFile="/tmp/$textHash"
finalFile="/tmp/final$textHash.wav"
logger "$0 Hash: $textHash original: $originalFile file: $finalFile"
if [ -f $finalFile ]
then
  logger "$0 File $originalFile already exists. Not contacting IBM."
else
  logger "$0 Creating file $originalFile. Contacting IBM."
  /usr/bin/curl -s -X POST -u USERNAME:PASSWORD \
    --header "Content-Type: application/json" \
    --header "Accept: audio/wav" \
    --data "{\"text\":\"$text\"}" \
    --output "$originalFile" \
    "https://stream.watsonplatform.net/text-to-speech/api/v1/synthesize"
  /usr/bin/avconv -loglevel quiet -y -i $originalFile -ar 8000 -ac 1 -ab 64 $finalFile
fi
fileWithoutExtension=${finalFile%.*}
logger "$0 Stream file $fileWithoutExtension"
echo "STREAM FILE $fileWithoutExtension \"\""
checkresults
exit 0;

Dial plan snippet /etc/asterisk/extensions.conf

[PremiseVisitSurvey]
exten => s,1(StartPremiseVisitSurvey),Wait(0.3)
  same => n,Set(text0=This is ABC Company with a five question survey about your recent service visit. After each question, press 1 for yes, or press 2 for no.)
  same => n,Set(randomNumber=${RAND()})
  same => n,agi(tts_ibm.sh)
  same => n,Set(text0=Did your recent ABC Company service visit meet your satisfaction?)
  same => n,agi(tts_ibm.sh)
  same => n,Playback(beep)
  same => n,Read(response,,1)
  same => n,System(/opt/scripts/postIvrResponse.sh ${phoneNumber} ${CONTEXT}_1_${randomNumber} ${response} ${accountNumber} ${orderNumber})
  same => n,Set(text0=Were you satisfied with the timeliness of the service?)
  same => n,agi(tts_ibm.sh)
  same => n,Playback(beep)
  same => n,Read(response,,1)
  same => n,System(/opt/scripts/postIvrResponse.sh ${phoneNumber} ${CONTEXT}_2_${randomNumber} ${response} ${accountNumber} ${orderNumber})
  same => n,Set(text0=Were you satisfied with the quality of the service?)
  same => n,agi(tts_ibm.sh)
  same => n,Playback(beep)
  same => n,Read(response,,1)
  same => n,System(/opt/scripts/postIvrResponse.sh ${phoneNumber} ${CONTEXT}_3_${randomNumber} ${response} ${accountNumber} ${orderNumber})
  same => n,Set(text0=Was the ABC Company technician knowledgeable and professional?)
  same => n,agi(tts_ibm.sh)
  same => n,Playback(beep)
  same => n,Read(response,,1)
  same => n,System(/opt/scripts/postIvrResponse.sh ${phoneNumber} ${CONTEXT}_4_${randomNumber} ${response} ${accountNumber} ${orderNumber})
  same => n,Set(text0=Would you like a follow-up call to discuss your responses to this survey?)
  same => n,agi(tts_ibm.sh)
  same => n,Playback(beep)
  same => n,Read(response,,1)
  same => n,System(/opt/scripts/postIvrResponse.sh ${phoneNumber} ${CONTEXT}_5_${randomNumber} ${response} ${accountNumber} ${orderNumber})
  same => n,Set(text0=Thank you. Good bye.)
  same => n,agi(tts_ibm.sh)
  same => n,hangup()

Call file /var/spool/asterisk/outgoing/premise_visit_survey_123456.call

CallerID: IVR <8431234567>
Channel: SIP/8431234567@abccompany
Context: PremiseVisitSurvey
Extension: s
Archive: Yes
MaxRetries: 0
SetVar: phoneNumber=8431234567
SetVar: accountNumber=123456789
SetVar: name=John Doe
SetVar: recordingName=premise_visit_survey_8431234567_20160602_81045
SetVar: orderNumber=123456