create account

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

View this thread on: hive.blogpeakd.comecency.com
· @moonrise · (edited)
$85.30
TUTORIAL - Beginner - Build your own Steem Upvote Bot - JavaScript - Part 7
![wallpaper_part7.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1518181210/v2orbly0kqveiukxrps0.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](https://res.cloudinary.com/hpiynhbhq/image/upload/v1518181363/y61sxzvg5ctrvcvpx5le.png)

#### Requirements
- Please read my previous part([Click here](https://utopian.io/utopian-io/@moonrise/tutorial-beginner-build-your-own-steem-upvote-bot-javascript-part-6))

#### 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:
``` javascript
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
``` javascript
    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:
``` javascript
    "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
``` javascript
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](https://res.cloudinary.com/hpiynhbhq/image/upload/v1518185058/fkj0dsxlvebc16ms1sxw.png)

### [Full Source Code](https://gist.github.com/caspernikus/a33fd21e78a6acfceeb462428ccf1745)

#### Curriculum
- [Part 1 - The Setup](https://utopian.io/utopian-io/@moonrise/tutorial-beginner-build-your-own-steem-upvote-bot-javascript-part-1)
- [Part 2 - The next Step](https://utopian.io/utopian-io/@moonrise/tutorial-beginner-build-your-own-steem-upvote-bot-javascript-part-2)
- [Part 3 - Refund](https://utopian.io/utopian-io/@moonrise/tutorial-beginner-build-your-own-steem-upvote-bot-javascript-part-3)
- [Part 4 - Post Age](https://utopian.io/utopian-io/@moonrise/tutorial-basic-build-your-own-steem-upvote-bot-javascript-part-4)
- [Part 5 - More refunding](https://utopian.io/utopian-io/@moonrise/tutorial-beginner-build-your-own-steem-upvote-bot-javascript-part-5)
- [Part 6 - Save data locally](https://utopian.io/utopian-io/@moonrise/tutorial-beginner-build-your-own-steem-upvote-bot-javascript-part-6)
    

<br /><hr/><em>Posted on <a href="https://utopian.io/utopian-io/@moonrise/tutorial-beginner-build-your-own-steem-upvote-bot-javascript-part-7">Utopian.io -  Rewarding Open Source Contributors</a></em><hr/>
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 40 others
properties (23)
authormoonrise
permlinktutorial-beginner-build-your-own-steem-upvote-bot-javascript-part-7
categoryutopian-io
json_metadata{"community":"utopian","app":"utopian/1.0.0","format":"markdown","repository":{"id":63857882,"name":"steem-js","full_name":"steemit/steem-js","html_url":"https://github.com/steemit/steem-js","fork":false,"owner":{"login":"steemit"}},"pullRequests":[],"platform":"github","type":"tutorials","tags":["utopian-io","steemit","steemjs","development","tutorial"],"users":["moonrise"],"links":["https://res.cloudinary.com/hpiynhbhq/image/upload/v1518181210/v2orbly0kqveiukxrps0.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1518181363/y61sxzvg5ctrvcvpx5le.png","https://utopian.io/utopian-io/@moonrise/tutorial-beginner-build-your-own-steem-upvote-bot-javascript-part-6","https://res.cloudinary.com/hpiynhbhq/image/upload/v1518185058/fkj0dsxlvebc16ms1sxw.png","https://gist.github.com/caspernikus/a33fd21e78a6acfceeb462428ccf1745","https://utopian.io/utopian-io/@moonrise/tutorial-beginner-build-your-own-steem-upvote-bot-javascript-part-1","https://utopian.io/utopian-io/@moonrise/tutorial-beginner-build-your-own-steem-upvote-bot-javascript-part-2","https://utopian.io/utopian-io/@moonrise/tutorial-beginner-build-your-own-steem-upvote-bot-javascript-part-3","https://utopian.io/utopian-io/@moonrise/tutorial-basic-build-your-own-steem-upvote-bot-javascript-part-4","https://utopian.io/utopian-io/@moonrise/tutorial-beginner-build-your-own-steem-upvote-bot-javascript-part-5"],"image":["https://res.cloudinary.com/hpiynhbhq/image/upload/v1518181210/v2orbly0kqveiukxrps0.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1518181363/y61sxzvg5ctrvcvpx5le.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1518185058/fkj0dsxlvebc16ms1sxw.png"],"moderator":{"account":"jestemkioskiem","time":"2018-02-09T14:15:58.444Z","reviewed":true,"pending":false,"flagged":false}}
created2018-02-09 14:10:57
last_update2018-02-09 14:15:57
depth0
children3
last_payout2018-02-16 14:10:57
cashout_time1969-12-31 23:59:59
total_payout_value59.500 HBD
curator_payout_value25.796 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length5,180
author_reputation10,047,335,281,671
root_title"TUTORIAL - Beginner - Build your own Steem Upvote Bot - JavaScript - Part 7"
beneficiaries
0.
accountutopian.pay
weight2,500
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id36,178,382
net_rshares14,634,399,234,882
author_curate_reward""
vote details (104)
@jestemkioskiem ·
Thank you for the contribution. It has been approved.

You can contact us on [Discord](https://discord.gg/uTyJkNm).
**[[utopian-moderator]](https://utopian.io/moderators)**
properties (22)
authorjestemkioskiem
permlinkre-moonrise-tutorial-beginner-build-your-own-steem-upvote-bot-javascript-part-7-20180209t141610502z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"community":"utopian","app":"utopian/1.0.0"}
created2018-02-09 14:16:12
last_update2018-02-09 14:16:12
depth1
children0
last_payout2018-02-16 14:16:12
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length172
author_reputation41,292,066,961,817
root_title"TUTORIAL - Beginner - Build your own Steem Upvote Bot - JavaScript - Part 7"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id36,179,489
net_rshares0
@steemitstats ·
@moonrise, I like your contribution to open source project, so I upvote to support you.
properties (22)
authorsteemitstats
permlink20180209t141512860z-post
categoryutopian-io
json_metadata{"tags":["utopian-io"]}
created2018-02-09 14:15:15
last_update2018-02-09 14:15:15
depth1
children0
last_payout2018-02-16 14:15:15
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length87
author_reputation351,882,871,185
root_title"TUTORIAL - Beginner - Build your own Steem Upvote Bot - JavaScript - Part 7"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id36,179,289
net_rshares0
@utopian-io ·
### 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. <a href="https://discord.gg/zTrEMqB">Participate on Discord</a>. Lets GROW TOGETHER!
- <a href="https://v2.steemconnect.com/sign/account-witness-vote?witness=utopian-io&approve=1">Vote for my Witness With SteemConnect</a>
- <a href="https://v2.steemconnect.com/sign/account-witness-proxy?proxy=utopian-io&approve=1">Proxy vote to Utopian Witness with SteemConnect</a>
- Or vote/proxy on <a href="https://steemit.com/~witnesses">Steemit Witnesses</a>

[![mooncryption-utopian-witness-gif](https://steemitimages.com/DQmYPUuQRptAqNBCQRwQjKWAqWU3zJkL3RXVUtEKVury8up/mooncryption-s-utopian-io-witness-gif.gif)](https://steemit.com/~witnesses)

**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**
properties (22)
authorutopian-io
permlinkre-moonrise-tutorial-beginner-build-your-own-steem-upvote-bot-javascript-part-7-20180211t082933484z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"community":"utopian","app":"utopian/1.0.0"}
created2018-02-11 08:29:33
last_update2018-02-11 08:29:33
depth1
children0
last_payout2018-02-18 08:29:33
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length1,141
author_reputation152,955,367,999,756
root_title"TUTORIAL - Beginner - Build your own Steem Upvote Bot - JavaScript - Part 7"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id36,617,189
net_rshares0