Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <subdomain> -e <email> -p <password> -t <slack_api_token>
```

You can find a `<slack_api_token>` 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:
Expand Down
39 changes: 34 additions & 5 deletions bin/emojipacks
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -17,19 +19,36 @@ 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);

/**
* Start process.
*/

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();
});

Expand All @@ -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`);
}
18 changes: 15 additions & 3 deletions lib/prompt.js
Original file line number Diff line number Diff line change
Expand Up @@ -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!');
Expand All @@ -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});
}

/**
Expand Down Expand Up @@ -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"
}
68 changes: 68 additions & 0 deletions lib/slack_public_api.js
Original file line number Diff line number Diff line change
@@ -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;