Back to Home

Google translate + Asterisk IVR

asterisk · google · IVR · sh

Google translate + Asterisk IVR

    I thought for a long time about which blog to post and decided that here it was the most suitable place for him. If only because the main idea of ​​the topic "sh - can do anything."

    An interesting topic was asked in this topic - to implement IVR for * using a synthesizer from Google Translate.

    In general, I did not even plan to do this, but it became interesting to me.


    And the first thing I did, I got to find out how Google talks. Speaks well, but only 100 characters. However, this is quite enough to create an IVR. Satisfied with the first result, I set off in search of how to cut this voice. A short search brought me to the option. I translate.google.com/translate_tts?q=Текст&tl=ru
    bumped into the browser with him and got an mp3 with the spoken text. Even more inspired, I put this line in wget.

    [utfadm@SIP:/var/lib/asterisk]> wget "http://translate.google.com/translate_tts?q=текст&tl=ru"
    --2011-12-01 13:24:53-- translate.google.com/translate_tts?q=%D1%82%D0%B5%D0%BA%D1%81%D1%82&tl=ru
    Распознаётся translate.google.com (translate.google.com)... 173.194.32.225, 173.194.32.234, 173.194.32.235, ...
    Подключение к translate.google.com (translate.google.com)|173.194.32.225|:80... соединение установлено.
    HTTP-запрос отправлен. Ожидание ответа... 403 Forbidden
    2011-12-01 13:24:53 ОШИБКА 403: Forbidden.

    --2011-12-01 13:24:53-- translate.google.com/translate_tts?q=%D1%82%D0%B5%D0%BA%D1%81%D1%82&tl=ru
    Повторное использование соединения с translate.google.com:80.
    HTTP-запрос отправлен. Ожидание ответа... 403 Forbidden
    2011-12-01 13:24:53 ОШИБКА 403: Forbidden.


    Then the first bummer was waiting for me. However, after a little thought, it was thought that in the corporation of good people are not stupid, and so they won’t give wget mp3shechki. But they give it to the browser ...

    So, we will disguise ourselves as a browser. Hmm ... the file length is zero. And if so And so it works ... We think, think, think ... Maybe Russian characters coming from lynx are not taken for Russian? Then change the user-agent to the one with which the Russian letters work exactly. Oh, it’s better ... only the name of the file is somehow clumsy in its correction, the -O key will help us, and the name will be set as needed. So, now that we have learned how to receive voice files, we need to learn how to do this *. To do this, write a small script

    [utfadm@SIP:/tmp]> wget -U "Lynx 1.2.3.4" "http://translate.google.com/translate_tts?q=текст&tl=ru"
    --2011-12-01 13:27:22-- translate.google.com/translate_tts?q=%D1%82%D0%B5%D0%BA%D1%81%D1%82&tl=ru
    Распознаётся translate.google.com (translate.google.com)... 74.125.232.1, 74.125.232.10, 74.125.232.11, ...
    Подключение к translate.google.com (translate.google.com)|74.125.232.1|:80... соединение установлено.
    HTTP-запрос отправлен. Ожидание ответа... 200 OK
    Длина: 0 [audio/mpeg]
    Сохранение в каталог: ««translate_tts?q=\321%82ек\321%81\321%82&tl=ru»».

    [ <=> ] 0 --.-K/s за 0s

    2011-12-01 13:27:22 (0,00 B/s) - «translate_tts?q=\321%82ек\321%81\321%82&tl=ru» saved [0/0]




    [utfadm@SIP:/tmp]> wget -U "Lynx 1.2.3.4" "http://translate.google.com/translate_tts?q=text&tl=ru"
    --2011-12-01 13:29:59-- translate.google.com/translate_tts?q=text&tl=ru
    Распознаётся translate.google.com (translate.google.com)... 74.125.232.2, 74.125.232.11, 74.125.232.12, ...
    Подключение к translate.google.com (translate.google.com)|74.125.232.2|:80... соединение установлено.
    HTTP-запрос отправлен. Ожидание ответа... 200 OK
    Длина: 4421 (4,3K) [audio/mpeg]
    Сохранение в каталог: ««translate_tts?q=text&tl=ru»».

    100%[===================================================================================================================>] 4 421 --.-K/s за 0s

    2011-12-01 13:29:59 (95,5 MB/s) - «translate_tts?q=text&tl=ru» saved [4421/4421]







    [utfadm@SIP:/tmp]> /usr/local/bin/wget -U "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5" "http://translate.google.com/translate_tts?q=текст&tl=ru"
    --2011-12-01 13:32:27-- translate.google.com/translate_tts?q=%D1%82%D0%B5%D0%BA%D1%81%D1%82&tl=ru
    Распознаётся translate.google.com (translate.google.com)... 173.194.32.225, 173.194.32.234, 173.194.32.235, ...
    Подключение к translate.google.com (translate.google.com)|173.194.32.225|:80... соединение установлено.
    HTTP-запрос отправлен. Ожидание ответа... 200 OK
    Длина: 4421 (4,3K) [audio/mpeg]
    Сохранение в каталог: ««translate_tts?q=\321%82ек\321%81\321%82&tl=ru.1»».

    100%[===================================================================================================================>] 4 421 --.-K/s за 0s

    2011-12-01 13:32:27 (103 MB/s) - «translate_tts?q=\321%82ек\321%81\321%82&tl=ru.1» saved [4421/4421]



    translate_tts?q=?%82ек?%81?%82&tl=ru.1





    #!/bin/sh
    `ls /var/lib/asterisk/festivalcache/$2.gsm`
    if [ $? -eq 1 ]; then
    NAME=/var/lib/asterisk/festivalcache/$2
    /usr/local/bin/wget -U "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5" "http://translate.google.com/translate_tts?q=$1&tl=ru" -O $NAME.mp3
    /usr/local/bin/mpg123 -w "$NAME.wav" "$NAME.mp3"
    echo "Converting from wav to gsm"
    /usr/local/bin/sox -t wav "$NAME.wav" -r 8000 -c1 -t gsm "$NAME.gsm"
    rm $NAME.mp3
    rm $NAME.wav
    fi

    Let's go over it.
    With the first line, everything is clear.
    The second one we check for the file, and if it already exists, then the script finishes its work on this.
    If not, then set the file name from the second launch parameter and the full path to it.
    We throw Google a request with the text from the first launch parameter to save the file in mp3.
    Then we drive it to wav, then to gsm.
    Delete intermediate files.

    At the output, we get a gsm format file that wonderfully can play *.

    Well, actually we write something like this in the dialplan:
    exten => 227,1,Set(home=/var/lib/asterisk/festivalcache)
    exten => 227,2,Wait(1)
    exten => 227,n,System(/bin/sh /var/lib/asterisk/tts.sh ". Здравствуйте" "${EXTEN}.${PRIORITY}")
    exten => 227,n,Playback(${home}/${EXTEN}.$[${PRIORITY} - 1])
    exten => 227,n,Set(tic=${STRFTIME(${EPOCH},,%Y%m%d)}))
    exten => 227,n,System(/bin/sh /var/lib/asterisk/dt.sh 1 "${EXTEN}.${tic}")
    exten => 227,n,Playback(${home}/date/${EXTEN}.${tic})
    exten => 227,n,System(/bin/sh /var/lib/asterisk/tts.sh ". Это меню сформированно при помощи Гугл переводчика" "${EXTEN}.${PRIORITY}")
    exten => 227,n,Playback(${home}/${EXTEN}.$[${PRIORITY} - 1])
    exten => 227,n,System(/bin/sh /var/lib/asterisk/tts.sh ". Досвидания." "${EXTEN}.${PRIORITY}")
    exten => 227,n,Playback(${home}/${EXTEN}.$[${PRIORITY} - 1])
    exten => 227,n,Set(tic=${STRFTIME(${EPOCH},,%H%M%S)})
    exten => 227,n,System(/bin/sh /var/lib/asterisk/dt.sh 2 "${tic}")
    exten => 227,n,Playback(${home}/time/${tic})
    exten => 227,n,Hangup()


    Thus, when the script runs, the files 227.3.gsm, 227.8.gsm, 227.10.gsm will be generated and played, and two more about which a little later. the listed files will be generated once, because, as we recall, the script is not executed if the file already exists. It seems to me that even 50-60 Google phrases once generated will not be burdened, but they will give us a full menu.

    Those two files that I promised to tell about later are the current date and time. They are generated and called by lines and accordingly. As you can see from the call they turn to another script. This is a wrapper for the already reviewed script for accessing Google for voice. It looks as follows
    exten => 227,n,Set(tic=${STRFTIME(${EPOCH},,%Y%m%d)}))
    exten => 227,n,System(/bin/sh /var/lib/asterisk/dt.sh 1 "${EXTEN}.${tic}")
    exten => 227,n,Playback(${home}/date/${EXTEN}.${tic})



    exten => 227,n,Set(tic=${STRFTIME(${EPOCH},,%H%M%S)})
    exten => 227,n,System(/bin/sh /var/lib/asterisk/dt.sh 2 "${tic}")
    exten => 227,n,Playback(${home}/time/${tic})



    #!/bin/sh
    if [ $1 -eq 1 ]; then
    q=`date +"Сегодня %d.%m.%Y года"`
    n=date/$2
    fi
    if [ $1 -eq 2 ]; then
    q=`date +"Точное время %H:%M:%S"`
    n=time/$2
    fi
    echo "$q"
    /var/lib/asterisk/tts.sh "$q" $n

    Everything is pretty obvious. The first parameter determines whether we get the date or time, the second file name. Dates are in the folder date time at time. The file name gives us *. tic = $ {STRFTIME ($ {EPOCH} ,,% Y% m% d)}) is a month and a half) and tic = $ {STRFTIME ($ {EPOCH} ,,% H% M% S)} is an hour and a second. Thus, if you do not clean the time folder for a long time, you can accumulate all possible combinations.

    Such a simple wrapper.
    To generate short phrases of any format, you just need to write a wrapper for the first script. Simple and tasteful.

    But I still had the idea of ​​reading files, but after all, a file can have more than 100 characters. So you have to split into several requests. The specifics of the files I need to read are such that they contain many lines, but they are all less than 100 characters. Therefore, I skipped the following script:
    [root@SIP:/var/lib/asterisk]# cat ttsb.sh
    #!/bin/sh
    Source=/var/lib/asterisk/source
    i=0
    splitted=''
    NAME=/var/lib/asterisk/festivalcache/$2
    `ls /var/lib/asterisk/festivalcache/$2.gsm`
    if [ $? -eq 1 ]; then
    for str in `cat $Source/$1`
    do
    i=`expr $i + 1`
    WORKNAME=/var/lib/asterisk/festivalcache/$2.work.$i.mp3
    splitted="$splitted $WORKNAME"
    #echo $WORKNAME
    #echo $str
    #echo SP: $splitted
    /usr/local/bin/wget -U "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5" "http://translate.google.com/translate_tts?q=$str&tl=ru" -O $WORKNAME
    done
    /usr/local/bin/mpg123 -w "$NAME.wav" $splitted
    echo "Converting from wav to gsm"
    /usr/local/bin/sox -t wav "$NAME.wav" -r 8000 -c1 -t gsm "$NAME.gsm"
    rm $splitted
    rm $NAME.wav
    fi

    Well, here everything is also quite obvious. We take the file and feed each line with Google, then we glue all the mp3s into one wav, convert it to gsm and delete the intermediate files. A short pause is heard, so it is good if the lines logically mean a pause between their pronunciation.

    On this, in general, I think you can finish: a principle has been set, a basic script has been set for which you can write wrappers that feed it what you need, there is an example of a wrapper and an example of reading files.

    The plans are to establish the sphinx (I saw the project of Russian grammars with an accuracy of 96%), feed the result of processing to Google, translate it into another language and pronounce it by Google. I really don’t know why.

    Read Next