Bot Build #9 - Comment Mention Notification Bot
Repository
e.g. https://github.com/steemit/steem-js
What Will I Learn?
- Create Mention Notification Bot
- Send Email With NodeJS
Requirements
- Node.JS, Here
- Steem Package (Install: npm install steem --save)
Difficulty
- Intermediate
Tutorial Contents
You will learn how to create Mention Notification Bot
Curriculum
- Bot Build #1
- Bot Build #2
- Bot Build #3
- Bot Build #4
- Bot Build #5
- Bot Build #6
- Bot Build #7
- Bot Build #8
- SteemJS Tutorials #1
- NodeJS Tutorials #1
The Tutorial
Step 1 - Setup & Variables
The notification will be sent through email, so we need to import nodemailer
to send emails with nodejs
//Importing NodeMailer & steem.
let nodemailer = require('nodemailer');
let steem = require('steem');
from here we need to create our username (mentioned account) that get the mention and create nodemailer transporter
//The Mentioned Account.
const ACC_NAME = "lonelywolf";
//setup nodemailer transporter (with gmail service)
let transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: '[email protected]',
pass: '123456789'
}
});
change user
to your Gmail address and pass
to your Gmail password.
make sure you do it on your PC and not in repl.it because someone can steal your account!
the service is the mail service that we use, I recommend to use Gmail because it's basic and easy service.
just to make it fancy I'll add 2 comments to the console when I run the script and to make the script "alive".
//"Starting" the script (sending comments to the console)
console.log("Comment Mention Notification Bot Running...");
console.log("Waiting For Mentions...");
Step 2 - Create The Mail Function
first of all, we need to create the function if you want to make it easier you can add a variable with the email address and use it or you can write it at the function.
//email sending function - mail - yourmail, to - reciever mail (author), title - the title of the email (subject), body - the body of the email (message).
function sendMail(mail, to, title, body){
}
as the comment //
said, this is mail sending function,
mail
- your email address
to
- the reciever (where the notification goes)
title
- the title of the mail
body
- the notification (body / message)
now we need to create a variable with the mail options, inside of that we will need the from
email the to
email, the subject
(title) email, the text
(subject) email.
let mailOptions = {
from: mail,
to: to,
subject: title,
text: body
};
and now we just need to send the email with the mail options.
transporter.sendMail(mailOptions, function(err, res){
if(err) {
console.log(err);
}else{
console.log('Notification Email Sent Successfully!');
}
});
as the function said sendmail
this function just send the mail, it uses JSON format for the options (the variable we created before) and it gives an error (if there is) and results.
if there is an error we send the error to the console and if not we send a comment to the console and say that the notification sent successfully.
full function code
//email sending function - mail - yourmail, to - reciever mail (author), title - the title of the email (subject), body - the body of the email (message).
function sendMail(mail, to, title, body){
let mailOptions = {
from: mail,
to: to,
subject: title,
text: body
};
transporter.sendMail(mailOptions, function(err, res){
if(err) {
console.log(err);
}else{
console.log('Notification Email Sent Successfully!');
}
});
}
now to complete the script we just need to listen for stream transactions (listen for new comments)
//Listen To Stream Transactions - Listening for comments.
steem.api.streamTransactions('head', function(err, result){
const type = result.operations[0][0]; // transaction type
const data = result.operations[0][1]; // transaction data
}
as always we're using streamTransactions
function to get the transaction we need (currently is comment)
so we need to check if the transaction type is a comment
if(type == 'comment'){ //checking the type of the transaction
}
if it's a comment we need to get the body of the comment and get the account name with the @
, so we just add @
before the account name.
const body = data.body, //the body of the post (post content)
ACCOUNT_NAME = "@" + ACC_NAME; //the account name + @ (mention)
to know if the account name is mentioned in the post we need to check the body of the comment with the account name
if(body.includes(ACCOUNT_NAME)){ //checking if the body(content) includes the mention (your account)
}
body.inclues (or variable
.includes) checks if something is include in the string.
so if someone mentioned us we send the mail and send a comment to the console
console.log("The Account " + data.author + " mentioned you at the post [PermaLink]: " + data.parent_permlink);
//sending email
sendMail("[email protected]", "[email protected]", "Steemit Comment Notificaion Alert", "The Account " + data.author + " mentioned you at the post [Permalink]: " + data.parent_permlink);
and, here we're done!
Suggestion
: if you're working on web browser extension you can send the notification to chrome notifications/firefox or at a website you can make notification box with the notifications, you can take it anywhere you want!
Please! for the next tutorial I will be really happy if you can give me a suggestion for the next script/tutorial!
have a great day!
You can check the full work at my repl.it account and other bots that I create!
Proof of Work Done
the work made in Repl.it, https://repl.it/@lonelywolf22/Steem-Bots-Comment-Mention-Notification-Bot-V02-Done
user: https://repl.it/@lonelywolf22
GitHub: https://github.com/lonelywolf1
Utopian Tutorials GitHub Repository: https://github.com/lonelywolf1/Bot-Projects-SteemJS (This tutorial will add to the GitHub repository in the next few days!)
Hello @lonelywolf,
This tutorial is also wonderful, as your previous. I have tried this and it works fine. But I think there is an issue, that should be mentioned.
If someone use 2-Step Verification service on their google account. this may occurs mail sending failed. What will be the solution for 2-Step Verification enabled gmail account.
Thanks for your nice work.
first of all thanks for your suggestion, I made a tutorial about almost the same script so I don't think it will be good to just change the way it works.
because it's not an advanced script I didn't think about it, the solution will be to create another account to send the emails, super easy.
Hey @touhidalam69
Here's a tip for your valuable feedback! @Utopian-io loves and incentivises informative comments.
Contributing on Utopian
Learn how to contribute on our website.
Want to chat? Join us on Discord https://discord.gg/h52nFrV.
Vote for Utopian Witness!
Hey @lonelywolf
Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!
Contributing on Utopian
Learn how to contribute on our website or by watching this tutorial on Youtube.
Want to chat? Join us on Discord https://discord.gg/h52nFrV.
Vote for Utopian Witness!
Thank you for your contribution.
While I liked the content of your contribution, I would still like to extend one advice for your upcoming contributions:
Looking forward to your upcoming tutorials.
Your contribution has been evaluated according to Utopian rules and guidelines, as well as a predefined set of questions pertaining to the category.
To view those questions and the relevant answers related to your post,Click here
Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]
Thank you! Because I Have 9 tutorials I already explained about some functions or codes that appear in this tutorial so I didn't go into the small details.
if you think it will be better to do it in every single tutorial, I'll do it