TUTORIAL - Beginner - Build your own Steem Upvote Bot - JavaScript - Part 7

in #utopian-io7 years ago (edited)

wallpaper_part7.png

What Will I Learn?

This Tutorial Series is all about creating a basic steem bid-based upvoting bot. We are basically coming to an end with this and the next tutorial for the series of how to create an upvote bot. In this part I want to cover how your upvote bot can create comments under the post it has upvoted, since some bots use this behaviour to seek attention.

An Example:
Screen Shot 2018-02-09 at 14.02.22.png

Requirements

Difficulty

  • Basic

Tutorial Contents

Posting Comments

Posting comments in steemit work exactly the same as creating posts since both is seen as comments. The difference between a comment and a post is, a post does not have a parent comment / author - a comment does.

Creating a post can be done with one single function:

function sendComment(vote) {
    const permlink = 're-' + vote.author.replace(/\./g, '') + '-' + vote.permlink + '-' + new Date().toISOString().replace(/-|:|\./g, '').toLowerCase();
    const comments = config.comments;

    // Broadcast the comment
    steem.broadcast.comment(config.private_posting_key, vote.author, vote.permlink, account.name, permlink, permlink, comment, '{"app":"upvoter/"}', function (err, result) {
      if (!err && result) {
        console.log('Posted comment: ' + permlink);
      } else {
        console.log('Error posting comment: ' + permlink + ', Error: ' + err);
      }
    });
}
  • Permlink:
    The permlink is needed and we will generate the permlink via steemits standard convention. Simply copy it and leave it like this.
  • Comments:
    We will come to this part later. Basically we will create an array of possible comments our bot can post in our config and randomly select one.
  • steem.broadcast.comment(wif, parentAuthor, parentPermlink, author, permlink, title, body, jsonMetadata, function(err, result) {...} is used to broadcast a comment to the blockchain. Since a comment does not have a title we use the permlink too. wif equals our private_posting_key and jsonMetadata is just some metadata, change the value for app to everything you like.

Now after we created the function we need to call it. The best place is after we successfully voted

    steem.broadcast.vote(config.private_posting_key, account.name, vote.author, vote.permlink, 10000, function (err, result) {
        if (err && !result) {
            console.log('Voting failed: ' + err);
            return;
        }

        sendComment(vote);

        if (callback) {
            callback();
        }
    });

Random comments

A bot which always comment the same sentence under the posts it upvoted would be way to boring and way to lame. An awesome bot is able to post different comments !

Head over to your config.json and add some possible comments your bot should post, e.g:

    "comments": [
        "Hi! I've just found you and this is crazy, but here's my vote so follow me maybe?",
        "You got an upvote from UpVote making your revenue going UUUUUPPPPP",
        "Voted for you my fellow steemian! Thanks for paying",
    ]

Now go back to the line const comments = config.comments; inside of your index.js and add following code below

const comment = comments[Math.floor(Math.random() * comments.length)];

This small line does:

  • Choses a random number between 0-1 (Math.random())
  • Multiply it with the amount of comments our bot uses
  • Round down the solution to have Integer values
  • Select the comment with index = solution from the comments list

Try it

Screen Shot 2018-02-09 at 15.04.02.png

Full Source Code

Curriculum



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

@moonrise, I like your contribution to open source project, so I upvote to support you.

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Hey @moonrise I am @utopian-io. I have just upvoted you!

Achievements

  • WOW WOW WOW People loved what you did here. GREAT JOB!
  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

Coin Marketplace

STEEM 0.18
TRX 0.15
JST 0.029
BTC 62837.64
ETH 2542.11
USDT 1.00
SBD 2.65