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

in #utopian-io7 years ago (edited)

wallpaper_part8.png

What Will I Learn?

This Tutorial Series is all about creating a basic steem bid-based upvoting bot. This part will probably be the last part of this bot creation series. We want to focus on two factors today:

  • More customized refund memo messages
  • Disallow upvotes on comments

Screen Shot 2018-02-10 at 14.56.18.png

Requirements

Difficulty

  • Basic

Tutorial Contents

Disable comment upvoting

Our bot should not be able to upvote comments, we only want to allow posts. For this we need to check whether the permlink we parsed from the memo field is a comment or a post. This is done by our function checkValidMemo(). We only need to add 4 lines more code !

Inside the getContent API call, we get as result the content of the post we want to upvote. Inside the result data exists a variable called parent_author ! We need to check if parent_author is empty. If not then we know it's a comment and not a post!

// Disable comment upvoting
if(result.parent_author != null && result.parent_author != '') {
  refund(sender, amount, 'no_comments');
  return;
}

In detail we check if result.parent_author is not empty, if so we send a refund with a custom no_comments message, which we will create in a second.

Head over to your config.json and create a new refund message with the key no_comments

        "already_voted": "Sorry, we already voted on this post!",
        "no_comments": "Upvoting comments is not allowed, sorry for your inconvenience"

Custom refund memo messages

Our bot is already able to send refund messages with custom messages for each different error type. But it is quite unpersonal and does not provide information about the amount send and the user. We want to change that now!

The idea behind it is to use variables inside your memo message which we will replace with user data.

So stay inside your config.json and change your memo messages to something similar like:

    "memo_messages": {
        "invalid_bid": "{sender}, your bid of {bid} is invalid!",
        "invalid_currency": "{sender}, your bid of {bid} is an invalid currency!",
        "post_to_old": "{sender}, your post is too old for upvoting it!",
        "invalid_memo": "{sender}, the memo you sended is invalid!",
        "already_voted": "Sorry {sender}, we already voted on this post!",
        "no_comments": "Upvoting comments is not allowed, sorry for your inconvenience"
    },

As you can see we use {sender} and {bid} as placeholders inside of our text. We will replace these placeholders via RegEx when we do a refund. For this open your index.js and scroll to your refund function and change the code inside as follows:

function refund(sender, amount, memoType) {
  var message = config.memo_messages[memoType];

  message = message.replace(/{sender}/g, sender);
  message = message.replace(/{bid}/g, amount);
    
  steem.broadcast.transfer(config.private_active_key, config.account_name, sender, amount, message, function (err, result) {
      if (err || !result) {
          console.log("Refund failed ! For: " + sender);
      }
    });
}

In detail:

  • We use the JavaScript function called .replace(searchvalue, newvalue) to replace searchvalue with the newvalue
  • And as searchvalue we use RegEx to define every /{sender}/g occurrences inside your String

Try it out

Screen Shot 2018-02-10 at 14.56.18.png

Full Source Code

Thank you for reading this mini tutorial serie ! The source code will be soon online on GitHub after some code cleanup !

Stay tuned for new bot tutorials coming !

Curriculum



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Thank you for the contribution. It has been approved.

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

Hey @creon, I just gave you a tip for your hard work on moderation. Upvote this comment to support the utopian moderators and increase your future rewards!

@moonrise, I always try to support who contribute to open source project, upvote you.

1UP-Kayrex_tiny.png

You've got upvoted by Utopian-1UP!

You can give up to ten 1UP's to Utopian posts every day after they are accepted by a Utopian moderator and before they are upvoted by the official @utopian-io account. Install the @steem-plus browser extension to use 1UP. By following the 1UP-trail using SteemAuto you support great Utopian authors and earn high curation rewards at the same time.


1UP is neither organized nor endorsed by Utopian.io!

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

Achievements

  • 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

Hey, great tutorial!

While I'm not a huge fan of the upvote bots, spreading them out, takes th power of the biggest bots down a peg, so kudos on sharing this.

I'll be sure and mention it in an upcoming article. Also, if you're interested, you can share your best tip and win some SBD. Just go here. Follow @dolphinschool for lots of great steemit training! @markrmorrisjr

Coin Marketplace

STEEM 0.18
TRX 0.15
JST 0.029
BTC 62170.06
ETH 2415.65
USDT 1.00
SBD 2.65