Steemit Post Comment Using Nodejs Bot Tutorial

in steemit •  6 years ago  (edited)

In this tutorial, you will learn to use steemjs to post a comment to steemit.com.

Prerequisites

Basic knowledge in nodejs.

The data is fetched as JSON from URL and then it is sent to the steemit. We will be using the following dependencies

  • Steem
  • HTTP

You can install these by the following command

  • npm install steem --save
  • npm install http --save
  • First, we need to create two variable for steem and http

    var steem = require('steem');
    var http = require('http');

    Next, We will create a server and listen to a port. You can use any other port.

    http.createServer(function (req, res) {
    }).listen(2001); 

    The next part is to fetch the data that we want to post. You need to add your username, posting key from steemit(Wallet -> Permission->Show Private Key)

    http.get('http://yourwebsite.com/api/steemit.json',function(res){
    var body = '';
    res.on('data',function(chunk){
    body += chunk;
    });
    res.on('end',function(){
    var post = JSON.parse(body);
    steem.broadcast.comment(
    'POSTING_PRIVATE_KEY', // your steemit posting private key
    '',
    post.category,
    'YOUR_STEEMIT_USERNAME', // your username
    post.slug, // Your Steemit post slug
    post.title,
    post.message,
    { tags: [ post.tags ] },
    function(err, result) {
    console.log(err, result);
    $('#message').text(err);
    $('#ms').text(result.id);
    });
    })
    }); 

    Example of JSON file that we fetch from URL

    {
    title: "Your Post Title",
    message: "Your message contents goes here. You can add image url too.",
    tag: ""general" ",
    category: "programming",
    slug: "your-post-title"
    }
    

    Full Source Code

    After uploading the file to your server you need to run node file.js or forever start file.js

    var steem = require('steem');
    var http = require('http'); 
    

    http.createServer(function (req, res) {

    http.get('http://yourwebsite.com/api/steemit.json',function(res){
    var body = '';

    res.on('data',function(chunk){
    body += chunk;
    });

    res.on('end',function(){
    var post = JSON.parse(body);
    steem.broadcast.comment(
    'POSTING_PRIVATE_KEY', // your steemit posting private key
    '',
    post.category,
    'YOUR_STEEMIT_USERNAME', // your username
    post.slug, // Your Steemit post slug
    post.title,
    post.message,
    { tags: [ post.tags ] },
    function(err, result) {
    console.log(err, result);
    $('#message').text(err);
    $('#ms').text(result.id);
    });

    })

    });
    res.end('Processing\n');

    }).listen(2001);

    Authors get paid when people like you upvote their post.
    If you enjoyed what you read here, create your account today and start earning FREE STEEM!
    Sort Order:  

    Congratulations @pjijin! You received a personal award!

    1 Year on Steemit

    Click here to view your Board

    Support SteemitBoard's project! Vote for its witness and get one more award!

    Congratulations @pjijin! You received a personal award!

    Happy Birthday! - You are on the Steem blockchain for 2 years!

    You can view your badges on your Steem Board and compare to others on the Steem Ranking

    Vote for @Steemitboard as a witness to get one more award and increased upvotes!