I am working on smooch-bot-example, I have set it up and uploaded to Heroku Git. I am getting bot replying successfully.
I am stuck in the scenario below. Here is my script.js
file (see my ADD_MOVIE
block)
'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');
}
}
});
What I have done is calling an yts Api in my ADD_MOVIE
block, upon success response I want to go to the block TEST_ROUT
but I am getting this in Heroku log.
2016-08-24T09:48:15.304174+00:00 app[web.1]: Undefined state
undefined for user 1c91f4b02bf493fc6e8c606a,reverting to default state 'start'
I am redirected to the 'start' block. Here is my Facebook Messenger screenshot of conversation with bot
What I am doing wrong here? Please keep in mind I have to call the API and upon success I have to redirect the TEST_ROUT
block.
Update 1
After applying solution, I am getting weird output. See image:
here is my updated ADD_MOVIE block now
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");
}
},
You are missing a return
statement in the receive
of ADD_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');
}
}
Update 1
The reason your messages are appearing out of order is because your call to getMoviesList
uses callbacks, not promises.
If you wrap your call in a Promise, and return that instead, it should work:
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'));
}
}
});
});