Bot Build #12 - Stats Bot (Responder Bot)
utopian-io·@lonelywolf·
0.000 HBDBot Build #12 - Stats Bot (Responder Bot)
 --- ### Repository e.g. https://github.com/steemit/steem-js ### What Will I Learn? - You will learn how to create a responder bot (stats bot). ### Requirements - Node.JS - Steem Package (Install: npm install steem --save) ### Difficulty - Advanced ### Tutorial Contents - You will learn how to create a steem responder bot (stats bot). ### Curriculum - [Bot Build 1 - 10](https://steemit.com/steem-bots/@lonelywolf/all-of-my-steem-bot-tutorials-in-one-post) - [Bot Build #11](https://steemit.com/utopian-io/@lonelywolf/bot-build-11-advanced-resteem-by-friend-list-bot) - [SteemJS Tutorial #1](https://utopian.io/utopian-io/@lonelywolf/steem-js-tutorials-1-basics-validating-wif-key) - [NodeJS Tutorial #1](https://utopian.io/utopian-io/@lonelywolf/nodejs-tutorials-1-creating-a-static-site-node-js) ### The Tutorial ## Step 1 - Setup Functions & Variables `Note`: this tutorial is the idea, you should use it for your own website with mysql. First of all, we need the user stats variable(json variable). ``` let user_stats = { "SE": 1957, "SSE": 158, "Points": 21789, "Name": "LonelyWolF" }; ``` this variable includes the username and all of his points. ` SE = SBD Points SSE = STEEM Points Points = Site Points Name = User Name ` we need a variable for our message, the stats message. ``` //Stats Message. const message = "<center><img src='https://i0.wp.com/levelmarketing.co.uk/wp-content/uploads/2018/01/stats-of-the-week-CONTENT-2017-840x460.jpg?fit=840%2C460&ssl=1' /><hr> Hello "+user_stats.Name+", Your Stats:<br> "+user_stats.SE+" SE Points = "+calculatePoints(user_stats.SE, "SE")+"(SBD)$<br> "+user_stats.SSE+" SSE Points = "+calculatePoints(user_stats.SSE, "SSE")+"(STEEM)$<br>"+user_stats.Points+" Points = "+calculatePoints(user_stats.Points, "Points")+" Votes / "+calculatePoints(user_stats.Points, "Points")*2+" Followers At The Exchange.<hr><h4>Have A Great Day!</h4></center>"; ``` It seems complicated but you only need to know this variable that you will use to send the message ``` calculatePoints() - we'll create this function user_stats.SE - returning the SE value user_stats.SSE - returning the SSE value user_stats.Points - returning the Points value ``` you can create your own message as you want. now we need a function to stream/send the comment, I'm not going to explain about the function because I've already used it a few times before! ``` // Comment Function function streamComment(author, permlink, permalink, body){ steem.broadcast.comment(ACC_KEY, author, permlink, ACC_NAME, permalink, '', body, JSON.stringify({ tags: ACC_NAME, app: 'steem-bot-advanced-resteem' }), function(err, result) { if(!!err) console.log("Comment Failed!", err); else console.log("Comment Posted Succesfully, Author: " + author); }); } ``` we need a function to create new permlink for the comment ``` function makeid(number) { var text = ""; var possible = "abcdefghijklmnopqrstuvwxyz"; for (var i = 0; i < number; i++) text += possible.charAt(Math.floor(Math.random() * possible.length)); return text; } ``` this function gets a number (integer) and randomly parsing text(words), for example, I choose number 5 it will parse `spwqz` for example. now we need account variables, this account is the official guest user of steemJS. ``` //Account Name & WIF(Posting) Key const ACC_NAME = 'guest123', //Account Name ACC_KEY = '5JRaypasxMx1L97ZUX7YuC5Psb5EAbF821kkAGtBj7xCJFQcbLg'; ``` you can use this account as you want! and we need the last function, function that calculates the points value. ``` function calculatePoints(points, type){ if(type == "SE") points = points/1000, points = points.toFixed(3); else if(type == "SSE") points = points/1180, points = points.toFixed(3); else if(type == "Points") points = points/87, points = Math.ceil(points); else throw "error, type invalid."; return points; } ``` the function gets the value of the points and the type, the type can be "SE", "SSE" or "Points" SE will calculate 1000 points to 1$(SBD) SSE will calculate 1180 points to 1$(STEEM) Points will calculate 87 points for 1 vote / 2 followers (This is an example for exchange site) now we just need to setup the streamTransactions function ``` // Custom Websocket. steem.api.setOptions({ url: 'wss://rpc.buildteam.io' }); //Streaming The latest Transactions That Goes Through The Steem Blockchain steem.api.streamTransactions('head', function(err, result) { // transaction type, the type of the transaction let type = result.operations[0][0]; // transaction data, the data that comes with the transaction let data = result.operations[0][1]; }); ``` we did this step alot of times so I'll not going to explain about that. ### Step 2 - Setup The Bot All of this step is inside the streamTransactions function. so first we need to check if the transaction type is a comment ``` if(type == "comment"){ } ``` if it is, we want to get the body of the comment (the content) ``` let body = data.body, ACCOUNT_NAME = "@" + ACC_NAME; ``` now we want to check if the author of the comment mentioned the bot and if he asked for his stats ``` //checking if the content(body) includes the mention and if the user requested his stats. if(body.includes(ACCOUNT_NAME) && body.includes("!BOT STATS!")){ console.log(data.author, "requested his stats"); /* you should use mysql to give him his real stats, but for this simple script, I'll use non-real stats */ ``` body.includes check if the string inside the function contains at the body and it checks if the author mentioned us and asked for the stats "!BOT STATS!" now we want to make a 25-second timer and send the comment (the stats) ``` // 25-second timer to not get the error "comment once every 20 seconds". setTimeout(function(){ streamComment(data.author, data.permlink, makeid(8), message); }, 25*1000); ``` and here we're done! just a little last step ### Step 3 *Not Required* - Test ``` const author = "lonelywolf", permlink = "bot-build-11-advanced-resteem-by-friend-list-bot", id = makeid(8), request = "@guest123 !BOT STATS!"; streamComment(author, permlink, id, request); ``` we'll get someone's post, for example, I took my post. we get the permlink and create new permlink for the comment and create a variable for the stats request and then we just send the comment and it will automatically work and it will send the stats to the comment. results:  have a great day! ***You can check the full work at my repl.it account and other bots that I create!*** ### Proof of Work Done the work made in Repl.it, https://repl.it/@lonelywolf22/Steem-Bots-Stats-Bot-V11-Done user: https://repl.it/@lonelywolf22 GitHub: https://github.com/lonelywolf1 Utopian Tutorials GitHub Repository: https://github.com/lonelywolf1/Bot-Projects-SteemJS (This tutorial will add to the GitHub repository in the next few days!)
👍 hackerzizon, lersus77, cleavageobjects, fedykosoy00, palmcomposed, tripadvisorbew, blackberry777, baejz, vaelnaltasa, kelal, hunangalstyan, minasyanvahe, pignutcraft, ellibor, ikalinowski, nautilusx, damiralanov, towheelocate, coalorebrunch, testifyservant, visitorsahem, finesuper, sigur, minnowbbooster, yuxi, gartiv88, jorikmxitaryan, halfpox, pavel.dalak, elvinhender, fryderykn, portionvolume, bowlinediamox, plopclone, hsufrank, denistolakov, antonpalesov, alenaesarionova, rebrovivan, mvetrov88, trubadurkir, exoticbaste, betweeneris, eclipticmelange, dancerant, dotfanning, curiouscred, cityscapeleaf, cootroach, abcsleeveless, bumpyforster, buildawhhale, minnowsupport, ityp, teampeople, cryptkeeper17, doomsdaychassis, smylie2005, royaleagle, bryarose23, beeyou, gracefavour, saifuddin07, wandy01, murhadi9, syahril9, dubmenikki, akmal93, emailbox19149, terminallyill, steaknsteem, mvanyi, jjay, jaff8, bachuslib, properfraction, moserich, fth, r351574nc3, salty-mcgriddles, exifr, exifr0, costanza, utopian-io, zartyo,