Hermes-javascript dialog with js promises

Hi,

I’m trying to use the hermes-javascript library to respond to an intent that checks bikes status (JC Decaux self-service bike rental) on a Raspberry Pi.

I followed this video tutorial and I’m able to answer to the intent velov-global-state like this :

dialog.flow('corentoulf:velov-global-state', (msg,flow) => {
    console.log(msg);
    flow.end();
    return 'Voici l\'état des vélos';
}) 

Now, when I write some more code to actually check bikes status like this :

dialog.flow('corentoulf:velov-global-state', (msg,flow) => {
    console.log(msg)
    getStationStatus()
        .then(function(stationStatus){
            console.log(stationStatus);
            flow.end()
            return stationStatus;
        })
        .catch(function(e){ 
            console.log(e);
            flow.end()
            return 'Hum. Je crois qu\'on a déraillé là...';
        });
})

(here is the this function to check bikes) :

module.exports = {
    getStationStatus : function(){
        return new Promise((resolve, reject) => {
            var favStation = ['6044','3101','3003','3087','6028','3015','3037','3034'];
            var stationStatus = [];
            var apiKeyVelov = process.env.JC_DECAUX_API_KEY
            // réussir une fois sur deux
            favStation.forEach(function(station){
                request('https://api.jcdecaux.com/vls/v3/stations/'+station+'?contract=lyon&apiKey='+apiKeyVelov, function (error, response, stationInfo) {
                    //console.log('error:', error); // Print the error if one occurred
                    error ? reject(error) : "";
                    //console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
                    //console.log('body:', stationInfo); // Print the HTML for the Google homepage.
                    stationInfo = JSON.parse(stationInfo)
                    let num = stationInfo.number
                    let bikes = stationInfo.totalStands.availabilities.bikes;
                    let stands = stationInfo.totalStands.availabilities.stands;
                    let name = stationInfo.name;
                    let lastUpdate = moment(stationInfo.lastUpdate).format('HH:mm:ss (MM/DD)');
                    stationStatus.push(name.split(' - ')[1].split(' / ').join(' ')+', '+bikes+' vélos et '+stands+ ' places.');
                    stationStatus.length == favStation.length ? resolve(stationStatus.join(' ')) : "";
                });
            });       
            resolve(stationStatus);
        })
    }
}

In this case, snips understand the intent, I can see stationStatus output in the log of my node index.js terminal window, but snips won’t say the response and will immediately start listening again, 3 times if I don’t say anything (or until I say stop).

Here is the output of sam watch :

 [08:02:14] [Hotword] detected on site default, for model hey_snips
[08:02:14] [Asr] was asked to stop listening on site default
[08:02:14] [Hotword] was asked to toggle itself 'off' on site default
[08:02:14] [Dialogue] session with id 'c6c09f78-ee95-4dc0-9706-50d95dbcf690' was started on site default
[08:02:14] [AudioServer] was asked to play a wav of 41.1 kB with id '32574176-4096-4da0-a6b5-572f58cdf107' on site default
[08:02:15] [AudioServer] finished playing wav with id '32574176-4096-4da0-a6b5-572f58cdf107'
[08:02:15] [Asr] was asked to listen on site default
[08:02:17] [Asr] captured text "donne moi état des vélos" in 2.0s
[08:02:17] [Asr] was asked to stop listening on site default
[08:02:17] [AudioServer] was asked to play a wav of 93.1 kB with id 'a435937e-00d5-46eb-b060-c73de8beb160' on site default
[08:02:18] [AudioServer] finished playing wav with id 'a435937e-00d5-46eb-b060-c73de8beb160'
[08:02:18] [Nlu] was asked to parse input "donne moi état des vélos"
[08:02:18] [Nlu] detected intent corentoulf:velov-global-state with confidence score 1.000 for input "donne moi état des vélos"
[08:02:18] [Dialogue] New intent detected corentoulf:velov-global-state with confidence 1.000
[08:02:18] [Dialogue] was ask to continue session with id c6c09f78-ee95-4dc0-9706-50d95dbcf690 by saying ''
[08:02:18] [Asr] was asked to stop listening on site default
[08:02:18] [Hotword] was asked to toggle itself 'off' on site default
[08:02:18] [Tts] was asked to say ""
[08:02:18] [Tts] finished speaking with id 'b840cb51-69c5-485a-8c6d-6fc7806052c2'
[08:02:18] [AudioServer] was asked to play a wav of 41.1 kB with id '133ed114-ac03-46bc-9f6a-038ec70f6d59' on site default
[08:02:19] [AudioServer] finished playing wav with id '133ed114-ac03-46bc-9f6a-038ec70f6d59'
[08:02:19] [Asr] was asked to listen on site default
[08:02:24] [Asr] captured text "" in 4.0s
[08:02:24] [Asr] was asked to stop listening on site default
[08:02:24] [AudioServer] was asked to play a wav of 93.1 kB with id 'd6120eaf-cb25-432b-aaa7-d7b7f85c645b' on site default
[08:02:25] [AudioServer] finished playing wav with id 'd6120eaf-cb25-432b-aaa7-d7b7f85c645b'
[08:02:25] [AudioServer] was asked to play a wav of 61.1 kB with id '2b1e0f43-6970-45dd-a805-90edb9d3e61c' on site default
[08:02:25] [AudioServer] finished playing wav with id '2b1e0f43-6970-45dd-a805-90edb9d3e61c'
[08:02:25] [Dialogue] was ask to continue session with id c6c09f78-ee95-4dc0-9706-50d95dbcf690 by saying ''
[08:02:25] [Asr] was asked to stop listening on site default
[08:02:25] [Hotword] was asked to toggle itself 'off' on site default
[08:02:25] [Tts] was asked to say ""
[08:02:25] [Tts] finished speaking with id '745ecfd9-0962-47a0-86a8-a79db55b8c2a'
[08:02:25] [AudioServer] was asked to play a wav of 41.1 kB with id '9f70790e-46bd-4655-86cd-446f7ea5cf1a' on site default
[08:02:26] [AudioServer] finished playing wav with id '9f70790e-46bd-4655-86cd-446f7ea5cf1a'
[08:02:26] [Asr] was asked to listen on site default
[08:02:28] [Asr] captured text "stop" in 2.0s
[08:02:28] [Asr] was asked to stop listening on site default
[08:02:28] [AudioServer] was asked to play a wav of 93.1 kB with id 'a554b31b-bcec-4134-b915-ee1188587dd6' on site default
[08:02:29] [AudioServer] finished playing wav with id 'a554b31b-bcec-4134-b915-ee1188587dd6'
[08:02:29] [Nlu] was asked to parse input "stop"
[08:02:29] [Nlu] detected intent corentoulf:velov-global-state with confidence score 0.054 for input "stop"
[08:02:29] [Dialogue] New intent detected corentoulf:velov-global-state with confidence 0.054
[08:02:29] [Dialogue] was ask to end session with id c6c09f78-ee95-4dc0-9706-50d95dbcf690 by saying 'OK, on arrête là.'
[08:02:29] [Tts] was asked to say "OK, on arrête là."
[08:02:32] [Tts] finished speaking with id '4a38a8e0-836a-4673-b9d0-c7d034f14256'
[08:02:32] [Dialogue] session with id 'c6c09f78-ee95-4dc0-9706-50d95dbcf690' was ended on site default. The session ended as expected
[08:02:32] [Asr] was asked to stop listening on site default
[08:02:32] [Hotword] was asked to toggle itself 'on' on site default

It seems like snips doesn’t wait for the response when It’s coming out with some delay (due to promise function) ?
Am I missing something ?

Thanks for the support !

Hi @corentoulf,

Seems like you are not returning the Promise here:

// Missing "return" keyword before the call:
getStationStatus()
        .then(function(stationStatus){
           /* ... */
        })
        .catch(function(e){ 
           / *... */
        });

Hi @elbywan ,

I’m not sure to understand your response.
There is a return in both .then() and .catch(), that returns the response to say :
in .then() => return stationStatus; (bike status)
in .catch() => return 'Hum. Je crois qu\'on a déraillé là...'; (error response)

What should I return moreover ?

return getStationStatus()

1 Like

Haaaan :slight_smile:

Thank you !