Skip to content

Instantly share code, notes, and snippets.

@olegpolyakov
Last active February 3, 2020 19:58
Show Gist options
  • Save olegpolyakov/22cfecdc01838d805c9d3fae7a56a152 to your computer and use it in GitHub Desktop.
Save olegpolyakov/22cfecdc01838d805c9d3fae7a56a152 to your computer and use it in GitHub Desktop.
Slack Service
'use strict';
const request = require('request');
module.exports = {
inviteToTeam({ user, channels, token }) {
const body = {
email: user.email,
first_name: user.firstname,
last_name: user.lastname,
channels,
token,
resend: true
};
return new Promise((resolve, reject) => {
request.post({
url: 'https://slack.com/api/users.admin.invite',
form: body
}, (error, response, rawBody) => {
let body;
if (error) return reject(error);
if (response.statusCode !== 200) return reject(new Error(`Invalid response ${response.statusCode}.`));
try {
body = JSON.parse(rawBody);
} catch (e) {
return reject(e);
}
if (!body.ok) {
if (body.error === 'missing_scope' && body.needed === 'admin') {
reject(new Error('Missing admin scope: The token you provided is for an account that is not an admin. You must provide a token from an admin account in order to invite users through the Slack API.'));
} else if (body.error === 'already_invited') {
resolve('Пользователь уже приглашен.');
} else if (body.error === 'already_in_team') {
resolve('Пользователь уже в команде.');
} else {
reject(new Error(body.error || 'Не удалось отправить приглашение в Slack.'));
}
} else {
resolve('Пользователь приглашен.');
}
});
});
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment