TUTORIAL - Beginner - Build your own Steem Upvote Bot - JavaScript - Part 7
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:
Requirements
- Please read my previous part(Click here)
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 ourprivate_posting_key
andjsonMetadata
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
Full Source Code
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
Posted on Utopian.io - Rewarding Open Source Contributors
@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
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