How I Built a Twitter Keyword Monitoring Using a Serverless Node.js Function With AWS Amplify
In this article, I will demonstrate to you how I built a simple serverless Node.js function on AWS that sends me a daily email with a list of tweets that mention me on Twitter.
Recently, I used Twilert and Birdspotter for that purpose, which are specialized tools for Twitter keyword monitoring. But their free plans/trials don't fulfill my simple requirements, so I decided to implement them independently.
Prerequisites
I chose again AWS Amplify to deploy the serverless function to AWS.
If you don't already have an AWS account, you'll need to create one to follow the steps outlined in this article. Please follow this tutorial to create an account.
Next, you need to install and configure the Amplify Command Line Interface (CLI).
The serverless function will need access to secrets stored in the AWS Secret Manager. My article “How to Use Environment Variables to Store Secrets in AWS Amplify Backend” will guide you through this process.
Add Serverless Function to AWS
The first step is to add a new Lambda (serverless) function with the Node.js runtime to the Amplify application.
The function gets invoked on a recurring schedule. In my case, it will be invoked every day at 08:00 PM.
Let's add the serverless function using the Amplify CLI:
Get a list of tweets for a specific Twitter keyword
Now it's time to write the JavaScript code that returns a list of tweets for a given keyword.
Let's start by writing the twitter-client.js
module. This module uses FeedHive’s Twitter Client to access the Twitter API. The first step is to initialize the Twitter API client and trigger the request:
Next, we want to filter the response into three groups:
- Tweets: Tweets from the last 24 hours that were not published by my Twitter account and are no replies or retweets
- Replies: Tweets from the last 24 hours that were not published by my Twitter account and are replies
- Retweets: Tweets from the last 24 hours that were not published by my Twitter account and are retweets
Let's start by the filtering the statuses
response for "normal" tweets that are no replies or retweets:
Now we can filter for retweets and replies in a similar way:
The last step is to map the results to a very simple HTML structure that will be rendered inside the email body:
This is the code for the whole twitter-client.js
module:
Serverless function code
We can now use the twitter-client.js
in our serverless function:
At this point, we can publish our function by running:
If we successfully pushed the function to AWS, we can manually invoke the function in AWS Lamba by clicking the "Test" button:
The serverless function should then send an email with a list of tweets if someone mentioned the monitored keyword in the last 24 hours:
Conclusion
I had a lot of fun building this simple serverless function to monitor keywords on Twitter.
Serverless functions are a perfect choice for such a monitoring tool, as we only have to pay for the execution time of the serverless function.
What do you think about my solution? Leave a comment and tell me how you monitor your Twitter keywords.
If you liked this article, follow me on Twitter to get notified about new blog posts and more content from me.
Alternatively (or additionally), you can also subscribe to my newsletter.
Document & Test Vue 3 Components With Storybook
Storybook is my tool of choice for UI component documentation. Vue.js is very well supported in the Storybook ecosystem and has first-class integrations with Vuetify and NuxtJS. It also has official support for Vue 3, the latest major installment of Vue.js.
The 10 Favorite Features of My Developer Portfolio Website
Inspired by Braydon Coyer's new blogfolio, I've added some excellent new features to my portfolio website.