diff --git a/.gitignore b/.gitignore index c72d8c81..014e44e5 100644 --- a/.gitignore +++ b/.gitignore @@ -25,6 +25,7 @@ build/Release # Dependency directory # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git node_modules +package-lock.json ./test resized/ diff --git a/README.md b/README.md index 0425eaed..53c64307 100644 --- a/README.md +++ b/README.md @@ -166,6 +166,29 @@ emojis: Want to contribute? [Suggest an emoji pack](https://20p.typeform.com/to/xOFDyq)! + + +# Bonus + + ## Membermojis + +Have you ever wanted to react with emojis of your friends and coworkers? +Well here is ***Membermojis*** ! + +The membermojis option will create an emoji for all the users of your slack ! The emojis will simply be their profile picture. + +### Usage: + +```bash +emojipacks --membermojis -s -e -p -t +``` + +You can find a `` for your workspace here: https://api.slack.com/custom-integrations/legacy-tokens + +And VoilĂ  ! + +--- + ## Troubleshooting This script will essentially log into your Slack and then submit a `POST` request on the emoji upload form page. If you are seeing errors, make sure that: diff --git a/bin/emojipacks b/bin/emojipacks index 84334185..29b14119 100755 --- a/bin/emojipacks +++ b/bin/emojipacks @@ -3,9 +3,11 @@ var program = require('commander'); var Prompt = require('../lib/prompt'); var Slack = require('../lib/slack'); +var SlackPublicAPI = require('../lib/slack_public_api'); var Pack = require('../lib/pack'); var co = require('co'); + /** * Usage. */ @@ -17,6 +19,8 @@ program .option('-e, --email [value]', 'Admin email address') .option('-p, --password [value]', 'Password for admin email') .option('-y, --pack [value]', 'YAML emoji pack') + .option('--membermojis', 'Will create emoji from your team users avatars. This option will ask for a token if not specified with -t') + .option('-t, --token [value]', 'Slack Public API token') .parse(process.argv); /** @@ -24,12 +28,27 @@ program */ co(function *() { - var user = yield Prompt.start(program.subdomain, program.email, program.password, program.pack); - var pack = yield Pack.get(user.pack); - pack = clean(pack); - user.emojis = pack.emojis; - var slack = new Slack(user, program.debug); + const userArgs = yield Prompt.start(program.subdomain, program.email, program.password, program.pack, program.membermojis, program.token); + const token = userArgs.token; + var user = userArgs.load; + var slack; + + if (program.membermojis !== true) { + var pack = yield Pack.get(user.pack); + + pack = clean(pack); + user.emojis = pack.emojis; + slack = new Slack(user, program.debug); + } else { + const slackPublicAPI = new SlackPublicAPI(token); + const emojipack = yield slackPublicAPI.getUsernamesWithAvatar(); + + memberEmojisDisclaimer() + user.emojis = emojipack + slack = new Slack(user, program.debug); + } yield slack.import(); + process.exit(); }); @@ -47,3 +66,13 @@ function clean(object) { } return object; } + +/** + * Standardize the emoji file. + */ + +function memberEmojisDisclaimer() { + console.log(`\n\nThis script doesn\'t override any existing emojis\n\ +If you want to update your membermojis you have to remove manually the old ones and re-run the script\n\ +Link: ${program.subdomain}.slack.com/customize/emoji \n\n`); +} \ No newline at end of file diff --git a/lib/prompt.js b/lib/prompt.js index 35f6d6af..b57eb092 100644 --- a/lib/prompt.js +++ b/lib/prompt.js @@ -15,7 +15,7 @@ var chalk = require('chalk'); * Start. */ -exports.start = function* (subdomain, email, password, pack) { +exports.start = function* (subdomain, email, password, pack, memberAvatar, token) { var load, valid; if (!subdomain) { subdomain = yield ask('Slack subdomain: ', isSubdomain, 'Uh oh! The subdomain should be at least one letter!'); @@ -26,16 +26,20 @@ exports.start = function* (subdomain, email, password, pack) { if (!password) { password = yield ask('Password: ', isPassword, 'A password (as defined by this script) needs to have at least one character (not including you).'); } - if (!pack) { + if (!memberAvatar && !pack) { pack = yield ask('Path or URL of Emoji yaml file: ', isPath, 'Does the path to the yaml file look right? :)'); } + if (memberAvatar && !token) { + token = yield ask('Slack API token:', isToken, 'Token seemed invalid. Try again? :'); + } + load = { url: url(subdomain), email: email, password: password, pack: pack }; - return load; + return ({load:load, token: token}); } /** @@ -77,3 +81,11 @@ function err(message) { function url(subdomain) { return 'https://' + subdomain + '.slack.com'; } + +/** + * is token + */ + +function isToken(path) { + return path.length > 20; // TODO check if user token "xox" +} diff --git a/lib/slack_public_api.js b/lib/slack_public_api.js new file mode 100644 index 00000000..fcf7200d --- /dev/null +++ b/lib/slack_public_api.js @@ -0,0 +1,68 @@ +var thunkify = require('thunkify-wrap'); +var request = thunkify(require('request')); + +function SlackPublicAPI(token) { + this.token = null + + this.setToken(token); +} + +SlackPublicAPI.prototype.setToken = function(token) { + this.token = token; +}; + +SlackPublicAPI.prototype.getUsers = function *() { + var opts = this.opts; + var load = { + url: "https://slack.com/api/users.list?token=" + this.token + "&pretty=1", + jar: { _jar: { store: { idx: {} } } }, + method: 'GET' + }; + const members = JSON.parse((yield request(load))[0].body).members; + + return members; +}; + +SlackPublicAPI.prototype.getUsernamesWithAvatar = function*() { + const members = yield this.getUsers(); + if (!members) { return; }; + var results = []; + + for (var i = 0; i < members.length; i++) { + var result; + const member = members[i]; + const image = yield this.extractImage(member); + const username = this.extractUsername(member); + + if (username !== null && image !== null){ + result = { + name: username, + src: image + }; + } + results.push(result); + } + + return results; +}; + +SlackPublicAPI.prototype.extractUsername = function(obj) { + return obj.name.replace(/[^\w ]/, ''); +} + +SlackPublicAPI.prototype.extractImage = function*(obj) { + const imageURL = obj.profile.image_24 + const load = { + method: 'GET', + url: imageURL + } + const res = yield request(load); + if (res[0].headers.link === undefined) { return imageURL }; + const indexLinkBegin = res[0].headers.link.indexOf('<'); + const indexLinkEnd = res[0].headers.link.indexOf('>'); + const imageLocation = res[0].headers.link.slice(indexLinkBegin + 1, indexLinkEnd); + + return imageLocation; +} + +module.exports = SlackPublicAPI; \ No newline at end of file