Want to write your own bot for CoinChat, a fast web based chat network integrated with bitcoin?
1. Install node.js and socket.io-client (npm install socket.io-client).
2. Register a new account for your bot by using a new browser or clearing your cookies. It must end in bot. Grab the session key from your cookies. This is how you'll sign in.
3. Use this code to get started:
var io = require('socket.io-client');
socket = io.connect("https://coinchat.org", {
secure: true
});
var username = "";
var outputBuffer = [];
socket.on('connect', function () {
//Your session key (aka API key)
//Get this from your browser's cookies.
socket.emit('login', {
session: "YOUR_SESSION_KEY_HERE"
});
socket.on('loggedin', function (data) {
username = data.username;
setTimeout(function () {
socket.emit("getcolors", {});
}, 1000);
setInterval(function () {
//CoinChat has a 550ms anti spam prevention. You can't send a chat message more than once every 550ms.
if (outputBuffer.length > 0) {
var chat = outputBuffer.splice(0, 1)[0];
socket.emit("chat", {
room: chat.room,
message: chat.message
});
}
}, 600);
});
socket.on('chat', function (data) {
if (contains(data.message, ["hi", username])) {
outputBuffer.push({
room: data.room,
message: 'Hi ' + data.user + "!"
});
}
if (contains(data.message, ["slaps", "ccbot"])) {
outputBuffer.push({
room: data.room,
message: "/me slaps " + data.user + " around a bit with a large trout."
});
}
if (contains(data.message, ["has tipped " + username])) {
var amount = data.message.split("has tipped " + username + " ")[1].split(" ")[0];
outputBuffer.push({
room: data.room,
message: "Thanks for the " + amount + " mBTC tip " + data.user + "!"
});
}
if (contains(data.message, ["!flip"])) {
var res = (Math.random() > 0.5 ? "heads" : "tails");
socket.emit("chat", {
room: data.room,
message: "Flipping coin: " + res + "!"
});
}
});
socket.on('disconnect', function () {});
});
function contains(string, terms) {
for (var i = 0; i < terms.length; i++) {
if (string.toLowerCase().indexOf(terms[i].toLowerCase()) == -1) {
return false;
}
}
return true;
}
There isn't any formal documentation for the API yet, but look through the
chat client code here to reverse-engineer it (it's really simple if you know socket.io). Feel free to ask any questions here, or /pm admin