This tutorial is part of a series on âHow To Build Botâs Using Steem-JS Open Source Libraryâ it aims to demonstrate practical applications for bots and demystify the code used to create them.
- part 1- auto-liking bot
- part 2 - Curation bot
- part 3 - Auto follow bot
- part 4 - Paid upvote bot
- part 5 - @ Mention Notifier bot
- part 6 - Auto Reward Claimer bot
The completed bot code is here - I know some people find it easier to see everything in one block and follow along that way. ⏠ď¸
Outline đ
In this tutorial, weâre looking at how to make Steem JS bot that run tasks at certain times of the day. Scheduling bot tasks is particularly useful for repetitive tasks such as claiming rewards, batch voting after power recharges or diversifying tokens. Almost all popular bots on the Steem network will be running in the background within continuously or scheduled to active at certain times.
Requirements
- A plain text editor (I use atom.io )
- Your private posting key - This is found in your wallet on the permissions tab on steemit.com, click âshow private keyâ (remember to keep this safe). This is what allows the bot to vote for you
Difficulty
This tutorial is intended to be beginner friendly. (I appreciate feedback on the teaching level).
Weâre looking at the command-line, if youâve installed node and used the command line before it may be a touch too easy for you.
Learning Goals
- familiarise yourself with previous Bots weâve created in this tutorial series
- Run a bot in the background with Node.js
- Schedule bot task for the future
Step 1 - Setup đť
In the previous six tutorials, our bots have been running in the browser. Browser bots are a great way to learn but the time has arrived for us to look at using the command-line to stop and start our bots. The command line allows our bot to run in the background on our computer or a remote server. Weâre going to use Node.js. Node.js is a platform that allows you to run Javascript (typically a browser-based language) on its own outside of the browser.
Download and install Node.js if you donât already have it installed. The download page offers installers for major operating systems. Once installed we use Node.js from the command-line. If youâve never used the command-line it can seem a little daunting but it shouldnât. the command line is just like using a computer with an app/program except you have to learn the individual commands and type them manually (appose to clicking buttons in an app that do things)
On Mac youâll open the Terminal app and on Windows youâll open the Command Prompt program.
You can test Node is install and running correctly by typing node -v
which will output the version you have installed. If you see the output you have node installed and we can continue.
Step 2 - Run A Bot With Node.js đ¤
Download the starter code here this is the code fom tut #6
Weâll start with the Bot code we made in tutorial #6. The bot we made was a HTML file that ran in the browser so the first step is to remove all the html, leaving us with just the javascript. Save the file under a new name bot.js
notice the .js extension for a javascript file.
// Tutorial 07 - Schedule Steem-Js Tasks
const ACCOUNT_NAME = ''
const ACCOUNT_KEY = ''
const INTERVAL = 60 * 60 * 1000
steem.api.setOptions({ url: 'https://api.steemit.com' });
setInterval(claimBalance, INTERVAL)
function claimBalance(){
steem.api.getAccounts([ACCOUNT_NAME], function(err, result){
console.log(err, result)
let sbdReward = result[0].reward_sbd_balance
let steemReward = result[0].reward_steem_balance
let steemPowerInVests = result[0].reward_vesting_balance
steem.broadcast.claimRewardBalance(ACCOUNT_KEY, ACCOUNT_NAME, steemReward, sbdReward, steemPowerInVests, function(err, result) {
console.log(err, result);
});
})
}
Youâre code should look like above, make sure to fill in the information for NAME and KEY.
In our previous browser bots, we linked to a remotely hosted version of the Seem-js library. In this bot weâll want to include it as a package. Node has a package manager built in that lets you download and use external code super easily.
Open Terminal or Command Prompt again.
The cd
command allows you to change directory, we want to change to the directory where we saved our bot.js file. For example I save mine in the documents folder then projects folders then Steem folder. On macOS ~/
represents the users home directory.
cd ~/Documents/Projects/Steem
One weâre in the correct folder we can download the external code by using the command npm install name-of-code
but first we need to initialise a project with npm init
this registers the name and author along with other settings for our project.
Run the command and follow the instructions.
npm init
With our project initialised install Steem through the package manager.
npm install steem
Youâll now have a new file package.json
that contains the project information and a new folder node_modules
that contains any third party code we install.
We need to add a line at the top of our bot.js file to include the steem.js library.
const steem = require('steemâ);
Run our bot using Node.js with the node
command and the file we want to run. Try it out!
node bot.js
If you filled in your account name you should see the output of the steem.api.getAccounts()
function.
Step 3 - Using Forever
Aside from not needing the browser window open, weâve not done much different, infant now weâre just left with the terminal open instead.
Forever is a node package that lets you run Node.js code in the background âforeverâ, if for any reason you code breaks it will restart it. It also means we can close the terminal and leave it running, typically you would use this in a server but it will work on a local computer too.
Install forever through npm. The -g
flag stands for global and allows itâs use anywhere on our system (as apposed to just this project weâre working on)
npm install forever -g
Now run the bot with forever.
forever start bot.js
Just like that the bot running in the background!
Step 4 - Scheduling Bot Tasks
Currently, our bot code runs continuously at the set interval, useful but other types of projects may want a more specific schedule.
Node Schedule is a package is a package set up for time-based scheduled aka running a task every day at 10 am or perhaps every Wednesday at 6pm. depending on what type of bot youâre making this can be super useful.
download & Install
npm install node-schedule
include in the bot code
const schedule = require('node-schedule')
To schedule the bot to run every day at 6 pm we would use this. The second number represents the hour of the day.
schedule.scheduleJob('* 18 * * *', function(){
claimBalance()
});
Node schedule uses the CRON job formatting. See more details here
To test itâs working try scheduling a task a couple minutes into the future and watching what happens with node bot.js
.
Now weâve looked at scheduling and background tasks it starts to open up the opportunities to make more creative applications with bots.
I hope you find this interesting and useful. If youâre looking into building bots let me know or ask any questions below âď¸
Other Posts in This Series
- Part 1 - Beginner friendly - Build your first Steem bot in Javascript - 30minutes
- Part 2 - Beginner friendly - Building bots With steem-js #2
- PArt 3 - Copy Follow Bot - Building Bots With steem-js #3
- Part 4 - Paid Voting Bot - Building Bots With steem-js #4
- Part 5 - Mention Notifier Bot - Building Bots With steem-js #5
- Part 6 - đ¤ TUTORIAL - Auto Reward Claimer - Building bots With steem-js #6
Posted on Utopian.io - Rewarding Open Source Contributors