Skip to content

Instantly share code, notes, and snippets.

@ha1t
Created October 7, 2015 09:47
Show Gist options
  • Save ha1t/f4b4ce1580c306482c82 to your computer and use it in GitHub Desktop.
Save ha1t/f4b4ce1580c306482c82 to your computer and use it in GitHub Desktop.
chatworkbot.js
// chatworkbot.js
// PhantomJS script for automatically sending chat messages to ChatWork.
//
// @url http://superbrothers.hatenablog.com/entry/2012/06/16/120249
//
var system = require('system');
var page = require('webpage').create();
var state_index = 1;
var loadInProgress = false;
page.onConsoleMessage = function(msg) {
console.log(msg);
};
page.onLoadStarted = function() {
loadInProgress = true;
console.log("load started");
};
page.onLoadFinished = function() {
loadInProgress = false;
console.log("load finished");
};
var options = {
email: { idx: 1, value: "", optional: false },
password: { idx: 2, value: "", optional: false },
roomname: { idx: 3, value: "", optional: false },
message: { idx: 4, value: "", optional: false },
label: { idx: 5, value: "", optional: true },
};
/*
* This function wraps WebPage.evaluate, and offers the possibility to pass
* parameters into the webpage function. The PhantomJS issue is here:
*
* http://code.google.com/p/phantomjs/issues/detail?id=132
*
* This is from comment #43.
*/
function evaluate(page, func) {
var args = [].slice.call(arguments, 2);
var fn = "function() { return (" + func.toString() + ").apply(this, " + JSON.stringify(args) + ");}";
return page.evaluate(fn);
}
function $(id) {
return document.querySelector(id);
}
for (var o in options) {
options[o].value = system.args[options[o].idx];
}
var max_state_index = 5;
var ChatWork = {
room_ids: [],
login: function () {
interval = setInterval(function () {
if (!loadInProgress && state_index <= max_state_index) {
console.log('step ' + state_index);
switch (state_index) {
case 1:
ChatWork.step1();
break;
case 2:
ChatWork.step2();
break;
case 3:
ChatWork.step3();
break;
case 4:
ChatWork.fetchRoomIds();
break;
case 5:
ChatWork.sendMessage(options.message.value);
break;
}
state_index++;
}
if (!loadInProgress && state_index > max_state_index) {
console.log('test complete');
clearInterval(interval);
//phantom.exit();
}
}, 1000);
console.log('login end');
},
step1: function () {
var url = 'https://kcw.kddi.ne.jp/login.php?lang=en&args=';
/*
page.open(url, function (status) {
console.log("-- open page: " + status);
});
*/
page.open(url);
console.log('step1 end');
},
step2: function () {
console.log("-- enter credential");
evaluate(page, function (s) {
console.log("-- eval start");
var email = s[0];
var password = s[1];
document.querySelector('input[name="email"]').value = email;
document.querySelector('input[name="password"]').value = password;
console.log("-- eval end");
}, [options.email.value, options.password.value]);
console.log("login button clicked");
},
step3: function () {
console.log("-- submit");
evaluate(page, function (s) {
document.querySelector('form').submit();
}, []);
},
sendMessage: function (message) {
//var roomLink = findRoomLink(options.roomname.value);
//roomLink.click();
evaluate(page, function (s) {
var message = s[0];
var $chattext = document.querySelector("#cw_chattext");
$chattext.focus();
$chattext.value = message;
var $cw_send_button = document.querySelector("#cw_send_button");
$cw_send_button.click();
}, [message]);
//setTimeout(waitChatSent, 1000);
},
fetchRoomIds: function () {
var room_ids = evaluate(page, function (s) {
var room_ids = [];
var elements = document.querySelectorAll("div.cw_room_link");
for (var i = 0; i < elements.length; i++) {
var element = elements[i];
room_ids.push(element.getAttribute('id').replace('cw_r', ''));
}
return room_ids;
}, []);
this.room_ids = room_ids;
}
};
console.log('start');
if (system.args.length > 0) {
//openLoginScreen();
ChatWork.login();
phantom.state = 'sendMessage';
} else if (phantom.state == 'sendMessage') {
console.log('start sendMessage');
ChatWork.fetchRoomIds();
} else {
printHelp();
}
function checkLoaded() {
return (CW.last_timeline_buildkey != "");
}
function waitFirstLoad() {
if (!checkLoaded()) {
console.log("Waiting for first load to end");
setTimeout(function() {
waitFirstLoad()
}, 1000);
} else {
sendMessage();
}
}
function openLoginScreen() {
console.log("Getting the login screen");
var page = require('webpage').create();
var url = 'https://kcw.kddi.ne.jp/login.php?lang=en&args=';
page.open(url, function (status) {
console.log("Logging in");
$('input[name="email"]').attr("value", options.email.value);
$('input[name="password"]').attr("value", options.password.value);
$('input[name="login"]').click();
});
}
function printHelp() {
var sorted = [];
for (var o in options) {
sorted[options[o].idx] = o;
if (options[o].optional) {
sorted[options[o].idx] = "[" + sorted[options[o].idx] + "]";
}
}
var help = "Usage: phantomjs %prog ";
for (var i in sorted) {
help += sorted[i] + " ";
}
console.log(help);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment