Author

Topic: (Node.js) Build your own bot kit for CoinChat (Read 4526 times)

vip
Activity: 1316
Merit: 1043
👻
August 18, 2013, 08:37:31 AM
#20
I've really enjoyed making and testing bots on coinchat.  It's taught me a lot about node.js and socket.io  I really appreciate the opportunity!

Glad to hear that Smiley
legendary
Activity: 1456
Merit: 1078
I may write code in exchange for bitcoins.
How to Install node.js and socket.io-client?
Depends on your OS.

I'm running debian wheezy.  I just got the source and did ./configure && make && make install (basically).
legendary
Activity: 1456
Merit: 1078
I may write code in exchange for bitcoins.
I've really enjoyed making and testing bots on coinchat.  It's taught me a lot about node.js and socket.io  I really appreciate the opportunity!
sr. member
Activity: 420
Merit: 250
★☆★777Coin★☆★
How to Install node.js and socket.io-client?
newbie
Activity: 55
Merit: 0
THank you all!

All this was so helpful!!
hero member
Activity: 658
Merit: 502
Doesn't use these forums that often.
Argh, I'll make an API later Tongue

EDIT: If you want a REAL functioning example of a bot, check out the source code of WhiskDiceBot (a bot like SatoshiDice):
https://github.com/whiskers75/coinchat-bot
member
Activity: 91
Merit: 10
How to Install node.js and socket.io-client?
give tutor please

Whats your OS?
member
Activity: 91
Merit: 10

Create a JS file with the code provided by tradefortress (lets call it botkit.js). Then from your command prompt, run: "node botkit.js"
newbie
Activity: 39
Merit: 0
How to Install node.js and socket.io-client?
give tutor please
sr. member
Activity: 287
Merit: 250
Windows doesn´t understand what you are trying to do with the command `socket io-client`, neither am I for that matter.

You can run a node script by typing in `node [scriptname]` instead of just the name of the script.

EDIT: so you can use the script provided by the OP, this means: copy and paste the script in a file, call it something.js and put it in the same directory. After that you can run it using node in your terminal.
vip
Activity: 1316
Merit: 1043
👻
I cant get this to run without an error, probably doing it wrong, any more advice? it says error on line 1 or something :/
You probably need socket.io client.
sr. member
Activity: 287
Merit: 250
I cant get this to run without an error, probably doing it wrong, any more advice? it says error on line 1 or something :/

Could you post the error? That way we can see what's wrong.
member
Activity: 63
Merit: 10
I cant get this to run without an error, probably doing it wrong, any more advice? it says error on line 1 or something :/
newbie
Activity: 39
Merit: 0
can you give me tutorial to create Bot...?
please.... PM me...
i want create
sr. member
Activity: 350
Merit: 250
I take off JS question Cheesy:D i checked topic label too late Cheesy
sr. member
Activity: 350
Merit: 250
Can you explain me in short what it is supposed to do? And the code is JS?
vip
Activity: 1316
Merit: 1043
👻
It would, but this is how it works now on the server.
full member
Activity: 196
Merit: 100
Code:
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 + "!"});
}

Wouldn't it make more sense to send a 'tip' event instead of parsing the text from a chat message?
vip
Activity: 1316
Merit: 1043
👻
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:

Code:
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
Jump to: