Bot Build #12 - Stats Bot (Responder Bot)

in #utopian-io6 years ago (edited)


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

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!)

Sort:  

Congratulations! This post has been upvoted from the communal account, @minnowsupport, by TheLonelyWolF🐺 from the Minnow Support Project. It's a witness project run by aggroed, ausbitbank, teamsteem, theprophet0, someguy123, neoxian, followbtcnews, and netuoso. The goal is to help Steemit grow by supporting Minnows. Please find us at the Peace, Abundance, and Liberty Network (PALnet) Discord Channel. It's a completely public and open space to all members of the Steemit community who voluntarily choose to be there.

If you would like to delegate to the Minnow Support Project you can do so by clicking on the following links: 50SP, 100SP, 250SP, 500SP, 1000SP, 5000SP.
Be sure to leave at least 50SP undelegated on your account.

Thank you for your contribution.
While I liked the content of your contribution, I would still like to extend one advice for your upcoming contributions:

  • Structure of the tutorial: Improve the structure of your tutorial so that it is easier to read. An example of a good tutorial link

Looking forward to your upcoming tutorials.

Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.


Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]

espoem once won an underwater breathing contest with a fish.While urinating, espoem is easily capable of welding titanium.

Hey @lonelywolf
Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Contributing on Utopian
Learn how to contribute on our website or by watching this tutorial on Youtube.

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!


Hello LonelyWolF, Your Stats:
1957 SE Points = 1.957(SBD)$
158 SSE Points = 0.134(STEEM)$
21789 Points = 251 Votes / 502 Followers At The Exchange.

Have A Great Day!

Coin Marketplace

STEEM 0.18
TRX 0.16
JST 0.030
BTC 62539.28
ETH 2437.94
USDT 1.00
SBD 2.67