Javascript is required
8 min read

How I Built a Twitter Keyword Monitoring Using a Serverless Node.js Function With AWS Amplify

How I Built a Twitter Keyword Monitoring Using a Serverless Node.js Function With AWS Amplify Image

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.


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:

1 amplify add function
2? Select which capability you want to add: Lambda function (serverless function)
3? Provide an AWS Lambda function name: twittersearchfunction
4? Choose the runtime that you want to use: NodeJS
5? Choose the function template that you want to use: Hello World
6? Do you want to configure advanced settings? Yes
7? Do you want to access other resources in this project from your Lambda function? No
8? Do you want to invoke this function on a recurring schedule? Yes
9? At which interval should the function be invoked: Daily
10? Select the start time (use arrow keys): 08:00 PM
11? Do you want to enable Lambda layers for this function? No
12? Do you want to configure environment variables for this function? No
13? Do you want to configure secret values this function can access? No
14? Do you want to edit the local lambda function now? No

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:

1const mokkappsTwitterId = 481186762
2const searchQuery = 'mokkapps'
3const searchResultCount = 100
5const fetchRecentTweets = async (secretValues) => {
6  // Configure Twitter API Client
7  const twitterClient = new twitterApiClient.TwitterClient({
8    apiKey: secretValues.TWITTER_API_KEY,
9    apiSecret: secretValues.TWITTER_API_KEY_SECRET,
10    accessToken: secretValues.TWITTER_ACCESS_TOKEN,
11    accessTokenSecret: secretValues.TWITTER_ACCESS_TOKEN_SECRET,
12  })
14  // Trigger search endpoint:
15  const searchResponse = await{
16    q: searchQuery,
17    count: searchResultCount,
18    result_type: 'recent',
19  })
21  // Access statuses from response
22  const statuses = searchResponse.statuses

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:

1const isTweetedInLast24Hours = (status) => {
2  const tweetDate = new Date(status.created_at)
3  const now = new Date()
4  const timeDifference = now.getTime() - tweetDate.getTime()
5  const daysDifference = timeDifference / (1000 * 60 * 60 * 24)
6  return daysDifference <= 1
9const fetchRecentTweets = async (secretValues) => {
10  // ...
11  const statuses = searchResponse.statuses
13  const tweets = statuses.filter((status) => {
14    const isNotOwnAccount = !== mokkappsTwitterId
15    const isNoReply = status.in_reply_to_status_id === null
16    const isNoRetweet = status.retweeted_status === null
17    return isNotOwnAccount && isNoReply && isNoRetweet && isTweetedInLast24Hours(status)
18  })

Now we can filter for retweets and replies in a similar way:

1const retweets = statuses.filter((status) => {
2  const isNotOwnAccount = !== mokkappsTwitterId
3  const isRetweet = status.retweeted_status
4  return isNotOwnAccount && isRetweet && isTweetedInLast24Hours(status)
7const replies = statuses.filter((status) => {
8  const isNotOwnAccount = !== mokkappsTwitterId
9  const isReply = status.in_reply_to_status_id !== null
10  return isNotOwnAccount && isReply && isTweetedInLast24Hours(status)

The last step is to map the results to a very simple HTML structure that will be rendered inside the email body:

1const { formatDistance } = require('date-fns')
3const mapStatus = (status) => {
4  const {
5    id_str: id,
6    created_at,
7    in_reply_to_screen_name,
8    in_reply_to_status_id_str,
9    text,
10    retweet_count,
11    favorite_count,
12    user: { screen_name: user_screen_name, followers_count, created_at: userCreatedAt,