Skip to content

Instantly share code, notes, and snippets.

@hkjels
Forked from jacques/clickatell.js
Created April 9, 2011 16:32
Show Gist options
  • Save hkjels/911528 to your computer and use it in GitHub Desktop.
Save hkjels/911528 to your computer and use it in GitHub Desktop.
/**
*/
config = require('yaml').eval(
require('fs')
.readFileSync(process.env['HOME'] + '/.clickatell.yml')
.toString('utf-8')
);
var clickatell = require("../lib/clickatell.js");
clickatell = new clickatell(config)
console.log (clickatell)
/**
console.log ("clickatell.auth: start")
ret = clickatell.auth(function(err, callback){
console.log(ret, function(){console.log("moo")})
console.log(clickatell)
})
/**
console.log ("clickatell.auth: end")
console.log(clickatell)
ret = clickatell.auth(function(){
console.log(ret)
console.log(clickatell)
})
*/
console.log(clickatell.getbalance())
/**
* Wrapper around the Clickatell HTTP/S API Interface
*
* Needs loads of love to get working 100%
*/
var http = require('http');
var sys = require('sys');
/**
*
*/
function clickatell(opts) {
if(!(this instanceof clickatell)) {
return new clickatell(opts);
}
console.log(opts)
if (!opts.username || !opts.password || !opts.api_id) {
throw new Exception("Missing a param");
}
this.username = opts.username;
this.password = opts.password;
this.api_id = opts.api_id;
this.session_id = '04c7f7085ecc56b23229dac97f92b494';
this.http = http.createClient(443, 'api.clickatell.com', true);
}
module.exports = clickatell;
clickatell.prototype.auth = function() {
console.log("clickatell.protoytype.auth: beginning")
if (this.session_id != null) {
console.log("clickatell.protoytype.auth: exiting - session_id is not null")
return true;
}
// (function() {
// return function (callback, errback) {
//}
console.log ("not authed == authing")
//# STATUS: 200
//# HEADERS: {"date":"Mon, 14 Feb 2011 19:34:44 GMT","server":"Apache/2.0.52 (Red Hat)","x-powered-by":"PHP/5.2.6","connection":"close","transfer-encoding":"chunked","content-type":"text/html"}
//# BODY: ERR: 001, Authentication failed
//BODY: OK: 159690a06b95b67d82d183a27358d5b7
//[ 'OK', '159690a06b95b67d82d183a27358d5b7' ]
// 04c7f7085ecc56b23229dac97f92b494
post_data = 'user='+this.username+'&password='+this.password+'&api_id='+ this.api_id
var request = this.http.request('POST', '/http/auth',
{
'Host': 'api.clickatell.com',
'User-Agent': 'hello danie',
'Content-Length': (post_data.length),
'Content-Type': 'application/x-www-form-urlencoded'//,
// 'Connection' : 'keep-alive',
// 'Keep-Alive' : 'true'
});
request.write(post_data);
request.end();
request.on('response', function (response) {
console.log('STATUS: ' + response.statusCode);
console.log('HEADERS: ' + JSON.stringify(response.headers));
response.setEncoding('utf8');
response.on('data', function (chunk) {
console.log('BODY: ' + chunk);
bits = chunk.split(": ")
if (bits[0] == 'OK') {
console.log(bits[0])
console.log(bits[1])
this.session_id = bits[1]
console.log(this)
} else {
//# sys.exit("foo")
console.log(chunk)
}
});
});
//})
console.log(this)
}
clickatell.prototype.getbalance = function(suc, err) {
if (this.session_id == null) {
console.log ("not authed == authing")
this.auth()
}
post_data = 'session_id='+this.session_id
var request = this.http.request('POST', '/http/getbalance',
{
'Host': 'api.clickatell.com',
'User-Agent': 'hello danie',
'Content-Length': (post_data.length),
'Content-Type': 'application/x-www-form-urlencoded'//,
// 'Connection' : 'keep-alive',
// 'Keep-Alive' : 'true'
});
request.write(post_data);
request.end();
request.on('response', function (response) {
console.log('STATUS: ' + response.statusCode);
console.log('HEADERS: ' + JSON.stringify(response.headers));
response.setEncoding('utf8');
response.on('data', function (chunk) {
console.log('BODY: ' + chunk);
bits = chunk.split(": ")
if (bits[0] == 'Credit') {
console.log(bits[0])
console.log(bits[1])
this.session_id = bits[1]
console.log(this)
} else {
return false;
//# sys.exit("foo")
console.log(chunk)
}
});
});
console.log(this)
}
/*
clickatell.prototype.ping = function(suc, err) {
(function() {
if (this.session_id == null) {
console.log ("not authed == authing")
this.auth()
}
})
console.log(this)
}
clickatell.prototype.sendmsg = function(opts, suc, err) {
if (this.session_id == null) {
console.log ("not authed == authing")
this.auth()
if (this.session_id != null) {
console.log ("yeah we got in")
} else {
console.log ("boo")
}
}
console.log(this)
}
*/
@hkjels
Copy link
Author

hkjels commented Aug 10, 2011

Would you look at that. I forgot about this one completely.
I made a module myself that worked flawlessly. I'll open-source it one of these days I guess. Just a lot on my plate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment