Bitcoin Forum

Economy => Service Discussion => Topic started by: 🏰 TradeFortress 🏰 on April 27, 2013, 09:27:44 AM



Title: (Node.js) Build your own bot kit for CoinChat
Post by: 🏰 TradeFortress 🏰 on April 27, 2013, 09:27:44 AM
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, ["<span class='label label-success'>has tipped " + username])) {
            var amount = data.message.split("<span class='label label-success'>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 (https://coinchat.org/static/js/scripts.js) here to reverse-engineer it (it's really simple if you know socket.io). Feel free to ask any questions here, or /pm admin


Title: Re: (Node.js) Build your own bot kit for CoinChat
Post by: vlees on June 01, 2013, 10:48:17 PM
Code:
						if(contains(data.message, ["<span class='label label-success'>has tipped " + username])){
var amount = data.message.split("<span class='label label-success'>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?


Title: Re: (Node.js) Build your own bot kit for CoinChat
Post by: 🏰 TradeFortress 🏰 on June 02, 2013, 08:23:49 AM
It would, but this is how it works now on the server.


Title: Re: (Node.js) Build your own bot kit for CoinChat
Post by: ondratra on June 02, 2013, 11:43:12 AM
Can you explain me in short what it is supposed to do? And the code is JS?


Title: Re: (Node.js) Build your own bot kit for CoinChat
Post by: ondratra on June 02, 2013, 11:43:55 AM
I take off JS question :D:D i checked topic label too late :D


Title: Re: (Node.js) Build your own bot kit for CoinChat
Post by: bayu41 on June 29, 2013, 09:00:26 AM
can you give me tutorial to create Bot...?
please.... PM me...
i want create


Title: Re: (Node.js) Build your own bot kit for CoinChat
Post by: Shinodan on June 29, 2013, 08:13:20 PM
I cant get this to run without an error, probably doing it wrong, any more advice? it says error on line 1 or something :/


Title: Re: (Node.js) Build your own bot kit for CoinChat
Post by: whydifficult on June 30, 2013, 03:02:57 PM
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.


Title: Re: (Node.js) Build your own bot kit for CoinChat
Post by: 🏰 TradeFortress 🏰 on July 01, 2013, 01:03:48 AM
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.


Title: Re: (Node.js) Build your own bot kit for CoinChat
Post by: Shinodan on July 01, 2013, 12:19:45 PM
http://gyazo.com/ad19b327d55d22729e7c14b43fe2ecae <screenshot


Title: Re: (Node.js) Build your own bot kit for CoinChat
Post by: whydifficult on July 01, 2013, 05:23:55 PM
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.


Title: Re: (Node.js) Build your own bot kit for CoinChat
Post by: bayu41 on July 08, 2013, 11:05:28 PM
How to Install node.js and socket.io-client?
give tutor please


Title: Re: (Node.js) Build your own bot kit for CoinChat
Post by: assortmentofsorts on July 09, 2013, 06:00:32 PM
http://gyazo.com/ad19b327d55d22729e7c14b43fe2ecae <screenshot

Create a JS file with the code provided by tradefortress (lets call it botkit.js). Then from your command prompt, run: "node botkit.js"


Title: Re: (Node.js) Build your own bot kit for CoinChat
Post by: assortmentofsorts on July 09, 2013, 06:01:57 PM
How to Install node.js and socket.io-client?
give tutor please

Whats your OS?


Title: Re: (Node.js) Build your own bot kit for CoinChat
Post by: whiskers75 on July 10, 2013, 06:27:14 AM
Argh, I'll make an API later :P

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


Title: Re: (Node.js) Build your own bot kit for CoinChat
Post by: cronopio on July 24, 2013, 05:12:02 AM
THank you all!

All this was so helpful!!


Title: Re: (Node.js) Build your own bot kit for CoinChat
Post by: faiza1990 on August 16, 2013, 03:16:27 PM
How to Install node.js and socket.io-client?


Title: Re: (Node.js) Build your own bot kit for CoinChat
Post by: tspacepilot on August 17, 2013, 06:07:00 AM
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!


Title: Re: (Node.js) Build your own bot kit for CoinChat
Post by: tspacepilot on August 17, 2013, 06:16:55 AM
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).


Title: Re: (Node.js) Build your own bot kit for CoinChat
Post by: 🏰 TradeFortress 🏰 on August 18, 2013, 12:37:31 PM
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 :)