var casper = require('casper').create({
  viewportSize: {width: 950, height: 950},
  pageSettings: {
    userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'
  }
});

var subdomain = casper.cli.get("subdomain"),
  username = casper.cli.get("username"),
  password = casper.cli.get("password"),
  emails = casper.cli.get("emails"),
  invitePage = 'https://' + subdomain + '.slack.com/admin/invites/full';

casper.start(invitePage, function() {
  this.fill('form[action="/"]', {
    'email': username,
    'password': password
  }, true);
});

casper.waitForUrl(invitePage, function() {
  this.click('a[onclick="TS.web.admin_invites.switchView(\'bulk\');"]');
});

casper.waitFor(function check() {
  return this.evaluate(function() {
    return document.querySelector('#bulk_invites').getAttribute('class') === '';
  });
  }, function then() {
    this.sendKeys('textarea[name=emails]', emails);
    this.evaluate(function(emails) {
      var method = 'users.admin.parseEmails',
          unixtime = Math.round(new Date().getTime() / 1000),
          url = TS.boot_data.api_url + method + "?t=" + unixtime + TS.appendQSArgsToUrl(),
          args = {
            emails: emails,
            token: TS.boot_data.api_token
          },
          handler = TS.web.admin_invites.onEmailsParsed;

      TS.api.ajax_call(url, method, args, function(data) {
        if (!data) {
          data = {};
        }
        ok = data.ok ? true : false;
        if (handler) {
          handler(ok, data, args)
        }
      });
    }, { emails: emails });
});

casper.waitForText("We've done our best to guess", function() {
  this.click('.btn.btn-primary.small_bottom_margin.api_send_invites.ladda-button.full_width');
});

casper.waitForText("Invites sent!", function() {
  this.echo('Invites sent.');
});

casper.run();

@@ -894,18 +894,16 @@
 ;%0A  %7D);%0A
-  
 %7D, funct
@@ -909,72 +909,13 @@
 tion
- then
 () %7B%0A
-    this.sendKeys('textarea%5Bname=emails%5D', emails);%0A  
   th
@@ -941,26 +941,24 @@
 n(emails) %7B%0A
-  
     var meth
@@ -997,18 +997,16 @@
         
-  
 unixtime
@@ -1049,18 +1049,16 @@
  1000),%0A
-  
         
@@ -1115,37 +1115,10 @@
 time
- + TS.appendQSArgsToUrl(),%0A  
+,%0A
     
@@ -1130,18 +1130,16 @@
 rgs = %7B%0A
-  
         
@@ -1154,24 +1154,52 @@
 : emails,%0A  
+        set_active: 'true',%0A
           to
@@ -1238,275 +1238,780 @@
     
-  %7D,%0A          handler = TS.web
+%7D;%0A    TS.api.ajax_call(url, method, args, function(data) %7B%0A      data.emails.forEach(function(email) %7B%0A        var method = 'users
 .admin
-_
+.
 invite
-s.onEmailsParsed;%0A%0A      TS.api.ajax_call(url,
+',%0A            unixtime = Math.round(new Date().getTime() / 1000),%0A            url = TS.boot_data.api_url +
  method
-, args, function(data) %7B%0A        if (!data) %7B%0A          data = %7B%7D;%0A        %7D%0A        ok = data.ok ? true : false;%0A        if (handler) %7B%0A          handler(ok, data, args)
+ + %22?t=%22 + unixtime,%0A            args = %7B%0A              email: email.email,%0A              first_name: email.first_name %7C%7C '',%0A              last_name: email.last_name %7C%7C '',%0A              set_active: 'true',%0A              token: TS.boot_data.api_token%0A            %7D;%0A        TS.api.ajax_call(url, method, args, function(data) %7B%0A          if (data.ok %7C%7C data.error == 'sent_recently' %7C%7C data.error == 'already_invited') %7B%0A            document.getElementById('invite_sending_success').style.display = '';%0A          %7D
 %0A   
@@ -2012,24 +2012,26 @@
  %7D%0A        %7D
+);
 %0A      %7D);%0A 
@@ -2029,18 +2029,24 @@
     %7D);%0A
-
   
+  %7D);%0A
   %7D, %7B e

Call users.admin.invite