我正在关注@dstoiko from here
的回答我在ADD_MOVIE
块中调用API,并希望将一些值传递给我的回发,并使用有效负载ADD_TO_FIREBASE
这是我的块
'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, message) => {
const movie_name_searched = message.text;
return bot.setProp('movie_name_searched', movie_name_searched)
.then(() => bot.say('Search in progress...'))
.then(() => {
YtsHelper.getMoviesList(movie_name_searched,function(movies_array){
var movies_postbacks = "";
console.log("Movies SIZE " + movies_array.length);
for (var i = 0; i < movies_array.length ; i++){
movies_postbacks = movies_postbacks + " %["+movies_array[i]+"](postback:ADD_TO_FIREBASE)";
}
bot.say(movies_postbacks)
.then(() => bot.say("Click any movie to add into firebase."));
});
});
}
},
ADD_TO_FIREBASE: {
prompt: (bot) => bot.say("confirm, y/n"),
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');
}
}
});
问题
Q0。我也是nodeJS的新手,我该怎么称呼ADD_MOVIE,start,showUserMenu(在我的代码中)块?功能,方法,代码,模块等。
Q1。我在ADD_MOVIE块中调用了yts api。可以在script.js文件中调用API吗?
Q2。重要!:如何通过有效负载ADD_MOVIE将参数传递给回发,以便我可以在ADD_TO_FIREBASE块中执行一些条件代码
答案 0 :(得分:2)
Q0:是一个风格问题,在这里没有明确的答案。换句话说,这是这种讨论的错误论坛:) https://stackoverflow.com/help/how-to-ask
Q1:是的,在receive
中进行数据库查询是正常的,但是receive
函数在解析您的机器人之前还没有等待查询完成州。例如,如果您不希望机器人接受用户输入,直到返回电影列表,您可以这样做:
receive: (bot, message) => {
const movie_name_searched = message.text;
return bot.setProp('movie_name_searched', movie_name_searched)
.then(() => bot.say('Search in progress...'))
.then(() => {
return new Promise((res) => YtsHelper.getMoviesList(movie_name_searched, (movies_array) => res(movies_array)));
})
.then((movies_array) => {
var movies_postbacks = "";
for (var i = 0; i < movies_array.length ; i++){
movies_postbacks = movies_postbacks + " %["+movies_array[i]+"](postback:ADD_TO_FIREBASE)";
}
return bot.say(movies_postbacks);
})
.then(() => bot.say("Click any movie to add into firebase."))
.then(() => 'ADD_MOVIE');
}
请注意,我使用&#39; ADD_MOVIE&#39;来解析承诺链的最后一部分,它告诉您的机器人保持与之前相同的状态。
Q2:我看到两个选项。
选项1:将电影ID附加到回发有效负载,例如ADD_TO_FIREBASE.movieid1
,ADD_TO_FIREBASE.movieid2
等等。
如果你这样做了,你必须在handlePostback中定义自己的行为,从你的回发有效载荷中解析出电影ID。
您还必须自己将状态机转换为所需的ADD_TO_FIREBASE
状态。例如,从您的自定义handlePostback
方法中,您可以执行以下操作:
const stateMachine = new StateMachine({
script,
bot: createBot(req.body.appUser)
});
stateMachine.setState('ADD_TO_FIREBASE');
选项2:您正在使用的%[foo](postback:bar)
消息实际上是一种速记语法。回发消息的真正内部工作原理是可以发送到Smooch API directly的操作按钮。操作按钮还允许您指定metadata
对象。如果不使用内置的bot.say
,您可以直接在API上发布消息,并且可以将动画ID存储在动作元数据中。您将再次通过自定义handlePostback
从此元数据中检索选定的movieId,如同在选项1中所做的那样。