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
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);
Congratulations @pjijin! You received a personal award!
Click here to view your Board
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Congratulations @pjijin! You received a personal award!
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!
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit