Skip to content

Instantly share code, notes, and snippets.

@vvakame
Last active May 16, 2019 14:57
Show Gist options
  • Save vvakame/8782485 to your computer and use it in GitHub Desktop.
Save vvakame/8782485 to your computer and use it in GitHub Desktop.
idobataはAPIのドキュメントがないので頑張って調べた https://idobata.io/
var http = require("https");
var qs = require('querystring');
var Pusher = require('pusher-client');
var urlBase = "https://idobata.io";
var token = "xxxxx"; // ここにBOTのtokenいれる
var pusherKey = "44ffe67af1c7035be764";
function request(method, path, params) {
var options = {
method: method,
hostname: "idobata.io",
port: 443,
path: path,
headers: {
"X-API-Token": token,
"User-Agent": "vv-sample"
}
};
if(params && method === "GET") {
options.path += "?" + qs.stringify(params);
} else if(params) {
options.headers["Content-Length"] = qs.stringify(params).length;
}
var req = http.request(options, function(res){
res.setEncoding('utf8');
var data = "";
res.on("data", function (chunk) {
data += chunk;
});
res.on("end", function() {
successCallback(res, data);
});
});
if(params && method !== "GET") {
req.write(qs.stringify(params));
}
req.end();
var successCallback = function() {};
var errorCallback = function() {};
return {
success: function(callback) {
successCallback = callback;
},
error: function(callback) {
errorCallback = callback;
}
};
}
function dump(path, promise) {
promise.success(function(response, body) {
console.log(path);
var data = JSON.parse(body);
console.log(JSON.stringify(data, null, 2));
});
promise.error(function(response, body) {
console.log(path);
console.error("error! " + response.statusCode);
});
}
function getSeed() {
var path = "/api/seed";
var p = request("GET", path);
dump(path, p);
}
function getOrganizations(organizationSlug) {
var path = "/api/organizations";
var options;
if(organizationSlug) {
options = {
organization_slug: organizationSlug
};
}
var p = request("GET", path, options);
dump(path, p);
}
function getUsers(ids) {
var path = "/api/users";
var options;
if(ids) {
options = {
"ids[]": ids
};
}
var p = request("GET", path, options);
dump(path, p);
}
function getBots(ids) {
var path = "/api/bots";
var options;
if(ids) {
options = {
"ids[]": ids
};
}
var p = request("GET", path, options);
dump(path, p);
}
function getMessages(roomId, olderThan) {
var path = "/api/messages";
var options;
if(olderThan) {
options = {
"older_than": olderThan
};
}
var p = request("GET", path, options);
dump(path, p);
}
function postMessages(roomId, source) {
var path = "/api/messages";
var options = {
"message[room_id]": roomId,
"message[source]": source
};
var p = request("POST", path, options);
dump(path, p);
}
function getHooks(ids) {
var path = "/api/hooks";
var options;
if(ids) {
options = {
"ids[]": ids
};
}
var p = request("GET", path, options);
dump(path, p);
}
function connectToPusher() {
var pusher = new Pusher(pusherKey, {
encrypted: true,
authEndpoint: urlBase + "/pusher/auth",
auth: {
headers: {
"X-API-Token": token,
"User-Agent": "vv-sample"
}
}
});
var channel = pusher.subscribe("????"); // getBots() とかやるとsubscribeするチャンネルがわかる
channel.bind("message_created", function(data) {
console.log(JSON.stringify(data, null, 2));
});
}
// getSeed();
// getOrganizations();
// getOrganizations("topgate");
// getUsers();
// getUsers([1181]);
// getBots();
// getBots(1185);
// getMessages(1067);
// postMessages(1067, "Hello! by bot " + new Date());
// getHooks();
// getHooks(1117);
// connectToPusher();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment