Skip to content

Instantly share code, notes, and snippets.

@selfcontained
Last active January 23, 2017 22:14
Show Gist options
  • Save selfcontained/ff8a15285f081ab765c8 to your computer and use it in GitHub Desktop.
Save selfcontained/ff8a15285f081ab765c8 to your computer and use it in GitHub Desktop.

Instructions

  • Add google-hangout.coffee to your hubots scripts folder
  • It's expecting the phantom script to live in a lib/phantom-create-hangout.js file (relative to your hubot project root)
  • Make sure to set the following environment variables somewhere before you launch hubot (I just have a bash file I source prior to running the hubot binary).
export HUBOT_HANGOUT_USERNAME='<your-hubot-email@gmail.com>'
export HUBOT_HANGOUT_PASSWORD='<your-password>'
export HUBOT_HANGOUT_VERIFY_EMAIL='<verification-email-for-gmail-account>'
# Description
# Create a Google+ Hangout and print out the url in chat
#
# Dependencies
# phantomjs
#
# Configuration
# HUBOT_HANGOUT_USERNAME
# HUBOT_HANGOUT_PASSWORD
# HUBOT_HANGOUT_VERIFY_EMAIL
#
# Commands:
# hubot +hangout - sends hubot on a mission to create a G+ Hangout
#
# Notes:
# You'll need to have phantomjs installed on your hubot server and on your PATH
#
# Author:
# bmharris
exec = require('child_process').exec
format = require('util').format
path = require('path')
user = process.env.HUBOT_HANGOUT_USERNAME
pass = process.env.HUBOT_HANGOUT_PASSWORD
verify = process.env.HUBOT_HANGOUT_VERIFY_EMAIL
phantomScriptDir = path.join __dirname, '../lib'
cmd = format 'phantomjs %s/%s %s %s %s', phantomScriptDir, 'phantom-create-hangout.js', user, pass, verify
module.exports = (robot) ->
robot.respond /\+hangout/i, (msg) ->
msg.reply 'one hangout, coming right up...'
exec cmd, (err, stdout, stdin) ->
if err
msg.reply "hrmmmz, y u no work?"
msg.reply err
msg.reply "fresh hangout: " + stdout
var page = require('webpage').create(),
USERNAME = phantom.args[0],
PASSWORD = phantom.args[1],
VERIFICATION_EMAIL = phantom.args[2],
HANGOUT_REGEX = /\/hangouts\/_\/[a-z0-9]+$/;
page.settings.javascriptEnabled = true;
page.settings.loadImages = true;
page.open('https://plus.google.com/hangouts/_/', function(status) {
// Give login page time to load
setTimeout(function() {
if(isLoginPage()) login();
var tries = 0;
(function pollForHangout() {
if(isVerificationPage()) verify();
isHangoutPage() || tries++ > 15 ? finish() : setTimeout(pollForHangout, 1000);
})();
}, 3000);
});
function finish() {
var url = getHangoutUrl();
console.log(HANGOUT_REGEX.test(url) ? url : 'no hangout for you, sry');
page.release();
phantom.exit();
}
function isLoginPage() {
return page.evaluate(function() {
return !!document.getElementById('Email');
});
}
function isVerificationPage() {
return page.evaluate(function() {
return !!document.getElementById('emailAnswer');
});
}
function login() {
page.evaluate(function(user, pass) {
document.getElementById('Email').value = user;
document.getElementById('Passwd').value = pass;
document.getElementById('signIn').click();
}, USERNAME, PASSWORD);
}
function verify() {
page.evaluate(function(email) {
document.getElementById('emailAnswer').value = email;
document.getElementById('submitChallenge').click();
}, VERIFICATION_EMAIL);
}
function getHangoutUrl() {
return page.evaluate(function() {
return document.location.href;
});
}
function isHangoutPage() {
return page.evaluate(function(regex) {
return regex.test(document.location.href);
}, HANGOUT_REGEX);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment