Title: [Tutorial] Writing a bot for coinchat using the coinchat-client NPM module Post by: SargoDarya on September 16, 2013, 01:38:12 PM Hey there folks,
a few weeks ago I was stumbling over CoinChat and was kind of impressed by it and quickly wanted to write a bot for a game I had in mind. First bot was done and I struggled with so many CoinChat stuff that I decided to write a coinchat-client module for easier re-use. The code for it can be found either on GitHub (http://"http://github.com/sargodarya/coinchat-client") or installed by using npm install coinchat-client. Also, if you have no experience writing JavaScript or have not yet touched anything Node.js, this tutorial probably isn't for you and you should start by getting into those two topics. For the purpose of this Tutorial, we're going to create a bot which allows you to flip a coin and depending on the outcome, tip the user a custom amount. Unless you're running on a Unix based system I suggest you running a virtual machine with Linux on it. I suggest NOT to just copy/paste but variate some of the stuff I write to get a feeling for the framework. Getting Started We start by creating a new project folder for our bot. Go to a directory of your choice, and type the following: Code: mkdir dicebot && cd dicebot It will automatically install all dependencies of coinchat-client and you're ready to go. Connecting to and logging in on CoinChat Connecting to coinchat really couldn't be any easier. First of, we're going to require the coinchat-client library and create a new instance of it. We need to pass an object to the coinchat-client so the framework knows its credentials (Note: You also may set the credentials at a later stage). After we have created a client, we're going to connect to the server. The connect method takes a callback which gets called when we are successfully connected to the server. In the callback, we're logging in with the specified credentials at the beginning which again, takes a callback which is called upon successful login. Code: var CoinChatClient = require('coinchat-client'); If you now run the exact code with the credentials given, you'll receive an exception. This is expected as the account is not registered and or the password is wrong. Both cases are an exit condition in which the code is expected to fail because it obviously cannot run in its current state (Note: You can use a try/catch around it but it really doesn't make sense). Change the credentials to your own ones and it should give you all debug messages thus showing a complete connection. Handling Messages The framework wouldn't be any fun if it wouldn't deal with the messages. In it's current form it abstracts away all the nitty gritty details and lets you work with the messages. For this, the client instance provides the register method. It either takes a function or an object containing a method handleMessage to deal with the messages in a standardized form regardless if they have color enabled or not. Let's get to the details and enhance our example a bit. First, let us define a function which should act as our message handler. Code: function messageParser(msg) { What this does is quite easy. It gets the message as a first parameter and writes a debug message to the console. Right now it doesn't work though because our client does not yet know the function. Let's extend the first code example and change it to look like this: Code: ... Now, every message that gets sent is received by our messageParser function and gets outputted to our command line. Well, ok. I lied. It works, but you won't see any message right now because you're not yet in any room! Let's fix this up... Joining and leaving rooms No fun without rooms. Let's make this quick. The client instance provides two methods for this, join and leave, both taking a rooms name as the first message. Inside your login callback type Code: client.join('foobar') and your bot will connect to the room. If you now write a message in there it will be outputted to your console. Isn't this great? What? You want to answer those messages? God you guys are hard to satisfy... Writing messages Bots have a limitation when it comes to writing messages which is, don't send multiple messages in less than 550ms or your bot will get muted. The framework aids you in this that it has a so called message pump which queues and sends messages in an interval thus not getting muted. For this there are 3 methods: Code: client.pushMessage(roomName, message, [color]); Each of those methods will push a new message to the message queue which gets delivered then. Go on, try it out in the messageHandler function we wrote earlier (Note: pushTip does NOT work if your bots balance is below 0.25mBTC as this is the minimal tip amount). Receiving tips When a message reaches your message handler function, there is an attribute on it called isTip. Always check isTip in your message handler first. If isTip is set, there will also be the tipAmount on it. Using an Object for message handling By now you should know most of the framework features and we're going to extend our example a bit. Remember earlier when I said you could use an object as a message handler? Well, now is the part where we are going to use exactly that. Let us first specify what our bot should be able to do. If you type !roll it should throw one 6 sided dice and output a message. If you type !roll 20 it should roll 20 6 sided die and if you do !roll 5 20 it should throw 5 20 sided die. As an admin only feature it should also have a shutdown command to disconnect from the server. Lets get started writing the bot in a traditional "OOP" manner: Code: var DiceBot = function(client) { Phew, that was a lot of code but you should understand it. The part you should notice in here is that we defined a handleMessage method. If we register an instance of the bot it will automatically call that message. Save that file in the same directory and name it "dicebot.js". Now, let's integrate this into our earlier sample. Code: var CoinChatClient = require('coinchat-client'), Run this code with your own credentials and everything should work. If you have any questions feel free to ask. If you want to add features to the framework just drop a pull request on GitHub and I will merge it. The tutorial can also be found on GitHub in the sample directory. Note: If you're going to handle balances be sure to add in a persistence layer as MySQL or MongoDB. Losing user balances is a pain and can get you banned. I hope you enjoyed this small tutorial and write some quality bots to contribute to coinchat. Cheers, Sargo Darya Title: Re: [Tutorial] Writing a bot for coinchat using the coinchat-client NPM module Post by: Anillos2 on September 16, 2013, 03:03:55 PM Thanks, now We will need to fill stupid captchas for chatting. >:(
Title: Re: [Tutorial] Writing a bot for coinchat using the coinchat-client NPM module Post by: roflmao129 on September 16, 2013, 03:05:41 PM Thanks, now We will need to fill stupid captchas for chatting. >:( Hm? Bots are allowed, they dont receive auto-tips. If youre botting without it being called anything with bot at the end youre going to be banned pretty fast. Title: Re: [Tutorial] Writing a bot for coinchat using the coinchat-client NPM module Post by: cainy393 on September 16, 2013, 06:39:51 PM This is really nice. I wrote a tutorial on making a bot from scratch here (https://bitcointalk.org/index.php?topic=283958) but ill be sure to link to this post too since it makes things a lot simpler! Really great work, keep it up! :D
Title: Re: [Tutorial] Writing a bot for coinchat using the coinchat-client NPM module Post by: MoneyGod on September 16, 2013, 07:07:47 PM thanks for this very useful help and thread cheers
Title: Re: [Tutorial] Writing a bot for coinchat using the coinchat-client NPM module Post by: dezza on September 28, 2013, 04:06:35 PM Thanks for a great and clean tutorial.
regards, dza Title: Re: [Tutorial] Writing a bot for coinchat using the coinchat-client NPM module Post by: Anon135246 on September 28, 2013, 06:46:07 PM Nice tutorial :)
Title: Re: [Tutorial] Writing a bot for coinchat using the coinchat-client NPM module Post by: 🏰 TradeFortress 🏰 on October 13, 2013, 07:24:55 AM Nice tutorial :) agreed :)Title: Re: [Tutorial] Writing a bot for coinchat using the coinchat-client NPM module Post by: Mondy on October 13, 2013, 08:31:19 AM thank you for taking the time to write up the tutorial. i havnt started yet but im going to soon
Title: Re: [Tutorial] Writing a bot for coinchat using the coinchat-client NPM module Post by: tautvilis on October 13, 2013, 09:13:31 AM Thanks for this, but i think it will be patched or something in few weeks
Title: Re: [Tutorial] Writing a bot for coinchat using the coinchat-client NPM module Post by: faiza1990 on October 13, 2013, 09:34:40 AM what is work of this bot on coinchat can you some explain Title: Re: [Tutorial] Writing a bot for coinchat using the coinchat-client NPM module Post by: galbros on October 13, 2013, 02:06:49 PM For people just starting out this is a really well done tutorial. Thanks for taking the time to comment on all of the steps and laying things out so clearly.
|