Skip to content

Latest commit

 

History

History
41 lines (34 loc) · 1.01 KB

slack-invite.md

File metadata and controls

41 lines (34 loc) · 1.01 KB

Slack invite

https://github.com/AngularFI/angular-fi/blob/old-heroku-version-placeholder/src/models/slack.model.ts

import * as request from 'request';
import * as EmailValidator from 'email-validator';

declare var process;

export class Slack {
  private url: string = 'https://slack.com/api/users.admin.invite';
  private token: string = process.env.SLACK_API_TOKEN;

  constructor() {}

  invite(email: string): Promise<boolean> {
    return new Promise( (resolve, reject) => {
      if (EmailValidator.validate(email)) {
        const url = this.url + '?token=' + this.token + '&email=' + encodeURIComponent(email);
        request.get(url, (error: any, response: any, body: any) => {
          let data;
          try {
            data = JSON.parse(body);
          } catch (err) {
            data = {};
          }

          if (!error && body && data && data.ok) {
            resolve(true);
          } else {
            reject();
          }
        });
      } else {
        reject();
      }
    });
  }
}