Skip to content
This repository has been archived by the owner on Aug 5, 2021. It is now read-only.

Commit

Permalink
simpler text message handling
Browse files Browse the repository at this point in the history
added a rudimentary plugin wrapper allowing developers to
dispatch a matched text message to its corresponding handler.
this mechanism greatly reduces the boilerplate plate code,
without interfering with the current none-text-message
handlers. updated plugins are: figlet, ping, sentiment,
spellcheck, tellmeabout and wikipedia
  • Loading branch information
omaraboumrad committed May 16, 2017
1 parent 91ddaa3 commit 26bed04
Show file tree
Hide file tree
Showing 9 changed files with 180 additions and 183 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"func-names": "off",
"prefer-arrow-callback": "off",
"import/no-dynamic-require": "off",
"no-unused-vars": "off"
"no-unused-vars": "off",
"no-param-reassign": "off"
}
}
33 changes: 11 additions & 22 deletions plugins/do_figlet.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const RTM_EVENTS = require('@slack/client').RTM_EVENTS;

const figlet = require('figlet');

const pre = require('../utils.js').pre;
const Plugin = require('../utils.js').Plugin;


const META = {
name: 'figlet',
Expand All @@ -12,30 +12,19 @@ const META = {
],
};

function handleFiglet(bot, rtm, message) {
return new Promise((resolve, reject) => {
const pattern = /<@([^>]+)>:? figlet (.*)/;
const [, target, text] = message.text.match(pattern) || [];

if (target === bot.self.id && text) {
figlet(text, (err, data) => {
if (err) {
reject(err);
} else {
rtm.sendMessage(pre(data), message.channel);
resolve(data);
}
});

function handleFiglet(options, message, who, text) {
figlet(text, (err, data) => {
if (!err) {
message.reply(pre(data));
}
});
}

function register(bot, rtm) {
rtm.on(RTM_EVENTS.MESSAGE, (message) => {
if (message.text) {
handleFiglet(bot, rtm, message);
}
});

function register(bot, rtm, web, config) {
const plugin = new Plugin({ bot, rtm, web, config });
plugin.route(/<@([^>]+)>:? figlet (.*)/, handleFiglet, { self: true });
}


Expand Down
24 changes: 10 additions & 14 deletions plugins/ping.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const RTM_EVENTS = require('@slack/client').RTM_EVENTS;
const Plugin = require('../utils.js').Plugin;

const META = {
name: 'ping',
Expand All @@ -8,25 +8,21 @@ const META = {
],
};

function handlePing(bot, rtm, message) {
if (message.text) {
const pattern = /<@([^>]+)>:? ping/;
const [, target] = message.text.match(pattern) || [];
function ping(options, message, who) {
message.reply('pong');
}

if (target === bot.self.id) {
rtm.sendMessage('pong', message.channel);
}
}
function pingThem(options, message, who, target) {
message.reply(`${target} PING PING PINGGGGGG!`);
}

function register(bot, rtm) {
rtm.on(RTM_EVENTS.MESSAGE, (message) => {
handlePing(bot, rtm, message);
});
function register(bot, rtm, web, config) {
const plugin = new Plugin({ bot, rtm, web, config });
plugin.route(/<@([^>]+)>:? ping$/, ping, { self: true });
plugin.route(/<@([^>]+)>:? ping (.+)/, pingThem, { self: true });
}

module.exports = {
META,
register,
handlePing,
};
50 changes: 20 additions & 30 deletions plugins/sentiment.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
const request = require('request');

const RTM_EVENTS = require('@slack/client').RTM_EVENTS;

const winston = require('winston');

const Plugin = require('../utils.js').Plugin;

const META = {
name: 'sentiment',
short: 'provides a sentiment analysis on the last 10 messages of a user',
examples: [
'@bosta how has jordan been recently?',
'@bosta analyse jordan',
],
};

Expand All @@ -30,13 +29,13 @@ function pickTarget(bot, channel) {
}


function loadRecentMessages(bot, web, config, channel, user) {
function loadRecentMessages(options, channel, user) {
return new Promise((resolve, reject) => {
const target = pickTarget(bot, channel);
let source = web.channels;
const target = pickTarget(options.bot, channel);
let source = options.web.channels;

if (target.is_group) {
source = web.groups;
source = options.web.groups;
}

source.history(channel, { count: 1000 }, (error, response) => {
Expand All @@ -45,8 +44,8 @@ function loadRecentMessages(bot, web, config, channel, user) {
} else {
let messages = response.messages.filter(m => m.user === user.id);

if (messages.length > config.plugins.sentiment.recent) {
messages = messages.slice(1, config.plugins.sentiment.recent);
if (messages.length > options.config.plugins.sentiment.recent) {
messages = messages.slice(1, options.config.plugins.sentiment.recent);
}

if (messages.length === 0) {
Expand Down Expand Up @@ -83,27 +82,18 @@ function analyseSentiment(secret, messages) {
}


function analyse(options, message, who, target) {
findUser(options.bot, target)
.then(user => loadRecentMessages(options, message.channel, user))
.then(messages => analyseSentiment(options.secret, messages))
.then(sentiment => message.reply(`${target} has recently been ${sentiment.output.result}`))
.catch(error => winston.error(`${META.name} - Error: ${error}`));
}


function register(bot, rtm, web, config, secret) {
rtm.on(RTM_EVENTS.MESSAGE, (message) => {
if (message.text) {
const pattern = /<@([^>]+)>:? how has ([^ ]+) been recently\?/;
const [, target, who] = message.text.match(pattern) || [];

if (target === bot.self.id) {
findUser(bot, who)
.then(user => loadRecentMessages(bot, web, config, message.channel, user))
.then(messages => analyseSentiment(secret, messages))
.then((sentiment) => {
rtm.sendMessage(
`${who} has recently been ${sentiment.output.result}`,
message.channel);
})
.catch((error) => {
winston.error(`${META.name} - Error: ${error}`);
});
}
}
});
const plugin = new Plugin({ bot, rtm, web, config, secret });
plugin.route(/<@([^>]+)>:? analyse (.+)/, analyse, { self: true });
}


Expand Down
48 changes: 23 additions & 25 deletions plugins/snippets.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const RTM_EVENTS = require('@slack/client').RTM_EVENTS;
const winston = require('winston');

const pre = require('../utils.js').pre;
const Plugin = require('../utils.js').Plugin;

const META = {
name: 'snippets',
Expand Down Expand Up @@ -133,37 +134,34 @@ function runSnippet(web, rtm, config, secret, file) {
.catch(() => {}); // bot already reacted supposedly
}

function register(bot, rtm, web, config, secret) {
rtm.on(RTM_EVENTS.MESSAGE, (message) => {
if (message.text) {
const pattern = /<@([^>]+)>:? snippets support/;
const [, target] = message.text.match(pattern) || [];
function supported(options, message) {
const languages = Object.keys(options.config.plugins.snippets.languages).join(', ');

message.reply(`I can run: ${languages}`);
}

if (target === bot.self.id) {
const languages = Object.keys(config.plugins.snippets.languages).join(', ');
rtm.sendMessage(`I can run: ${languages}`, message.channel);
}
}

if (message.text) {
const pattern = /<@([^>]+)>:? snippets config (.*)/;
const [, target, lang] = message.text.match(pattern) || [];
function langConfig(options, message, who, lang) {
try {
const { timeout, crop, memory } = loadConfig(options.config, lang);

if (target === bot.self.id) {
try {
const { timeout, crop, memory } = loadConfig(config, lang);
rtm.sendMessage(pre(`${lang}:
message.reply(pre(`${lang}:
Timeout : ${timeout} seconds
Memory : ${memory}MB
Crops at : ${crop} characters`), message.channel);
} catch (e) {
rtm.sendMessage(
pre(`${lang} is not supported`),
message.channel);
}
}
}
Crops at : ${crop} characters`));
} catch (e) {
message.reply(pre(`${lang} is not supported`));
}
}


function register(bot, rtm, web, config, secret) {
const plugin = new Plugin({ bot, rtm, web, config });
plugin.route(/<@([^>]+)>:? snippets support/, supported, { self: true });
plugin.route(/<@([^>]+)>:? snippets config (.*)/, langConfig, { self: true });


rtm.on(RTM_EVENTS.MESSAGE, (message) => {
if (message.file
&& message.file.mode === 'snippet'
&& message.subtype === 'file_share'
Expand Down
43 changes: 20 additions & 23 deletions plugins/spellcheck.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const RTM_EVENTS = require('@slack/client').RTM_EVENTS;

const checker = require('spellchecker');

const Plugin = require('../utils.js').Plugin;

const META = {
name: 'spellchecker',
short: 'spell checks any word in a sentence',
Expand All @@ -10,27 +10,24 @@ const META = {
],
};

function register(bot, rtm) {
rtm.on(RTM_EVENTS.MESSAGE, (message) => {
if (message.text) {
const pattern = /(\w+)\(sp\?\)/;
const [, word] = message.text.match(pattern) || [];
if (word) {
const wrong = checker.isMisspelled(word);
const result = checker.getCorrectionsForMisspelling(word);

if (!wrong) {
rtm.sendMessage(`${word} is spelled correctly.`, message.channel);
} else if (result.length === 0) {
rtm.sendMessage(`I don't know how to fix ${word}`, message.channel);
} else {
rtm.sendMessage(
`possible spelling for ${word}: ${result.join(', ')}`,
message.channel);
}
}
}
});

function spell(options, message, word) {
const wrong = checker.isMisspelled(word);
const result = checker.getCorrectionsForMisspelling(word);

if (!wrong) {
message.reply(`${word} is spelled correctly.`);
} else if (result.length === 0) {
message.reply(`I don't know how to fix ${word}`);
} else {
message.reply(`possible spelling for ${word}: ${result.join(', ')}`);
}
}


function register(bot, rtm, web, config) {
const plugin = new Plugin({ bot, rtm, web, config });
plugin.route(/(\w+)\(sp\?\)/, spell, {});
}


Expand Down

0 comments on commit 26bed04

Please sign in to comment.