我制作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.
如您所见,用户只会使用 command 触发一次事件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
or来实现robot.respond
,因为用户输入只发生一次。
还有其他方法可以实现吗?如果没有,我应该将此功能分成许多部分,例如add job
,add workplace
等等。