为了好玩,我正在制作hubot-slack。
我想要实现的是让hubot立刻得到几个问题。例如,如下所示:
user : @hubot: add job
hubot : what is your job ?
user : jobless
hubot : where is your workplace ?
user : home
hubot : your job has registered.
如您所见,用户只能使用命令add job
触发一次事件。
以下是我的javascript源代码来处理它。
致电部分:
robot.respond(/register me/igm, function(msg){
//var promptConsole = new Prompt(msg);
var questions = [
{'question' : 'What is your name ?', 'dataname' : 'name'}
, {'question' : 'What is your job ?', 'dataname' : 'job'}
];
var promptConsole = new Prompt(msg, questions);
promptConsole.init().startPrompt(robot);
});
和提示对象:
var Prompt = function (msg, questions) {
console.log(msg);
this.msg = msg;
this.user = msg.message.user;
this.room = msg.message.room;
this.results;
this.questions = questions;
//console.log(user +' is in room [ ' + room + ' ] ');
this.init = function () {
this.results = [];
for(var i = 0; i < questions.length; i ++) {
var singleQuestion = questions[i];
var result = {'key': '', 'question' : '', 'value' : ''};
result.key = singleQuestion.dataname;
result.question = singleQuestion.question;
this.results.push(result);
}
return this;
}
this.startPrompt = function () {
for(var i = 0; i < this.results.length; i ++) {
console.log(this.results[i]);
this.msg.send(this.results[i].question);
}
}
}
但是,使用robot.hear
或robot.respond
无法实现此功能,因为用户输入只发生一次。
还有其他方法可以实现吗?如果没有,我应该将此功能分为许多部分,例如add job
,add workplace
等等。