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.
In part 1 we built an auto-liking bot and in part 2 we created a curation trail bot. Please follow those tutorials first as we will build on knowledge learned.
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 đ
We will create a follow bot, our bot will automatically follow accounts that any of our target listed start following. If youâre looking for new accounts to follow and trust other accounts to only follow quality this can be a good way to find new content
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)
Learning Goals
- re-use knowledge learned about steem-js in parts 1 & 2
- look at the
custom_json
part of a Steem blockchain transaction - parse JSON into readable information
- look at broadcasting our own customJson over the Steem network to follow new accounts with the API
Step 1 - Setup đť
For this tutorial our bot will run in the web browser, a more effective way to use this bot would be on the command-line but I feel weâll still benefit from this minimal in browser approach for now.
Use this template to get started - This is a base file for today's project and includes code from the previous two tutorials. Youâll see the output in your browser console again like last time (refer to the previous post if you canât remember). Familiarise yourself with the functions included and notice they are the same as we have already used. You may notice I changed the name we had on tutorial 1 from isFriend
to isTarget
, itâs important in programming to name things as appropriately as possible even if this code is just for yourself, itâll help.
Fill in your account information as per usual. In this tutorial, weâll be working with a list of targets, whenever any of those accounts follow a new account we will too. Add as many names to TARGET_ACCOUNTS
as you would like, if possible make it an account you have access to so itâs easier to test itâs working.
// Tutorial 03 - curation trail bot
const ACCOUNT_NAME = ''
const ACCOUNT_KEY = ''
const TARGET_ACCOUNTS = ['sambillingham', 'cryptoctopus', 'kevinwong', 'wehmoen', 'cutemachine']
Step 2 - Check Connection đ
You should see streamTransactions function setup. Go ahead and add a console.log(txType, txType) to check everything is working as expected. See example below.
steem.api.streamTransactions('head', function(err, result) {
let txType = result.operations[0][0]
let txData = result.operations[0][1]
console.log(txType, txData)
});
This should feel familiar so lets jump into something new.
Step 3 - Access Custom JSON For Followers đž
There is no transaction type âfollowâ.
The fundamental parts of the Steem blockchain voting, commenting, rewards etc work regardless of what type of platform itâs running. While Steemit a blog style website was the first app to run on Steem it was never itâs sole purpose and we already see many types of apps. âFollowersâ although not unique to blogs is an idea that might not be necessary for all platforms. With that in mind followers are stored as âcustom_jsonâ (JSON - Javascript Object Notation - is a format for organising data in a file) where any app or platform might use slightly different âcustom_jsonâ.
For today weâre going to check for json as our transaction type.
if(txType == 'custom_json') {
console.log(txData)
}
Looking at the screenshot we can see the data we have to work with. The json is an Array (list of data ) where the first item is the type of data and the second item is the content
json : "["follow" {"follower":"dianadee","following":"dilkash","what":["blog"]}]"
to work with this in javascript we need to parse the json. PARSE? I hear you. json is stored as text and by default a computer would think you really wanted those brackets and quotes as part of a sentence. Parsing breaks the long chink of text into a group of readable information that we can pick and choose from.
let json = JSON.parse(txData.json)
let dataType = json[0]
let dataContent = json[1]
Store part one as the type and part two as the content, remember weâre trying to make our life easier by giving things helpful names.
that dataContent is further broke down into follower and following, you guessed it weâre going to check if the follower (that who just followed a new person) is someone weâre watching. The following is the person they just followed.
We can use the same function we made in part one to make this nice and simple. occasionally you might se a âreblogâ (also stored under custom_json) so we need to check that the datatype is a follow
if ( dataType == 'follow' && isTarget(dataContent.follower) ){
console.log(`${dataContent.follower} started following ${dataContent.following}`)
}
Now weâll start to see a list of who our targets started following, ask a friend if they can follow someone knew while youâre bot is running to test it out.
Step 3 - Broadcast A Follow To Steem đââď¸
In the last two parts we looked at sending votes, send json aka a follow request is very similar but it has a little more setup.
Letâs make a function for this action. Weâll have a single parameter called name, thatâs who we want to follow.
function followAccount(name){}
Weâre going to create an array and turn it into json just like what we saw when watching transactions coming in on the blockchain.
let info = ['follow', {
follower: ACCOUNT_NAME,
following: name,
what: ['blog']
}]
We have an array with the first item âfollowâ and the second item is our set of data. Our ACCOUNT NAME is going to become the follower and whoever we set as the parameter(input data) will be who we follow. Next turn it into json, javascript works hand in hand with json so there is a function built in for this.
let json = JSON.stringify(info);
Now letâs send that over the network
steem.broadcast.customJson( ACCOUNT_KEY, [], [ACCOUNT_NAME], 'follow', json, function(err, result) {
console.log(`${ACCOUNT_NAME} has just started following ${name}`)
console.log(err, result);
});
Input the required data into the customJson function and it will work itâs magic. Now we can use the function followAccount(âsambillinghamâ) and it would go-ahead and follow whoever name we use. Pretty ace once itâs working.
Step 4 Combine it all Together đ
The final step! Use our new function inside of our transaction stream like so.
if ( dataType == 'follow' && isTarget(dataContent.follower) ){
followAccount(dataContent.following)
}
đĽ Just like that it will start copying the follows of any of youâre target list.
Our third Bot is now finished and ready to copy followers. This bot runs in the browser so for it to work weâll have to keep the page open, weâll look at making background tasks for bots once weâve learned a few more tools
Hereâs the full code #3 đ¤
Thereâs still a lot more we can do with bots and the Steem API but 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
Posted on Utopian.io - Rewarding Open Source Contributors