我正在处理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');
}
}
});
我所做的是在我的ADD_MOVIE
块中调用yts Api,成功响应后我想转到块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'
我被重定向到' start'块。这是我与Facebook的对话的Facebook Messenger截图
我在这里做错了什么?请记住,我必须调用API,一旦成功,我必须重定向TEST_ROUT
块。
更新1
在应用解决方案后,我得到了奇怪的输出。见图:
这是我现在更新的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");
}
},
答案 0 :(得分:2)
您在return
的{{1}}内遗漏了receive
声明:
ADD_MOVIE
更新1
您的邮件显示不正常的原因是因为您对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');
}
}
的调用使用的是回调,而不是promises。
如果你把你的电话包裹在一个Promise中,然后返回它,它应该有效:
getMoviesList