TUTORIAL - Beginner - Build your own Steem Upvote Bot - JavaScript - Part 1
What Will I Learn?
In this first part of my tutorial series for Steem-JS I will prepare you how to create a simple upvote bot based on your received transactions.
The Goal of this Part is:
- Understanding the Steem-JS Library and setup a project
- Read your account information
- Read your transactions
Requirements
You should have a basic understanding of JavaScript.
Also:
- A TextEditor (e.g Atom.io)
- A working Terminal Console (e.g CMD for Windows / iTerm for Mac OS X)
- NodeJS Installed on your OS (Download Page)
- Also make sure NPM is installed correctly (See here)
- Some time and a working brain
Difficulty
- Basic
Tutorial Contents
Project Setup
Start off by creating a new folder in a directory of your choice. Navigate with your console to the created directory (cd - command). To work with the Steem Blockchain we need to install Steem-JS. As you probably already realised we are working with Node.js and its Node Package Manager(NPM), so to use it correctly run npm init
and simple hit enter to finish the creational process of the package.json.
What is this package.json ?
- it lists the packages that your project depends on.
- it allows you to specify the versions of a package that your project can use using semantic versioning rules.
- it makes your build reproducible, and therefore much easier to share with other developers.
After creating the package.json we are ready to install the Steem-JS package! Run npm install steem --save
inside of your console and we should be ready to go!
With --save we install the package ONLY in the current directory where we run the command
Now open your directory / project with atom! It should look like this:
Let's start the Coding !
Create an index.js
and write as your first code console.log('Hello Stemian!');
. Now head over to your terminal / console and run node index.js
. You should see following output:
But why ? Well Node.js runs your index.js and our code provided an output for the console!
But now head over to the real stuff:
Delete the line of code and write this:
Let's break it down; First we include the steem library in our index.js after that we define our node to https://rpc.buildteam.io
.
With setInterval(loop, 10000);
we call our function loop
every 10 seconds.
The function loop is really basic at the moment, we have an output at each call of the function and afterwards we get the user-account via the steem-js api. Please make sure to use your Steemit Username!
steem.api.getAccounts(['moonrise'], function (err, result) {});
getAccounts expects an array of usernames, so you can add more than one; But
account = result[0];
Only takes the first result ! So it's better to just get ONE account.
If we get an error from the steem api an output with the error is triggered.
If there is no error we will save the resulting account in our account
variable.
Afterwards we check if account
is really set and if so we output the account data.
Lets run it and see what happens! Head over to your console and run node index.js
. To end the programm press crtl and c
.
As you see we get a ton of informations about the account, you can use every existing steem username and get these informations.
So the last step of this tutorial is to get our transactions and check for valid an URL in the memo field !
Please run following command npm install valid-url
, to include the package in your project.
Update your code like this (Complete code is available at the bottom):
How we changed the code?
If an account exist we will now search for the last 10 transactions to the account with
steem.api.getAccountHistory(account.name, -1, 10, function (err, result) {});
-1 means starting by the last transaction, and 10 the amount of last transactions since then.
After that we loop through the found transactions.
var op = trans[1].op;
Gets the transaction data. trans[0] is the transaction number/id.
if (op[0] == 'transfer' && op[1].to == account.name)
Checks if the transaction was an transfer AND if the receiver is your account name.
If so we will check the memo field if it is a valid URL and output the whole transaction
if (validUrl.isUri(op[1].memo)) {
console.log(trans);
}
You can test it by transfering 0.01 SBD from your account to your account with a valid URL. Afterwards run the programm with node index.js
, your output will look like this:
Awesome ! You are done with my first part of my tutorials ! I hope you enjoyed it !
What's up next ?
In the next part we will create a config file, and upvote the post from the memo!
Thanks for reading !
Complete Code
https://gist.github.com/caspernikus/e7b730262412b1675f5855ba52eecaa4
Curriculum
Posted on Utopian.io - Rewarding Open Source Contributors
Hey @moonrise I am @utopian-io. I have just upvoted you!
Achievements
Suggestions
Get Noticed!
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
Thank you for the contribution. It has been approved.
You can contact us on Discord.
[utopian-moderator]
Cool Tutorial! I will follow you from now on and I hope I'll be as good as you when it comes to coding :)
Awesome tutorials! I am putting it all together, and when I try to run index.js through Nodejs I am getting:
SyntaxError: Unexpected token )
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:616:28)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Function.Module.runMain (module.js:693:10)
at startup (bootstrap_node.js:188:16)
at bootstrap_node.js:609:3
Do you know why I am getting this error? Thanks!