Archive tweets and make them searchable

Overview

Tweeter

Archive and search your tweets and liked tweets using AWS Lambda, DynamoDB and Elasticsearch.

Note: this project is primarily being used a test bed for figuring out best practices with AWS Lambda

Architecture

Requirements

Make sure you have the following installed before you proceed

Setup

Twitter API key setup

Set up a Twitter Developer account. Once you are signed up, create an app.

Add the credentials to SSM Parameter Store:

aws ssm put-parameter --name /tweeter/twitter/consumer_key --value <your consumer key value> --type SecureString --overwrite
aws ssm put-parameter --name /tweeter/twitter/consumer_secret --value <your consumer secret value> --type SecureString --overwrite
aws ssm put-parameter --name /tweeter/twitter/access_token --value <your access token value> --type SecureString --overwrite
aws ssm put-parameter --name /tweeter/twitter/access_token_secret --value <your access token secret value> --type SecureString --overwrite

Elastic Cloud setup

Elastic Cloud is not free

Set up an account with Elastic Cloud. Create a deployment and then an Elastic App Search engine. Retrieve the private key from the Credentials section.

Add the private key to SSM.

aws ssm put-parameter --name /tweeter/es/private_key --value <your password> --type SecureString --overwrite

Also, update the configuration in ui/src/config/engine.json, specifically the values of endpointBase and searchKey.

Deploy the backend

Build and deploy your application for the first time by running the following commands in your shell:

make build
make deploy.guided

The first command will build the source of your application within a Docker container. The second command will package and deploy your application to AWS. Guided deploy means SAM CLI will ask you about the name of your deployment/stack, AWS Region, and whether you want to save your choices, so that you can use make deploy next time.

Deploy the frontend

In the ui directory, deploy the application with AWS Amplify.

amplify publish

Use the SAM CLI to build and test locally

Whenever you change your application code, you'll have to run build command:

make build

The SAM CLI installs dependencies defined in poller/requirements.txt, creates a deployment package, and saves it in the .aws-sam/build folder.

Test a single function by invoking it directly with a test event:

make invoke.poller
make invoke.indexer

Working with localstack

Validate DynamoDB was populated using Localstack:

docker-compose up -d
make invoke.poller
aws --endpoint-url=http://localhost:4566 dynamodb scan --table-name TweetsTable --max-items 10

An event is a JSON document that represents the input that the function receives from the event source. Test events are included in the events folder in this project.

Fetch, tail, and filter Lambda function logs

To simplify troubleshooting, SAM CLI has a command called sam logs. sam logs lets you fetch logs generated by your deployed Lambda function from the command line. In addition to printing the logs on the terminal, this command has several nifty features to help you quickly find the bug.

NOTE: This command works for all AWS Lambda functions; not just the ones you deploy using SAM.

sam logs -n TweeterPoller --stack-name tweeter --tail

You can find more information and examples about filtering Lambda function logs in the SAM CLI Documentation.

CI/CD with GitHub

Backend

Create a user github with the following policy: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-permissions.html.

Create secrets with AWS.

AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY

Frontend

Follow the AWS Amplify console to configure the frontend.

Notes

  1. Set StreamModeEnabled to false in samconfig.toml the first time you run the poller to get all tweets.
  2. Twitter apparently only allows access to a user's most recent 3,240 tweets with this method.
  3. This probably isn't the best use case for Lambda, which has a timeout of 15 minutes. Ideally we should be calling Fargate for long running task. That said, in practice, because of the limitations of Twitter's API, getting all the available tweets is well within that limit.

Appendix: Powertools

Tracing

Tracer utility patches known libraries, and trace the execution of this sample code including the response and exceptions as tracing metadata - You can visualize them in AWS X-Ray.

Logger

Logger utility creates an opinionated application Logger with structured logging as the output, dynamically samples 10% of your logs in DEBUG mode for concurrent invocations, log incoming events as your function is invoked, and injects key information from Lambda context object into your Logger - You can visualize them in Amazon CloudWatch Logs.

Metrics

Metrics utility captures cold start metric of your Lambda invocation, and could add additional metrics to help you understand your application KPIs - You can visualize them in Amazon CloudWatch.

License

MIT

You might also like...
Trading bot that uses Elon Musk`s tweets to know when to buy cryptocurrency.
Trading bot that uses Elon Musk`s tweets to know when to buy cryptocurrency.

Elonbot Trading bot that uses Elon Musk`s tweets to know when to buy cryptocurrency. Here is how it works: Subscribes to someone's (elonmusk?) tweets

 A Bot to get RealTime Tweets to a Specific Chats from Desired Persons on Twitter to Telegram Chat.
A Bot to get RealTime Tweets to a Specific Chats from Desired Persons on Twitter to Telegram Chat.

TgTwitterStreamer A Bot to get RealTime Tweets to a Specific Chats from Desired Persons on Twitter to Telegram Chat. For Getting ENV's Refer this Link

🔪 Block replies to viral tweets from users getting paid to promote useless products
🔪 Block replies to viral tweets from users getting paid to promote useless products

This Tweet Took Off Ublock Origin filter list targeting long reply chains posted by twitter users who get paid to promote random products on viral twe

This is a simple Python bot to identify sentiments in tweets

Twitter-Sentiment 👋 Hi There! 📱 This is a simple Python bot to identify sentiments in tweets 👨‍💻 This project was made for study, and pratice. You

Python script to harvest tweets with the Twitter API V2 Academic Research Product Track

Tweet harvester Python script to scrape, collect, and/or harvest tweets with the Twitter API V2 Academic Research Product Track. Important note: In or

Sentiment Analysis web app using Streamlit - American Airlines Tweets
Sentiment Analysis web app using Streamlit - American Airlines Tweets

Analyse des sentiments à partir des Tweets L'application est développée par Streamlit L'analyse sentimentale est effectuée sur l'ensemble de données d

A Bot that Forwards Tweets to Telegram using Airtable as a database.

Twitter Telegram Forward A Bot that Forwards Tweets to Telegram using Airtable as a Database. Features: Handles multiple twitter and telegram channels

Twitter-Scrapping - Tweeter tweets extracting using python

Twitter-Scrapping Twitter tweets extracting using python This project is to extr

BLYRIC is a Twitter bot that tweets a song lyric every night.
BLYRIC is a Twitter bot that tweets a song lyric every night.

BLYRIC BLYRIC, a bot that tweets a song lyric every night. Follow on Twitter: @blyric_ Overview BLYRIC is a Twitter bot that tweets a song quote every

This bot will pull a stream of tweets based on rules you set and automatically reply to them.

Twitter reply bot This bot will pull a stream of tweets based on rules you set and automatically reply to them. I built this bot in order to help comb

Brains 1 Feb 13, 2022
An attempt to make a bot that can auto-archive Danganronpa KG RPs on Discord.

Danganronpa Killing Game Archiving Bot An attempt to make a bot that can auto-archive Danganronpa KG RPs on Discord. The final format is meant to look

Astrea 1 Nov 30, 2021
❄️ Don't waste your money paying for new tokens, once you have used your tokens, clean them up and resell them!

TokenCleaner Don't waste your money paying for new tokens, once you have used your tokens, clean them up and resell them! If you have a very large qua

0xVichy 59 Nov 14, 2022
The purpose of this bot is to take soundcloud track requests, that are posted in the stream-requests channel, and add them to a playlist, to make the process of scrolling through the requests easier for Root

Discord Song Collector Dont know if anyone is actually going to read this, but the purpose of this bot is to check every message in the stream-request

null 2 Mar 1, 2022
A Twitter Bot that retweets and likes tweets with the hashtag #girlscriptwoc and #girlscript, and also follows the user.

GirlScript Winter of Contributing Twitter Bot A Twitter Bot that retweets and likes tweets with the hashtag #girlscriptwoc and #girlscript, and also f

Pranay Gupta 9 Dec 15, 2022
Fetching tweets and integrating it with Kafka and PySpark

KafkaPySpark Zookeeper bin/zookeeper-server-start.sh config/zookeeper.properties Kafka Server bin/kafka-server-start.sh config/server.properties Kafka

Priyansh 2 Dec 29, 2021
null 471 Dec 24, 2022
(@Tablada32BOT is my bot in twitter) This is a simple bot, its main and only function is to reply to tweets where they mention their bot with their @

Remember If you are going to host your twitter bot on a page where they can read your code, I recommend that you create an .env file and put your twit

null 3 Jun 4, 2021
A python package that fetches tweets and user information in a very pythonic manner.

Tweetsy Tweetsy uses Twitter's underlying API to fetch user information and tweets and present it in a human-friendly way. What makes Tweetsy special

Sakirul Alam 5 Nov 12, 2022
Scrape Twitter for Tweets

Backers Thank you to all our backers! ?? [Become a backer] Sponsors Support this project by becoming a sponsor. Your logo will show up here with a lin

Ahmet Taspinar 2.2k Jan 2, 2023