Bot Build #11 - Advanced Resteem By Friend List Bot

in #utopian-io6 years ago

Repository

e.g. https://github.com/steemit/steem-js

What Will I Learn?

  • You will learn how to create an advanced resteem by friend list bot.

Requirements

  • Node.JS, Here
  • Steem Package (Install: npm install steem --save)

Difficulty

  • Advanced

Tutorial Contents

You will learn how to create an advanced resteem by friend list bot with SteemJS.

Curriculum

The Tutorial

Step 1 - Setup The Functions

First Of All, we will set up the vote & resteem function, I'm not going to explain about them because it's already mentioned at my previous tutorials.

// Vote Post Function
function streamVote(author, permalink, weight) {
    steem.broadcast.vote(ACC_KEY, ACC_NAME, author, permalink, weight, function(err, result) {
        if(err) return console.log(err);
        console.log('Voted Succesfully, permalink: ' + permalink + ', author: ' + author + ', weight: ' + weight / 1000 + '%.');
    });
}

// Resteem Post Function
function streamResteem(ACC, permlink, author) {
    const json = JSON.stringify([
        'reblog',
        {
            account: ACC,
            author: author,
            permlink: permlink
        }
    ]);
    steem.broadcast.customJson(ACC_KEY, [], [ACC], 'follow', json, (err, result) => {
        if(!!err)
            console.log("Resteem Failed!", err);
        else
            console.log("Resteemed Succesfully, Friend Name: " + author);
    });
}

now we will set up the comment function

// Comment Function
function streamComment(author, permlink, permalink, body){

}

so, first of all, we need the permalink of the post, we need the author of the post and we need new permalink, this permalink will be the permalink of the comment and we need the body (the content).

the comment function is exactly the post function but we will use parent-author and parent-permlink to make the comment.

    steem.broadcast.comment(ACC_KEY, author, permlink, ACC_NAME,
    permalink, '', body,
    JSON.stringify({
    tags: ACC_NAME,
    app: 'steem-bot-advanced-resteem'
    }),
    function(err, result) {
        if(!!err)
            console.log("Comment Failed!", err);
        else
            console.log("Comment Posted Succesfully, Friend Name: " + author);
    });

first of all, we're using the Account Key (Private Posting WIF Key), the author name (who made the post), the permalink of the post, our account name(the commenter), the comment permalink and the body(content).

then if we have an error, we send a comment to the function with the error result and if there were no errors we will send a successful comment to the console.

and now the last function, we need makeid function, this function gets number and creates random name or id based of characters, for steem we need only id that based of abcdefghijklmnopqrstuvwxyz.

the function -

function makeid(number) {
  var text = "";
  var possible = "abcdefghijklmnopqrstuvwxyz";

  for (var i = 0; i < number; i++)
    text += possible.charAt(Math.floor(Math.random() * possible.length));

  return text;
}

at first, we take the number of characters that we will use.
we create 2 variables, text this variable will be our text that we will send back to the comment permalink and possible this variable will be our possible characters.

then we running a loop and end the loop after X times and every time we add a character randomly.

Step 2 - Setup JSON file for friend list & settings

this step is simple, you just need to create a file called settings.json or whatever name you want with an end of .json.

we will use a simple settings file

{
  "friends": {
    "list": [
      "headcorner"
    ]
  },
  "settings": {
        "VOTING_WEIGHT": 2000,
        "UPVOTE_ENABLE": true,
        "BODY": "Test Comment",
        "HOURS": 2
  }
}

first of all we have friends and inside we have list, in the list we can add the names
for example

"list":[
"lonelywolf",
"headcorner",
"techslut"
]

then we have the settings, inside we have the voting weight variable, the enable upvoting variable (check if we enabled voting when resteeming), the body (the content of the comment) and the hours (the number of hours between the rounds).

and now we get back to the script and set up the JSON file inside the script and we will set up the rest of the variables.
first, we will start with the json variables.

const file = require('./settings.json'),
    friends = file.friends,
    settings = file.settings

friends = file.friends, it means that we get access to everything inside friends, same for settings

now we need to set up the account variables

const ACC_NAME = 'guest123',
    ACC_KEY = '5JRaypasxMx1L97ZUX7YuC5Psb5EAbF821kkAGtBj7xCJFQcbLg';

as always, this user is the official guest user of the steem-js framework/package for javascript/node.js!

Step 3 - Setup The Bot

so one step before we're done, we need to set up the bot.

function startResteem(){
    friends.list.forEach(function(res){
        
        }
});

so we create a function called startResteem and inside we're making a loop that goes between all of the friends.

 steem.api.getDiscussionsByBlog({tag: res, limit: 1}, function(err, result){
}

to get the last post of the friend we're using the function getDiscussionsByBlog, and limit the number of posts to 1, and we'll get the last post of the friend(author).

console.log("Author: " + res + " Last Post Loaded, Starting The Process...");
console.log("Post Title: " + result[0].title);
streamResteem(ACC_NAME, result[0].permlink, result[0].author);
streamComment(result[0].author, result[0].permlink, makeid(12),settings.BODY);

if(settings.UPVOTE_ENABLE){
    streamVote(result[0].author, result[0].permlink, settings.VOTING_WEIGHT);
}

so first, we send a comment to the function so we can know that we starting the process.
then we resteem the post with the author name and the permalink of the post that we get from the result of the getDiscussionsByBlog function.
and we're commenting on the post with our body(content) that we already configured at the JSON file and we create new permalink with the makeid function.

then we check if the upvote function enabled if it is we send a vote.

each function sends a comment to the console so we don't need to send any comments to the console.

and to start the next round we create a timer at the end of the function

    setTimeout(function(){console.log("Next Round Start In " + settings.HOURS + " Hours...")}, 0.5*60*1000);
    setTimeout(function(){startResteem();}, settings.HOURS*60*60*1000);

the first timer send comment to alert when the next round starts with timer of 30 seconds (this timer is to be sure that the process done before we send the comment, if it's too fast you can take it longer).
0.5 = minutes, half minute = 30 seconds.

then we create another timer, at the function we start the function and set the timer to the X hours we configured at the json file.

full code:

function startResteem(){
    friends.list.forEach(function(res){
          steem.api.getDiscussionsByBlog({tag: res, limit: 1}, function(err, result){
                console.log("Author: " + res + " Last Post Loaded, Starting The Process...");
                console.log("Post Title: " + result[0].title);
                streamResteem(ACC_NAME, result[0].permlink, result[0].author);
                streamComment(result[0].author, result[0].permlink, makeid(12),settings.BODY);

                if(settings.UPVOTE_ENABLE){
                    streamVote(result[0].author, result[0].permlink, settings.VOTING_WEIGHT);
                }
            });
    });
    setTimeout(function(){console.log("Next Round Start In " + settings.HOURS + " Hours...")}, 0.5*60*1000);
    setTimeout(function(){startResteem();}, settings.HOURS*60*60*1000);
}

Step 4 - Last Step, Start The Bot!

now when we're done we can start the script!

console.log("Welcome To The Advanced Friend List Resteem Bot!");
console.log("The Script Is Running Now...");
startResteem();

and we're done!

results:

Welcome To The Advanced Friend List Resteem Bot!
The Script Is Running Now...
Author: valikos Last Post Loaded, Starting The Process...
Post Title: A random day from my Life(Love my Job Bartender)
Voted Successfully, permalink: a-random-day-from-my-life-love-my-job-bartender, author: valikos, weight: 2%.
Comment Posted Successfully, Friend Name: valikos
Resteemed Successfully, Friend Name: valikos
Next Round Start In 2 Hours...
 

steemd:


Note: At The Script You Have Two Extra Functions, This Functions Can Add Friend To The List And Change Settings Value At The File!
Please! for the next tutorial I will be really happy if you can give me a suggestion for the next script/tutorial!

have a great day!

You can check the full work at my repl.it account and other bots that I create!


Proof of Work Done

the work made in Repl.it, https://repl.it/@lonelywolf22/Steem-Bots-Advanced-Resteem-by-Friend-List-v10-Done
user: https://repl.it/@lonelywolf22

GitHub: https://github.com/lonelywolf1
Utopian Tutorials GitHub Repository: https://github.com/lonelywolf1/Bot-Projects-SteemJS (This tutorial will add to the GitHub repository in the next few days!)

Sort:  

Did you know that there is a livestreaming service built on top of the Steem network called DLive? DLive enables anyone to stream what they enjoy and earn rewards in the form of Steem, just like on Steemit.com. Come check out some streams and see how easy it is to join in the conversation!

Want to promote your posts?

Send at least 0.1 STEEM or SBD to @dlivepromoter with the post link as the memo and receive an upvote on that post!

Delegate Steem Power to @dlivepromoter for a daily payout!

25 SP, 50 SP, 100 SP, 250 SP, 500 SP, custom amount
We pay 95% of bids back to the delegators each day. The remaining 5% is contributed towards @dlivecommunity to help create a larger community.

Disclaimer:

@dlivepromoter is a part of the community project @dlivecommunity. We aim to help streamers navigate their way through @dlive and the overall Steem ecosystem. We are not affiliated with @dlive.

Excellent! Thanks for that man! I’m not skilled programming but I’m going to try it!
Do you think I can discriminate the posts I want to resteem by the posts that I don’t want to resteem by using a #tag ?

Thank you for your contribution.
While I liked the content of your contribution, I would still like to extend few advices for your upcoming contributions:

  • Structure of the tutorial: Improve the structure of the tutorial.
  • Text format: Don't use uppercase and lowercase letters in the middle of the text. Always start a paragraph with capital letters.

Looking forward to your upcoming tutorials.

Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.


Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]

Hello, my name is @guest123, I'm Here To Give You A Look At The New Script Of @lonelywolf! you can check out the tutorial here: Here.
If you want to take a look at the code checkout the repl(script) at repl.it: Here.


Hello, my name is @guest123, I'm Here To Give You A Look At The New Script Of @lonelywolf! you can check out the tutorial here: Here.
If you want to take a look at the code checkout the repl(script) at repl.it: Here.


Hey @lonelywolf
Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Contributing on Utopian
Learn how to contribute on our website or by watching this tutorial on Youtube.

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!


Hello LonelyWolF, Your Stats:
1957 SE Points = 1.957(SBD)$
158 SSE Points = 0.134(STEEM)$
21789 Points = 251 Votes / 502 Followers At The Exchange.

Have A Great Day!

Coin Marketplace

STEEM 0.17
TRX 0.15
JST 0.028
BTC 59989.12
ETH 2380.65
USDT 1.00
SBD 2.49