0

我正在研究smooch-bot-example,我已将其设置并上传到 Heroku Git。我让机器人成功回复。

我被困在下面的场景中。这是我的script.js文件(见我的ADD_MOVIE块)

'use strict';

const Script = require('smooch-bot').Script;


var YtsHelper = require('./libs/YtsHelper.js');
const FirebaseHelper = require('./libs/FirebaseHelper.js');
var firebaseHelperObj = new FirebaseHelper();

module.exports = new Script({

processing: {
    prompt: (bot) => bot.say('Beep boop...'),
    receive: () => 'processing'
},

start: {
    receive: (bot) => {
        return bot.say('Hi! I\'m Smooch Bot!')
            .then(() => 'showUserMenu');
    }
},

showUserMenu: {

        prompt: (bot) => bot.say("Here are the areas I can help you out. %[Add Movie](postback:ADD_MOVIE) %[Serve Food](postback:SERVE_FOOD)"),
        receive: () => 'finish'
},

ADD_MOVIE : {

    prompt: (bot) => bot.say('Enter movie name or keywords you want to search please.'),
    receive: (bot) => {

        bot.say("Search in progress...")
        .then(() => {
            // Call API Here
            return bot.say("Search Finished.")
            .then(() => 'TEST_ROUT') // Go to TEST_ROUT
        });

    }

},

TEST_ROUT: {
        prompt: (bot) => bot.say("Test rout called"),
        receive: () => 'showUserMenu'
},


finish: {
    receive: (bot, message) => {
        return bot.getProp('name')
            .then((name) => bot.say(`Sorry ${name}, my creator didn't ` +
                    'teach me how to do anything else!'))
            .then(() => 'showUserMenu');
    }
}

});

我所做的是在我的块中调用一个yts ApiADD_MOVIE,在成功响应后我想去块,TEST_ROUT但我在 Heroku 日志中得到了这个。

2016-08-24T09:48:15.304174+00:00 app[web.1]: Undefined state
undefined for user 1c91f4b02bf493fc6e8c606a,reverting to default state 'start'

我被重定向到“开始”块。这是我与机器人对话的 Facebook Messenger 截图

在此处输入图像描述

我在这里做错了什么?请记住,我必须调用 API,成功后我必须重定向TEST_ROUT块。

更新 1

应用解决方案后,我得到了奇怪的输出。见图片:

在此处输入图像描述

  • 在 api 结果之前调用 TEST_ROUT

这是我现在更新的 ADD_MOVIE 块

TEST_ROUT: {
        prompt: (bot) => bot.say("Test rout called"),
        receive: () => 'showUserMenu'
},

ADD_MOVIE : {

    prompt: (bot) => bot.say('Enter movie name or keywords you want to search please.'),
    receive: (bot, message) => {
        const movie_name_searched = message.text;
        return bot.say('Search in progress...')
            .then(() => {
               return YtsHelper.getMoviesList(movie_name_searched,function(movies_array){

                    if(movies_array[0] != "ERROR_FOUND" && movies_array[0] != "NO_MOVIE_FOUND"){

                        var movies_postbacks = "";
                        for (var i = 0; i < movies_array.length ; i++){
                            movies_postbacks = movies_postbacks + " %["+movies_array[i]+"](postback:ADD_TO_FIREBASE)";
                        }

                        movies_postbacks = "Click any movie to add into firebase." + movies_postbacks;
                        bot.say(movies_postbacks);

                    }else{

                        if(movies_array[0] == "ERROR_FOUND"){
                            bot.say("ERROR Occured");
                        }else if(movies_array[0] == "NO_MOVIE_FOUND"){
                            bot.say("No movie found");   
                        }
                    }
                });
            })
            .then(() => "TEST_ROUT");
    }
},
4

1 回答 1

2

您在of中缺少一条return语句:receiveADD_MOVIE

ADD_MOVIE : {
    prompt: (bot) => bot.say('Enter movie name or keywords you want to search please.'),
    receive: (bot) => {
        return bot.say("Search in progress...")
        .then(() => {
              return callMyApi();
        })
        .then(() => bot.say("Search Finished."))
        .then(() => 'TEST_ROUT');
    }
}

更新 1

您的消息出现乱序的原因是您的调用getMoviesList使用回调,而不是promises

如果您将调用包装在 Promise 中,然后将其返回,它应该可以工作:

return new Promise((resolve) => {
    YtsHelper.getMoviesList(movie_name_searched, function(movies_array) {
        if (movies_array[0] != 'ERROR_FOUND' && movies_array[0] != 'NO_MOVIE_FOUND') {
            var movies_postbacks = '';
            for (var i = 0; i < movies_array.length; i++) {
                movies_postbacks = movies_postbacks + ' %[' + movies_array[i] + '](postback:ADD_TO_FIREBASE)';
            }

            movies_postbacks = 'Click any movie to add into firebase.' + movies_postbacks;
            resolve(bot.say(movies_postbacks));
        } else {
            if (movies_array[0] == 'ERROR_FOUND') {
                resolve(bot.say('ERROR Occured'));
            } else if (movies_array[0] == 'NO_MOVIE_FOUND') {
                resolve(bot.say('No movie found'));
            }
        }
    });
});
于 2016-08-24T12:03:24.497 回答