Repeat TTS audio n times

Hello Everyone and Happy New Year 2025!

I would like to be able to repeat an automatic message set in dialplan n times. So far, I’ve managed to play an endless loop with the following:

[emergency_112]
exten => _[67]XXXXXXXX,1,NoOP(Llamadas por alarma)
same => n,Set(CHANNEL(language)=es_ES)
same => n,Set(CALLERID(all)=${my_number} <${my_number}>)
same => n,NoOp(CALLERID(all))
same => n(message),agi(googletts.agi,"Mensaje",es)
same => n,Goto(message)
same => n,Hangup(21)

However, I don’t know how to finish the loop. I have checked the documentation and I don’t find a way to cancel the “Goto” line after a specific number of times, although I am not an Asterisk specialist and I am perhaps missing some command or dialplan flow for this. Any suggestion, please?

Cheers,
Sésar

On Monday 06 January 2025 at 16:39:57, sesardelaisla via Asterisk Community
wrote:

I would like to be able to repeat an automatic message set in dialplan n
times.

I don’t find a way to cancel the “Goto” line after a specific number of times

Use a loop variable with some simple arithmetic and GotoIf().

Antony.


“Life is just a lot better if you feel you’re having 10 [small] wins a day
rather than a [big] win every 10 years or so.”

  • Chris Hadfield, former skiing (and ski racing) instructor

                                                Please reply to the list;
                                                      please *don't* CC me.
    

Thank you. Tried this (which it makes sense to me) but it doesn’t work. Message is not repeated. Any advise, please?

[emergencia_real]
exten => _[67]XXXXXXXX,1,NoOP(Llamadas por alarma)
same => n,Set(CHANNEL(language)=es_ES)
same => n,Set(CALLERID(all)=${callerid} <${callerid}>)
same => n,NoOp(CALLERID(all))
same => n,Set(COUNT=1)
same => n(message),agi(googletts.agi,"ATENCIÓN: POR FAVOR, NO CUELGUE.",es)
same => n,Set(TOTAL=${${COUNT}+1})
same => n,GotoIf(${TOTAL}=3?hangup:message)
same => n(hangup),1,Hangup(21)

${${COUNT}+1} attempts to obtain the value of the variable called 1+1. You need to use square brackets, not curly ones. Also TOTAL should be COUNT.

1 Like

For anyone’s reference, after some trial/errors and debugging the diaplan with NoOP’s, I realized that the variable count was considered text, so all “+1” strings were being attached “as is” to the previous result. Finally achieved this with the following using the MATH function:

[emergencia_real]
exten => _[67]XXXXXXXX,1,NoOP(Llamadas por alarma)
same => n,Set(CHANNEL(language)=es_ES)
same => n,Set(CALLERID(all)=${callerid} <${callerid}>)
same => n,NoOp(CALLERID(all))
same => n,Set(count=0)
same => n(message),agi(googletts.agi,"MENSAJE",es)
same => n,Set(count=${MATH(${count}+1,int)})
same => n,NoOP(${count})
same => n,GotoIf($[${count}=3]?hangup:message)
same => n(hangup),Hangup(21)

Message is played three times as expected and then call is hanged up.

On Tuesday 07 January 2025 at 13:12:32, sesardelaisla via Asterisk Community
wrote:

Finally achieved this with the following using the MATH function:

same => n,Set(count=${MATH(${count}+1,int)})

I still think David551’s suggestion of using square brackets would be cleaner:

same => n,Set(count=$[ ${count} + 1 ])

Antony.


A user interface is like a joke.
If you have to explain it, it means it doesn’t work.

1 Like

Agree, but it didn’t work when I tried earlier for whatever reason. I guess I made some mistake with some bracket or parenthesis because now it works with your suggestion. Thank you for taking some time to type the full solution.

For future reference, pasting below the full context dialplan block:

[emergencia_real]
exten => _[67]XXXXXXXX,1,NoOP(Llamadas por alarma)
same => n,Set(CHANNEL(language)=es_ES)
same => n,Set(CALLERID(all)=${callerid} <${callerid}>)
same => n,NoOp(CALLERID(all))
same => n,DBdeltree(lastcallerout)
same => n,set(DB(lastcallerout/lastcallerout)=${EXTEN})
same => n,Set(count=0)
same => n(message),agi(googletts.agi,"WHATEVER MESSAGE",es)
same => n,Set(count=$[ ${count} + 1 ])
same => n,NoOP(${count})
same => n,GotoIf($[${count}=3]?hangup:message)
same => n(hangup),Hangup(21)

Moving the generation of the TTS out of the loop should yield a better UX and save with non-free TTS services (e.g. Azure, Polly, Watson).

I cache all the TTS ‘renderings’ by computing the MD5 of the text and storing the file as '/tts-cache/<provider>/<language>/<voice>/<md5>.wav