create account

Tutorial: Create a Bot that sends you an Email on received Votes and Comments (nodeJS, steem-js, nodemailer) by schererf

View this thread on: hive.blogpeakd.comecency.com
· @schererf ·
$27.27
Tutorial: Create a Bot that sends you an Email on received Votes and Comments (nodeJS, steem-js, nodemailer)
Today I want to present you a little tutorial about writing a bot based on "steem-js". 
# The Mission
The requirements for my bot are somehow simple:
1. Listen the blockchain for new votes and comments on my posts
2. Send me an email if "1." occured

# System Requirements
For developing and running the bot some tools are needed on your system. I will show you a brief overview of the tools I have used for my bot.

### Code Editor: Visual Studio Code
To write the source code of your bot, you need an editor. What editor you use doesn't matter, but if you choose one that has some features like "syntax highlighting" you can make your self's life easier.

Visual Studio Code is free, Open Source and runs on every platform. You can download it here: https://code.visualstudio.com/

### nodeJS
To run your steem-js bot you need a runtime system. This can be a browser window or nodeJS. In this tutorial I choose nodeJS.
You can download nodeJS here: https://nodejs.org

Please choose the LTS version matching your operating system. 
At the time of writing this tutorial version 6.11.4 LTS was the latest.

# Write the bot
Choose a folder on your filesystem where the source-code file of your new bot should be saved. 

### npm steem-js
To use the steem JS API it is recommended that you install the steem-js package. Using node this is very easy:
1. Open the command line console
2. Change to the folder of your bot
3. Install steem-js package

```javascript
npm install steem --save
```

### npm nodemailer
For sending mails you need a package that offers this feature for JS. I choose the nodemailer package which you always have to install:
1. Open the command line console
2. Change to the folder of your bot
3. Install nodemailer package

```javascript
npm install nodemailer --save
```

### Create "steemMailerBot.js"
Add a new file "steemMailerBot.js" to your folder and open it with Visual Studio Code.

Here ist the complete source-code of the bot. Please copy it to the "steemMailerBot.js".

```javascript
/*
** steemMailerBot v0.0.1, by @schererf
** Feel free to use and modify.
*/

// REQUIREMENTS: 
// nodeJS: https://nodejs.org
// steem-js: npm install steem --save
// nodemailer-js: npm install nodemailer --save
var steem = require('steem');
var nodemailer = require('nodemailer');

// INITIALIZE
const steemUsername = "todoSteemitUsername";
const mailAccountUser="todoEMailAddress@gmail.com";
let transporter = nodemailer.createTransport({
	pool: true,
	host: 'smtp.gmail.com',
	port: 465,
	secure: true,
	auth: {
		user: mailAccountUser,
		pass: 'todoEMailPassword'
	}
});

// FUNCTIONS
function sendMail(subject, body){
	let mailOptions = {
		from: "steemMailerBot@gmail.com",
		to: mailAccountUser,
		subject: subject,
		text: body
	   };

	// send mail with defined transport object
	transporter.sendMail(mailOptions, (error, info) => {
		if (error) {
			return console.log(error);
		}
		console.log("Message sent to " + mailOptions.to + " (" + info.messageId + ")");
	});
}

function listenForVoteOrCommentAndSendMail(pAuthor){
	steem.api.streamOperations((err, operations) => {
		if (err) {
		  throw(new Error('Something went wrong with streamOperations method of Steem-js'));
		  console.log(err);
		}
		  
		const opType = operations[0];
		const op = operations[1];	
		if (opType==="comment" && op.parent_author === pAuthor){
			// comment received
			var subject = "Comment received for @" + op.parent_author + "/" + op.permlink;
			console.log (subject);
			sendMail(subject,
				"Author: " + op.author + "\nComment:\n" + op.body + "\n" + JSON.stringify(op)
			);
		} 
		if (opType==="vote" && op.author === pAuthor){
			// vote received
			var subject = "Vote received for @" + op.author + "/" + op.permlink;
			console.log (subject);
			sendMail(subject,
				"Voter: " + op.voter + "\nWeight:\n" + op.weight + "\n" + JSON.stringify(op)
			);
		}
	});
}

// MAIN: start the bot
console.log("Listening for new votes and comments for posts of @" + steemUsername)
listenForVoteOrCommentAndSendMail(steemUsername);
```

###  Example Visual Studio Code
![steemMailerBot VisualStudioCode.jpg](https://steemitimages.com/DQmXana6f6tk7X4qLMnTN8VCZwusXQks63rjrJtUyhEdMMf/steemMailerBot%20VisualStudioCode.jpg)

### Replace placeholder values
No you have to replace the placeholders in the source-code marked with "todo".

Placeholder | Usage
------------ | -------------
todoSteemitUsername | Your steemit username (without the @)
todoEMailAddress@gmail.com | Your Email-Address
todoEMailPassword | The password of your Email-Account

### Email account settings
If you don't use an gmail account, you also have to change the host of "nodemailer.createTransport".

If you use an gmail account, you maybe have to turn of the "Allow less secure apps" switch at your gmail account:
https://myaccount.google.com/lesssecureapps

# Run the bot
To run the bot using node you have to do the following steps:
1. Open the command line console
2. Change to the folder where your bot is present
3. Start the bot using nodeJS

```javascript
node steemMailerBot.js
```

### Example nodeJS
![steemMailerBot.jpg](https://steemitimages.com/DQmVNY4Qeqpe1ShqBcaygQ1P4SBiGyWv39YgyT5U2KnEsi3/steemMailerBot.jpg)

### Example Email
![steemMailerBot mailexample.jpg](https://steemitimages.com/DQmektxv2jB4B9ZcUKudnrdPU3EXrYJx9CWYBY6Gf9y5EAy/steemMailerBot%20mailexample.jpg)

Enjoy your new bot!

Thanks for reading and watching. Do you like this post?
Please leave some comments and follow me for my next post on steemit.
@schererf
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
properties (23)
authorschererf
permlinktutorial-create-a-bot-that-sends-you-an-email-on-received-votes-and-comments-nodejs-steem-js-nodemailer
categorysteemdev
json_metadata{"tags":["steemdev","programming","bot","nodejs","steemjs"],"users":["schererf"],"image":["https://steemitimages.com/DQmXana6f6tk7X4qLMnTN8VCZwusXQks63rjrJtUyhEdMMf/steemMailerBot%20VisualStudioCode.jpg","https://steemitimages.com/DQmVNY4Qeqpe1ShqBcaygQ1P4SBiGyWv39YgyT5U2KnEsi3/steemMailerBot.jpg","https://steemitimages.com/DQmektxv2jB4B9ZcUKudnrdPU3EXrYJx9CWYBY6Gf9y5EAy/steemMailerBot%20mailexample.jpg"],"links":["https://code.visualstudio.com/","https://nodejs.org","https://myaccount.google.com/lesssecureapps"],"app":"steemit/0.1","format":"markdown"}
created2017-10-08 12:03:54
last_update2017-10-08 12:03:54
depth0
children12
last_payout2017-10-15 12:03:54
cashout_time1969-12-31 23:59:59
total_payout_value21.905 HBD
curator_payout_value5.366 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length5,528
author_reputation26,658,463,264,121
root_title"Tutorial: Create a Bot that sends you an Email on received Votes and Comments (nodeJS, steem-js, nodemailer)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id17,097,103
net_rshares11,042,564,063,027
author_curate_reward""
vote details (49)
@ace108 ·
$0.07
Interesting bot. looks like should start figuring out nodejs. Thanks for the information.
👍  , ,
properties (23)
authorace108
permlinkre-schererf-tutorial-create-a-bot-that-sends-you-an-email-on-received-votes-and-comments-nodejs-steem-js-nodemailer-20171008t144821504z
categorysteemdev
json_metadata{"tags":["steemdev"],"app":"steemit/0.1"}
created2017-10-08 14:48:48
last_update2017-10-08 14:48:48
depth1
children1
last_payout2017-10-15 14:48:48
cashout_time1969-12-31 23:59:59
total_payout_value0.054 HBD
curator_payout_value0.012 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length89
author_reputation1,221,074,090,656,572
root_title"Tutorial: Create a Bot that sends you an Email on received Votes and Comments (nodeJS, steem-js, nodemailer)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id17,109,927
net_rshares27,713,404,667
author_curate_reward""
vote details (3)
@schererf ·
$0.06
Yes do it if you have the time ;-)
I'm still not a pro with nodejs but if you questions just aks me...
👍  ,
properties (23)
authorschererf
permlinkre-ace108-re-schererf-tutorial-create-a-bot-that-sends-you-an-email-on-received-votes-and-comments-nodejs-steem-js-nodemailer-20171009t084518003z
categorysteemdev
json_metadata{"tags":["steemdev"],"app":"steemit/0.1"}
created2017-10-09 08:45:15
last_update2017-10-09 08:45:15
depth2
children0
last_payout2017-10-16 08:45:15
cashout_time1969-12-31 23:59:59
total_payout_value0.047 HBD
curator_payout_value0.015 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length102
author_reputation26,658,463,264,121
root_title"Tutorial: Create a Bot that sends you an Email on received Votes and Comments (nodeJS, steem-js, nodemailer)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id17,179,818
net_rshares25,763,032,264
author_curate_reward""
vote details (2)
@blazing ·
$0.03
Now that's really genius of a work !!
👍  ,
properties (23)
authorblazing
permlinkre-schererf-tutorial-create-a-bot-that-sends-you-an-email-on-received-votes-and-comments-nodejs-steem-js-nodemailer-20171008t171840734z
categorysteemdev
json_metadata{"tags":["steemdev"],"app":"steemit/0.1"}
created2017-10-08 17:18:45
last_update2017-10-08 17:18:45
depth1
children0
last_payout2017-10-15 17:18:45
cashout_time1969-12-31 23:59:59
total_payout_value0.024 HBD
curator_payout_value0.007 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length37
author_reputation117,662,220,860,076
root_title"Tutorial: Create a Bot that sends you an Email on received Votes and Comments (nodeJS, steem-js, nodemailer)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id17,122,647
net_rshares13,365,730,264
author_curate_reward""
vote details (2)
@boomerang ·
$0.03
This post has received a 29.41 % upvote from @boomerang thanks to: @schererf<br><br>@boomerang distributes 100% of the SBD and up to 80% of the Curation Rewards to STEEM POWER Delegators. If you want to bid for votes or want to delegate SP please read <a href="https://steemit.com/steemit/@boomerang/boomerang-a-new-steemit-bid-bot-for-all-steemians-whitepaper">the @boomerang whitepaper</a>.
👍  ,
properties (23)
authorboomerang
permlinkre-tutorial-create-a-bot-that-sends-you-an-email-on-received-votes-and-comments-nodejs-steem-js-nodemailer-20171014t124339
categorysteemdev
json_metadata"{"app": "pysteem/0.5.2"}"
created2017-10-14 12:43:39
last_update2017-10-14 12:43:39
depth1
children0
last_payout2017-10-21 12:43:39
cashout_time1969-12-31 23:59:59
total_payout_value0.023 HBD
curator_payout_value0.007 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length392
author_reputation1,273,205,827,891
root_title"Tutorial: Create a Bot that sends you an Email on received Votes and Comments (nodeJS, steem-js, nodemailer)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id17,664,297
net_rshares12,753,148,734
author_curate_reward""
vote details (2)
@fode ·
$0.02
Great tutorial. We need more like this one. Building apps on the Steemit Blockchain is not a trivial thing lol
👍  ,
properties (23)
authorfode
permlinkre-schererf-tutorial-create-a-bot-that-sends-you-an-email-on-received-votes-and-comments-nodejs-steem-js-nodemailer-20180103t150020353z
categorysteemdev
json_metadata{"tags":["steemdev"],"app":"steemit/0.1"}
created2018-01-03 15:00:18
last_update2018-01-03 15:00:18
depth1
children0
last_payout2018-01-10 15:00:18
cashout_time1969-12-31 23:59:59
total_payout_value0.024 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length110
author_reputation18,524,616,078
root_title"Tutorial: Create a Bot that sends you an Email on received Votes and Comments (nodeJS, steem-js, nodemailer)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id26,798,993
net_rshares2,603,641,612
author_curate_reward""
vote details (2)
@jusdev89 ·
Wow im using this forsure. Good work man Thank you.
properties (22)
authorjusdev89
permlinkre-schererf-tutorial-create-a-bot-that-sends-you-an-email-on-received-votes-and-comments-nodejs-steem-js-nodemailer-20180103t213259468z
categorysteemdev
json_metadata{"tags":["steemdev"],"app":"steemit/0.1"}
created2018-01-03 21:33:00
last_update2018-01-03 21:33:00
depth1
children0
last_payout2018-01-10 21:33:00
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length51
author_reputation5,205,613,679
root_title"Tutorial: Create a Bot that sends you an Email on received Votes and Comments (nodeJS, steem-js, nodemailer)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id26,873,929
net_rshares0
@msaid ·
![](https://steemitimages.com/DQmVkXgMfk9EcEYK5MG2oaB3NgrSjXPG1ZXofVP1L3Hk6eh/image.png)
properties (22)
authormsaid
permlinkre-schererf-tutorial-create-a-bot-that-sends-you-an-email-on-received-votes-and-comments-nodejs-steem-js-nodemailer-20180217t030557611z
categorysteemdev
json_metadata{"tags":["steemdev"],"image":["https://steemitimages.com/DQmVkXgMfk9EcEYK5MG2oaB3NgrSjXPG1ZXofVP1L3Hk6eh/image.png"],"app":"steemit/0.1"}
created2018-02-17 03:05:57
last_update2018-02-17 03:05:57
depth1
children0
last_payout2018-02-24 03:05:57
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length88
author_reputation3,344,656,366
root_title"Tutorial: Create a Bot that sends you an Email on received Votes and Comments (nodeJS, steem-js, nodemailer)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id38,150,572
net_rshares0
@palog ·
$0.03
wow, nice
👍  ,
properties (23)
authorpalog
permlinkre-schererf-tutorial-create-a-bot-that-sends-you-an-email-on-received-votes-and-comments-nodejs-steem-js-nodemailer-20171013t084114975z
categorysteemdev
json_metadata{"tags":["steemdev"],"app":"steemit/0.1"}
created2017-10-13 08:41:15
last_update2017-10-13 08:41:15
depth1
children0
last_payout2017-10-20 08:41:15
cashout_time1969-12-31 23:59:59
total_payout_value0.033 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length9
author_reputation85,940,164,101
root_title"Tutorial: Create a Bot that sends you an Email on received Votes and Comments (nodeJS, steem-js, nodemailer)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id17,566,015
net_rshares14,170,165,260
author_curate_reward""
vote details (2)
@schererf ·
$0.06
Brief Update: It works great :-)

![steemMailerBot Results.jpg](https://steemitimages.com/DQmd4kxHyShAsgsW1SmgDUYcVUHPoTTvVbsenZkGdpPae5S/steemMailerBot%20Results.jpg)
👍  ,
properties (23)
authorschererf
permlinkre-schererf-tutorial-create-a-bot-that-sends-you-an-email-on-received-votes-and-comments-nodejs-steem-js-nodemailer-20171009t085509955z
categorysteemdev
json_metadata{"tags":["steemdev"],"image":["https://steemitimages.com/DQmd4kxHyShAsgsW1SmgDUYcVUHPoTTvVbsenZkGdpPae5S/steemMailerBot%20Results.jpg"],"app":"steemit/0.1"}
created2017-10-09 08:55:06
last_update2017-10-09 08:55:06
depth1
children0
last_payout2017-10-16 08:55:06
cashout_time1969-12-31 23:59:59
total_payout_value0.060 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length167
author_reputation26,658,463,264,121
root_title"Tutorial: Create a Bot that sends you an Email on received Votes and Comments (nodeJS, steem-js, nodemailer)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id17,180,589
net_rshares25,324,541,553
author_curate_reward""
vote details (2)
@shelob9 ·
$0.06
Neat little bot. This is a great introduction to the steem js client. I upvoted and resteemed.

I'd love to try adapting to use Serverless so that it could run on demand and would ot have to be running locally.
👍  ,
properties (23)
authorshelob9
permlinkre-schererf-tutorial-create-a-bot-that-sends-you-an-email-on-received-votes-and-comments-nodejs-steem-js-nodemailer-20171008t155246257z
categorysteemdev
json_metadata{"tags":["steemdev"],"app":"steemit/0.1"}
created2017-10-08 15:52:48
last_update2017-10-08 15:52:48
depth1
children1
last_payout2017-10-15 15:52:48
cashout_time1969-12-31 23:59:59
total_payout_value0.047 HBD
curator_payout_value0.015 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length210
author_reputation3,252,412,717
root_title"Tutorial: Create a Bot that sends you an Email on received Votes and Comments (nodeJS, steem-js, nodemailer)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id17,115,321
net_rshares26,028,001,041
author_curate_reward""
vote details (2)
@schererf ·
That's a good idea. I think that schouldn't be a big problem to change the source-code that it runs inside the browser-engine. Please keep me informed about it...
👍  
properties (23)
authorschererf
permlinkre-shelob9-re-schererf-tutorial-create-a-bot-that-sends-you-an-email-on-received-votes-and-comments-nodejs-steem-js-nodemailer-20171009t085311887z
categorysteemdev
json_metadata{"tags":["steemdev"],"app":"steemit/0.1"}
created2017-10-09 08:53:09
last_update2017-10-09 08:53:09
depth2
children0
last_payout2017-10-16 08:53:09
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length162
author_reputation26,658,463,264,121
root_title"Tutorial: Create a Bot that sends you an Email on received Votes and Comments (nodeJS, steem-js, nodemailer)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id17,180,430
net_rshares0
author_curate_reward""
vote details (1)
@terenceplizga ·
$0.03
Excellent tutorial, and nicely written.
👍  ,
properties (23)
authorterenceplizga
permlinkre-schererf-tutorial-create-a-bot-that-sends-you-an-email-on-received-votes-and-comments-nodejs-steem-js-nodemailer-20171023t002241607z
categorysteemdev
json_metadata{"tags":["steemdev"],"app":"steemit/0.1"}
created2017-10-23 00:24:45
last_update2017-10-23 00:24:45
depth1
children0
last_payout2017-10-30 00:24:45
cashout_time1969-12-31 23:59:59
total_payout_value0.023 HBD
curator_payout_value0.007 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length39
author_reputation393,106,184,435
root_title"Tutorial: Create a Bot that sends you an Email on received Votes and Comments (nodeJS, steem-js, nodemailer)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id18,338,742
net_rshares14,262,048,845
author_curate_reward""
vote details (2)