TUTORIAL - Beginner - Build your own Steem Upvote Bot - JavaScript - Part 8
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
Requirements
- Read my previous part(Click here)
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
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
- Part 1 - The Setup
- Part 2 - The next Step
- Part 3 - Refund
- Part 4 - Post Age
- Part 5 - More refunding
- Part 6 - Save data locally
- Part 7 - Automatically commenting
Posted on Utopian.io - Rewarding Open Source Contributors
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!
I upvoted you....upvote me
https://steemit.com/download/@arjunrai/see-you-again
@moonrise, I always try to support who contribute to open source project, upvote you.
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
Community-Driven Witness!
I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!
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