This tutorial is part of a series on âHow To Build Botâs Using Steem-JS Open Source Libraryâ it aims to demonstrate practical applications for bots and demystify the code used to create them.
- part 1- Auto liking bot
- part 2 - Curation trail bot
- part 3 - Auto follow bot
- part 4 - Paid upvote bot
Please check out those tutorials in order before this tutorial as we will build on knowledge learned.
The completed bot code is here - I know some people find it easier to see everything in one block and follow along that way. ⏠ď¸
Outline đ
One key feature missing from Steemit/Busy.org is a system to notify you whenever youâre mentioned in someone's post or comment. Today weâre going to be building a bot that will notify us whenever weâre mentioned. Itâs pretty common on other social media sites to be notified with badge or push notification. Notifications arenât my favourite things but again itâs a good opportunity to learn about building with Steem-js.
Requirements
- A plain text editor (I use atom.io )
Difficulty
This tutorial is intended to be beginner friendly. (I appreciate feedback on the teaching level)
Learning Goals
- Re-use knowledge learned about Steem-js in parts 1-4 investigating streaming transactions
- Search the content of posts/comments for mentions with Javascript
- Send an AJAX request
- Deploy to Netlify
Step 1 - Setup đť
For this tutorial, our bot will run in the web browser but weâll need to deploy it online for everything to work, weâll get to the later.
Use this template to get started - This is a base file for today's project and will kick things off with the standard streamTransactions()
function. Open the file in your text editor and browser to get started.
In this tutorial weâll need an ACCOUNT NAME
, weâll check to see whenever this account is mentioned.
// Tutorial 05 - @mention bot
const ACCOUNT_NAME = âyour-username-hereâ
Step 2 - Check content for Mention đ
steem.api.streamTransactions('head', function(err, result) {
let txType = result.operations[0][0]
let txData = result.operations[0][1]
});
Starting with the standard streaming function weâre going to create a function that uses the txType
and txData
to check for mentions. letsâs create a function that assumes we have access to that data.
function checkContentForMention(txType,txData) {}
If you remember from proviso tutorials all posts and comments on the Steem network are considered comments
even if it is a top-level post on your own blog. First, weâll check that the transaction type is a comment as weâre not interested in transfers, rewards, votes etc.
if(txType == 'comment') {
console.log('CHECKING FOR MENTION: ', txData)
let commentBody = txData.body;
}
Weâll add a console.log so we can see whatâs going on and start by storing the content of the post/comment which is stored in the .body
property.
Next, weâll add the @ symbol to the account name as weâre specifically looking for @mentions and not just the use of a name.
function checkContentForMention(txType,txData) {
if(txType == 'comment') {
console.log('CHECKING FOR MENTION: ', txData)
let commentBody = txData.body;
let mentionUsername = '@' + ACCOUNT_NAME;
return commentBody.includes(mentionUsername);
}
}
The .includes()
function will look if the commentBody contains our selected username. We then return the result so our checkContentForMention
will work to give us a true or false answer to the question Does this post contain the username?
Step 3 - Send an Email if mentioned đď¸ââď¸
What should we do if we find a post that has the username within it? It depends on the purpose of our bot, for this tutorial weâre going to send an email notifying us of the author and post that mentioned us.
To keep this tutorial shorter and friendly for beginners weâll use jQuery to send a request to a third party provider to deal with the email. Typically youâd have a backend system setup on a server that deals with sending emails, today building a bot in the browser thatâs out of scope so weâll use the service from formspree.io and theyâll send the email we a simple AJAX request. AJAX or Asynchronous Javascript and XML is a system that allows us to send or receive information to another server without reloading the page.
jQuery was already included in the base template. Let us add the code for an AJAX call to form spree
function sendEmail(txData){
console.log('SENDING EMAIL')
$.ajax({
url: 'https://formspree.io/[email protected]',
method: 'POST',
data: { message: `Hey! Looks like @${txData.author} mentioned you in their post http://steemit.com/@${txData.author}/${txData.permlink}` },
dataType: 'json'
});
}
The first time you use form spree youâll get an email asking to verify your email address, after that it will work as expected. It might seem too simple to be true but thatâs all thatâs needed. Formspree does all the heavy lifting to send emails correctly. Here weâve created a function that uses our ```txData`` for the author and permalink parts of the email message.
Note, the request might fail at this point
Because weâre dealing with an AJAX request to Formspree just opening the HTML file in our browser wonât be enough for this one. Weâll need to deploy our bot online.
Step 4 Deploying to Netlify đ
[https://netlify.com] is a modern hosting platform, it allows for super fast deployment of static sites. All you need to do is create an account then drag and drop your project folder. No long configuration, setup or ridiculous settings.
Before dragging your folder lets look at the finished code.
steem.api.streamTransactions('head', function(err, result) {
let txType = result.operations[0][0]
let txData = result.operations[0][1]
let includesMention = checkContentForMention(txType,txData)
if (includesMention) {
console.log('MENTION FOUND: ', txData)
sendEmail(txData)
}
});
Hereâs the full code #5 đ¤
Thatâs our 5th bot in the series if youâve deployed on Netlify your bot is ready to go. You might have to wait a minute to deploy but then visit the address and open the console. Get a friend to mention you in a comment and watch what happens. Whenever that webpage is open youâll get notifications. In the next post in the series, itâs time we started working with the command line and node.js.
Thereâs still a lot more we can do with bots and the Steem API but I hope you find this interesting and useful. If youâre looking into building bots let me know or ask any questions below âď¸
Other Posts in This series
- Part 1 - Beginner friendly - Build your first steem bot in Javascript - 30minutes
- Part 2 - Beginner friendly - Building bots With steem-js #2
- PArt 3 - Copy Follow Bot - Building Bots With steem-js #3
- Part 4 - Paid Voting Bot - Building Bots With steem-js #4
Posted on Utopian.io - Rewarding Open Source Contributors