Created
November 28, 2011 17:31
-
-
Save darkua/1401205 to your computer and use it in GitHub Desktop.
ManyBots APi, nodejs authtoken example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var myLib = require('./urlReq'); | |
var Manybots = { | |
base_uri : 'https://www.manybots.com', | |
req_params : { | |
auth_token : 'YOUR API TOKEN :)', | |
page : 1, | |
per_page : 20 | |
}, | |
filter_activities : function(filters){ | |
this.url = this.base_uri+'/activities/filter.json'; | |
this.req_params.filter = filters; | |
this.request(); | |
}, | |
bundle_activities : function(filters){ | |
this.url = this.base_uri+'/activities/bundle.json'; | |
this.req_params.filters = filters; | |
this.request(); | |
}, | |
create_activity : function(activity){ | |
this.url = this.base_uri+'/activities.json'; | |
this.req_params.version = '1.0'; | |
this.req_params.activity = activity; | |
this.request(); | |
}, | |
request : function(){ | |
myLib.urlReq(this.url, { | |
method: 'POST', | |
params:this.req_params | |
}, function(body, res){ | |
var obj = JSON.parse(body); | |
if(obj.data){ | |
var items= obj.data.items; | |
for (var i = items.length-1;i >=0;i--) { | |
console.log( items[i].actor.displayName + ' '+ items[i].verb +' '+ | |
items[i].object.displayName + ' : ' + items[i].target.displayName); | |
} | |
}else{ | |
console.log(obj); | |
} | |
}); | |
} | |
} | |
Manybots.filter_activities({objects:'phone-call'}); | |
Manybots.bundle_activities([ | |
{objects:'phone-call',target_values:'RUI TELELE'}, | |
{objects:'phone-call',target_values:'PAI'} | |
]); | |
Manybots.create_activity({ | |
auto_title : true, | |
title : 'ACTOR like OBJECT', | |
verb : 'like', | |
object : { | |
objectType : 'script', | |
id : 'http://ruby-lang.org', | |
url : 'http://ruby-lang.org', | |
displayName : 'Oranges' | |
}, | |
generator : { | |
url : 'http://myscript.com', | |
id : 'http://myscript.com', | |
displayName : 'My Script', | |
}, | |
provider : { | |
url : 'http://myscript.com', | |
id : 'http://myscript.com', | |
displayName : 'My Script', | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// module dependencies | |
var http = require('http'), | |
url = require('url'); | |
/** | |
* UrlReq - Wraps the http.request function making it nice for unit testing APIs. | |
* | |
* @param {string} reqUrl The required url in any form | |
* @param {object} options An options object (this is optional) | |
* @param {Function} cb This is passed the 'res' object from your request | |
* | |
*/ | |
exports.urlReq = function(reqUrl, options, cb){ | |
if(typeof options === "function"){ cb = options; options = {}; }// incase no options passed in | |
// parse url to chunks | |
reqUrl = url.parse(reqUrl); | |
// http.request settings | |
var settings = { | |
host: reqUrl.hostname, | |
port: reqUrl.port || 80, | |
path: reqUrl.pathname, | |
headers: options.headers || {}, | |
method: options.method || 'GET' | |
}; | |
// if there are params: | |
if(options.params){ | |
options.params = JSON.stringify(options.params); | |
settings.headers['Content-Type'] = 'application/json'; | |
settings.headers['Content-Length'] = options.params.length; | |
}; | |
// MAKE THE REQUEST | |
var req = http.request(settings); | |
// if there are params: write them to the request | |
if(options.params){ req.write(options.params) }; | |
// when the response comes back | |
req.on('response', function(res){ | |
res.body = ''; | |
res.setEncoding('utf-8'); | |
// concat chunks | |
res.on('data', function(chunk){ res.body += chunk }); | |
// when the response has finished | |
res.on('end', function(){ | |
// fire callback | |
cb(res.body, res); | |
}); | |
}); | |
// end the request | |
req.end(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment