Bot Build #4 - Resteem Bot With Friend List

in #utopian-io7 years ago (edited)


What Will I Learn?

  • Build A Resteem Bot With A Friend List

Requirements

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

Difficulty

  • Intermediate

Tutorial Contents

In this tutorial, you will learn how to use steem-js to make a bot to resteem your friend's posts.

Curriculum

The Tutorial

Step 1 - setup the server

you can get a full explanation about setting up NodeJS server Here

Full Code

const http = require('http');
const steem = require('steem');
const hostIP = "localhost";
const port = 80;

const server = http.createServer((req, res) => {
  res.statusCode = 200;

  res.setHeader('Content-type', 'text-plain');

  res.end('Hello World! this is my first project at node.js!');

  });

  server.listen(port, hostIP, () => {

  console.log("Listen to port " + port);
});

this is really simple one, we get 2 variables for steem and http packages, steem for steem-js and http for the node.js server.

then 2 variables for IP and Port and we create the server with the IP and port and send status code, header (the type of page) and if you get into the IP (localhost:80) you will get this result

image.png

Step 2 - create the friend list

create new file friends.json

image.png

with this code

[
  "yairdd"
]

to add friends just change the file to this (example)

[
  "yairdd",
  "atukh09"
]

easy enough.

now get this list to the server script.

const friends = require("./friends.json");

to get the file from the main folder you need to use ./ and then the file name.

and check if it works

console.log(friends);

result

image.png

Step 3 - Create The Resteem Function

First, we need the function

function ResteemPost(username, wif){
  for(var i=0; i<friends.length; i++){
  steem.api.getDiscussionsByBlog({tag: friends[i], limit: 1}, function(err, result){
  const json = JSON.stringify(['reblog', {
  account: username,
  author: result[0].author,
  permlink: result[0].permlink
  }]);

  steem.broadcast.customJson(wif, [], [username], 'follow', json, (err, result) => {
  console.log(err, result);
  });
  });
  }
}

first, we need a function with the name ResteemPost(you can change it to whatever you want) and we need 4 variables.

user - your username (the user who make the resteem)

wif - the posting private key

author - the user who gets the resteem

permlink - the permalink of the post you resteem

now we make the json variable for the reblog (Resteem)

account - your username

author - the username of the author

permlink - the permalink of the post

then we broadcast customJson because there is no specific function for resteem/reblog.

wif - the private posting key.

user - the user who makes the resteem.

json - the json variable we made before.

Step 4 - Make The Resteem

(now we're going to use a guest user from the steem-js project)

you can use it if you want

const username = 'guest123';
const privPostingWif = '5JRaypasxMx1L97ZUX7YuC5Psb5EAbF821kkAGtBj7xCJFQcbLg';

now we need to use the function

ResteemPost(username, privPostingWif);

Now when you start the server it should resteem the posts

result

null { id: '889311544b429857577370316d28cbc00d29eb52',
  block_num: 21698585,
  trx_num: 27,
  expired: false,
  ref_block_num: 6151,
  ref_block_prefix: 767446380,
  expiration: '2018-04-19T08:24:06',
  operations: [ [ 'custom_json', [Object] ] ],
  extensions: [],
  signatures:
  [ '20573a97a13eafbb10adcbe1e013b98392275f84a1a1266aa81381ec3e408fc21e47d0b9d9c39b42b949088fcfcc2f60d5204602ea9bcf0b6501819eea66c666b4' ] }

image.png

steemit results

image.png

Step 5 - 60 Minutes Timer

to make it automatic you want to make a timer, for example for every hour.

at the end of the ResteemPost function use this code

  setTimeout(function(){ResteemPost(username, wif);}, 60*60*1000);

Full Function Code

function ResteemPost(username, wif){
  for(var i=0; i<friends.length; i++){
  steem.api.getDiscussionsByBlog({tag: friends[i], limit: 1}, function(err, result){
  const json = JSON.stringify(['reblog', {
  account: username,
  author: result[0].author,
  permlink: result[0].permlink
  }]);

  steem.broadcast.customJson(wif, [], [username], 'follow', json, (err, result) => {
  console.log(err, result);
  });
  });
  }
  setTimeout(function(){ResteemPost(username, wif);}, 30*60*1000);
}

setTimeout - timer function

inside we make function and send our ResteemPost function again so it's looping it every hour

60*60*1000 - 60 = minutes, 60 = seconds, 1000 = milliseconds.

120 = 2 hours
240 = 4 hours

you can change it for hours easily

2*60*60*1000 - the new 2 is the hours.

if you have done all correctly, you're done!

The Full Code

You can find the full server code in pastebin



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Thanks for the contribution.


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

[utopian-moderator]

Wonderful tutorial,
Wants more from you...

Hi
Thank you
For your writing
I've seen your writing come pretty much
I hope to write something better in the future.

Hey @lonelywolf! Thank you for the great work you've done!

We're already looking forward to your next contribution!

Fully Decentralized Rewards

We hope you will take the time to share your expertise and knowledge by rating contributions made by others on Utopian.io to help us reward the best contributions together.

Utopian Witness!

Vote for Utopian Witness! We are made of developers, system administrators, entrepreneurs, artists, content creators, thinkers. We embrace every nationality, mindset and belief.

Want to chat? Join us on Discord https://discord.me/utopian-io

What a nice tutorial. I was couldn't see that much detailed tutorial to make re-steem bot. Appreciate your efforts. Thanks!

Huhuhu, it really is not making sense to me. I will just appreciate bots and that's it.

Coin Marketplace

STEEM 0.23
TRX 0.25
JST 0.038
BTC 95317.76
ETH 3302.38
USDT 1.00
SBD 3.31