Dial() with announcement option and external script problem

Hi,

I require an extension to call a PSTN phone and connect the call with the person who dialled the extension. I would like to announce vocally who is calling to the PSTN phone so I am using the A option, as per the documentation for Dial().

I am using Google TTS to generate the sound file. I can’t use pre-recorded sound files because callers will be constantly added and removed from the database, it would be too tedious of a process.

I am using an AGI script (googletts.agi) for my other prompts but it doesn’t play well with the A option in Dial().

So I thought I would put together a bash script (because I suck at Perl) that would generate and convert the file before the Dial() application is called.

What is mind-boggling for me is that this script works fine when called from the shell but when I call it with System() in Asterisk it doesn’t generate the expected files and the announcement fails.

Here is the code
extensions.conf

[call-gala]
exten => start,1,agi(googletts.agi,"Calling Gala now, please wait...",en)
    same => n,Set(callername=${DB(auth_cid/${CALLERID(num)})})
    same => n,Set(caller_name_announce="/home/user/caller_name_announce.sh ${callername} en")
    same => n,System(${caller_name_announce}))
    same => n,NoOp(${caller_name_announce})
    same => n,Dial(SIP/voipms/15551231234,,A(/tmp/galacb_name_announce))

exten => h,1,System(rm /tmp/galacb_name_announce.*)

/home/user/caller_name_announce.sh

#!/usr/bin/env bash
# Renders callername using Google TTS API
# call it with script.sh callername lang
wget -O /tmp/galacb_name_announce.mp3 --user-agent="Mozilla/5.0 (X11\; Linux\; rv:8.0) Gecko/20100101" http://translate.google.com/translate_tts?tl=$2\&q=$1+calling

mpg123 -w /tmp/galacb_name_announce.wav /tmp/galacb_name_announce.mp3

sox /tmp/galacb_name_announce.wav -t raw -r 8000 -c 1 /tmp/galacb_name_announce.sln

chown root.root /tmp/galacb_name_announce.*
chmod 644 /tmp/galacb_name_announce.*

exit 0

And the console whining

    -- Executing [1@checklocaltime:1] Goto("SIP/user1-00000082", "call-gala,start,1") in new stack
    -- Goto (call-gala,start,1)
    -- Executing [start@call-gala:1] AGI("SIP/user1-00000082", "googletts.agi,"Calling Gala now, please wait...",en") in new stack
    -- Launched AGI Script /usr/share/asterisk/agi-bin/googletts.agi
    -- Playing '/tmp/5f93250388561e955898ba34bd4e7350' (escape_digits=) (sample_offset 0)
    -- <SIP/user1-00000082>AGI Script googletts.agi completed, returning 0
    -- Executing [start@call-gala:2] Set("SIP/user1-00000082", "callername=someuser") in new stack
    -- Executing [start@call-gala:3] Set("SIP/user1-00000082", "caller_name_announce="/home/user/caller_name_announce.sh someuser en"") in new stack
    -- Executing [start@call-gala:4] System("SIP/user1-00000082", ""/home/user/caller_name_announce.sh someuser en")") in new stack
    -- Executing [start@call-gala:5] NoOp("SIP/user1-00000082", ""/home/user/caller_name_announce.sh someuser en"") in new stack
    -- Executing [start@call-gala:6] Dial("SIP/user1-00000082", "SIP/voipms/15551231234,,A(/tmp/galacb_name_announce)") in new stack
  == Using SIP RTP CoS mark 5
    -- Called SIP/voipms/15551231234
    -- SIP/voipms-00000083 is making progress passing it to SIP/user1-00000082
    -- SIP/voipms-00000083 answered SIP/user1-00000082
[Jan  2 18:34:50] WARNING[28795][C-00000069]: file.c:698 ast_openstream_full: File /tmp/galacb_name_announce does not exist in any format
[Jan  2 18:34:50] WARNING[28795][C-00000069]: file.c:1014 ast_streamfile: Unable to open /tmp/galacb_name_announce (format (ulaw)): No such file or directory
[Jan  2 18:34:50] ERROR[28795][C-00000069]: app_dial.c:2758 dial_exec_full: error streaming file '/tmp/galacb_name_announce' to callee
    -- Remotely bridging SIP/user1-00000082 and SIP/voipms-00000083
    -- Executing [h@call-gala:1] System("SIP/user1-00000082", "rm /tmp/galacb_name_announce.sln") in new stack
  == Spawn extension (call-gala, start, 6) exited non-zero on 'SIP/user1-00000082'

I think the bash script is correct because when I run it beforehand in a shell and comment these lines in extensions.conf

    same => n,Set(caller_name_announce="/home/user/caller_name_announce.sh ${callername} en")
    same => n,System(${caller_name_announce}))

Then it plays the announcement fine.
Console output when file generated from the shell

    -- Executing [1@checklocaltime:1] Goto("SIP/user1-00000076", "call-gala,start,1") in new stack
    -- Goto (call-gala,start,1)
    -- Executing [start@call-gala:1] AGI("SIP/user1-00000076", "googletts.agi,"Calling Gala now, please wait...",en") in new stack
    -- Launched AGI Script /usr/share/asterisk/agi-bin/googletts.agi
    -- Playing '/tmp/5f93250388561e955898ba34bd4e7350' (escape_digits=) (sample_offset 0)
    -- <SIP/user1-00000076>AGI Script googletts.agi completed, returning 0
    -- Executing [start@call-gala:2] Dial("SIP/user1-00000076", "SIP/voipms/15551231234,,A(/tmp/galacb_name_announce)") in new stack
  == Using SIP RTP CoS mark 5
    -- Called SIP/voipms/15551231234
    -- SIP/voipms-00000077 is making progress passing it to SIP/user1-00000076
    -- SIP/voipms-00000077 answered SIP/user1-00000076
[Jan  2 18:15:36] WARNING[28554][C-00000062]: format_wav.c:112 check_header_fmt: Unexpected frequency mismatch 16000 (expecting 8000)
[Jan  2 18:15:36] WARNING[28554][C-00000062]: file.c:395 fn_wrapper: Unable to open format wav
    -- <SIP/voipms-00000077> Playing '/tmp/galacb_name_announce.slin' (language 'en')
    -- Remotely bridging SIP/user1-00000076 and SIP/voipms-00000077
  == Spawn extension (call-gala, start, 2) exited non-zero on 'SIP/user1-00000076'

It seems that calling the script from within the dialplan is problematic, I have no idea why and I have no idea either on how to remedy it.

What did I overlook?