Skip to content

Commit

Permalink
Merge pull request publiclab#48 from ryzokuken/minor-2
Browse files Browse the repository at this point in the history
Switch to a promise based workflow
  • Loading branch information
ryzokuken authored Jun 28, 2017
2 parents 69cf2a8 + 12982d7 commit 702e738
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 45 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "plotsbot",
"version": "1.0.1",
"version": "1.0.2",
"description": "A bot for Public Lab",
"main": "src/bot.js",
"repository": "[email protected]:publiclab/plotsbot.git",
Expand Down
16 changes: 12 additions & 4 deletions spec/help-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,26 @@ describe('Help Behavior', () => {
const invalidHelp = helpMessage(botNick, 'invalid');

it('should print general help', () => {
expect(behaviors.getResponse(botNick, 'help')).toBe(chatbotHelp);
behaviors.getResponse(botNick, 'help').then(response => {
expect(response).toBe(chatbotHelp);
});
});

it('should print specific help for existing modules', () => {
expect(behaviors.getResponse(botNick, 'help chatbot')).toBe(chatbotHelp);
behaviors.getResponse(botNick, 'help chatbot').then(response => {
expect(response).toBe(chatbotHelp);
});
});

it('should print specific help for nonexisting modules', () => {
expect(behaviors.getResponse(botNick, 'help invalid')).toBe(invalidHelp);
behaviors.getResponse(botNick, 'help invalid').then(response => {
expect(response).toBe(invalidHelp);
});
});

it('should print specific help for existing and nonexisting modules combined', () => {
expect(behaviors.getResponse(botNick, 'help chatbot invalid')).toBe(chatbotHelp + '\n\n' + invalidHelp);
behaviors.getResponse(botNick, 'help chatbot invalid').then(response => {
expect(response).toBe(chatbotHelp + '\n\n' + invalidHelp);
});
});
});
87 changes: 47 additions & 40 deletions src/behaviors/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,25 @@ function parseMessage (message) {
}

function messageResponse(botNick, parsed, behaviors) {
// This function takes the parsed version of the message and the array of
// behaviors with `trigger` equal to "message" (i.e. the behaviors supposed to
// be triggered on message), executes the action of a behavior if it was
// mentioned by `keyword` and returns the result
return Promise.resolve().then(() => {
// This function takes the parsed version of the message and the array of
// behaviors with `trigger` equal to "message" (i.e. the behaviors supposed to
// be triggered on message), executes the action of a behavior if it was
// mentioned by `keyword` and returns the result

// This snippet looks if the parsed message contains the keyword of any of the
// behavior inside the behaviors array.
const behavior = behaviors.find(behavior =>
utils.contains(parsed, behavior.keyword)
);
// This snippet looks if the parsed message contains the keyword of any of the
// behavior inside the behaviors array.
const behavior = behaviors.find(behavior =>
utils.contains(parsed, behavior.keyword)
);

// If there was a match, remove the behavior's keyword from the parsed message
// call the behavior's action with the remaining message and the bot's nick
if (behavior) {
utils.remove(parsed, behavior.keyword);
return behavior.action(botNick, parsed);
}
// If there was a match, remove the behavior's keyword from the parsed message
// call the behavior's action with the remaining message and the bot's nick
if (behavior) {
utils.remove(parsed, behavior.keyword);
return behavior.action(botNick, parsed);
}
});
}

class Behaviors {
Expand All @@ -45,36 +47,41 @@ class Behaviors {

addMessageHandler() {
this.client.addMessageHandler((from, to, message) => {
const response = this.getResponse(to, message);
if(response) {
if(to === this.botNick) {
// Message was recieved in a DM
this.client.sendMessage(from, response);
} else {
// Message was recieved in a normal channel
this.client.sendMessage(to, response);
this.getResponse(to, message).then((response) => {
if(response) {
if(to === this.botNick) {
// Message was recieved in a DM
this.client.sendMessage(from, response);
} else {
// Message was recieved in a normal channel
this.client.sendMessage(to, response);
}
}
}
}).catch(err => {
console.error(err);
});
});
}

getResponse(to, message) {
let parsed = parseMessage(message);
if(to === this.botNick) {
// If the message was sent directly to the bot (eg: in a DM)
return messageResponse(this.botNick, parsed, this.messageBehaviors);
} else if (utils.contains(parsed, this.botNick)) {
// If bot was mentioned
utils.remove(parsed, this.botNick);
return messageResponse(this.botNick, parsed, this.messageBehaviors);
} else if (utils.contains(parsed, '@' + this.botNick)) {
// If bot was mentioned, Gitter style
utils.remove(parsed, '@' + this.botNick);
return messageResponse(this.botNick, parsed, this.messageBehaviors);
} else {
// If the message was not meant for the bot
return undefined;
}
return Promise.resolve().then(() => {
let parsed = parseMessage(message);
if(to === this.botNick) {
// If the message was sent directly to the bot (eg: in a DM)
return messageResponse(this.botNick, parsed, this.messageBehaviors);
} else if (utils.contains(parsed, this.botNick)) {
// If bot was mentioned
utils.remove(parsed, this.botNick);
return messageResponse(this.botNick, parsed, this.messageBehaviors);
} else if (utils.contains(parsed, '@' + this.botNick)) {
// If bot was mentioned, Gitter style
utils.remove(parsed, '@' + this.botNick);
return messageResponse(this.botNick, parsed, this.messageBehaviors);
} else {
// If the message was not meant for the bot
return undefined;
}
});
}

bootstrap() {
Expand Down

0 comments on commit 702e738

Please sign in to comment.