|
|
@@ -0,0 +1,41 @@
|
|
|
+const { WebClient: SlackWebClient } = require('@slack/client');
|
|
|
+
|
|
|
+const EMAIL_REGEX = /^(.*)@(.*)$/;
|
|
|
+
|
|
|
+function getAllSlackUsersWithNames(config) {
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
+ const webClient = new SlackWebClient(config.slack.token);
|
|
|
+
|
|
|
+ webClient.users.list({}, (err, info) => {
|
|
|
+ if (err) {
|
|
|
+ return reject(err);
|
|
|
+ }
|
|
|
+
|
|
|
+ const users = info.members
|
|
|
+ .filter(user => user &&
|
|
|
+ !user.deleted &&
|
|
|
+ user.name &&
|
|
|
+ user.profile &&
|
|
|
+ user.profile.email &&
|
|
|
+ user.profile.email.match(EMAIL_REGEX)
|
|
|
+ )
|
|
|
+ .map(user => {
|
|
|
+ const [, shortName] = user.profile.email.match(EMAIL_REGEX);
|
|
|
+
|
|
|
+ return {
|
|
|
+ id: user.id,
|
|
|
+ name: user.name,
|
|
|
+ shortName,
|
|
|
+ email: user.profile.email.toLowerCase()
|
|
|
+ };
|
|
|
+ });
|
|
|
+
|
|
|
+ return resolve(users);
|
|
|
+ });
|
|
|
+ });
|
|
|
+}
|
|
|
+
|
|
|
+module.exports = {
|
|
|
+ getAllSlackUsersWithNames
|
|
|
+};
|
|
|
+
|