Skip to content

josepmonclus/twitter-bot-python-aws-lambda

 
 

Repository files navigation

Twitter Bot Using Python and AWS Lambda

Python License contributions welcome Twitter

This is a simple template you can use to build a twitter bot using Python and an AWS Lambda Function. I used it to create @dereksiversbot. Learn how to make your own here.

Why build a bot this way?

  1. It's quick and easy
  2. You have full control over the bot's actions
  3. It only uses services from AWS free tier (but see limitations first)

Pre-requisites

To build and use the bot, you'll need to:

  1. Register for a twitter developer account
  2. Create a twitter app. Make sure to give it Read and Write permissions.
  3. Set up an AWS account
  4. Create a Lambda Function for your bot
  5. Create a Lambda Layer to use additional libraries in your Lambda Function

How to use

To make your own bot follow these steps:

  1. Clone this repository on your local machine
  2. Create a virtual environment in your project's root directory: python3 -m venv venv && source venv/bin/activate
  3. Install the required libraries using pip: pip install -r requirements.txt
  4. Create a file called .env in the root directory of your project. Put your twitter App keys there:
ACCESS_TOKEN=<YOUR_ACCESS_TOKEN_HERE>
ACCESS_TOKEN_SECRET=<YOUR_ACCESS_TOKEN_SECRET_HERE>
CONSUMER_KEY=<YOUR_CONSUMER_KEY_HERE>
CONSUMER_SECRET=<YOUR_CONSUMER_SECRET_HERE>
  1. Make changes in the logic of the bot by modyifing src/lambda_function.py
  2. Test your changes locally by running python entrypoint.py from the root directory of your project

How to deploy

Once you are happy with your bot:

  1. Add any additional packages you used to requirements.txt
  2. Run sh createlambdalayer.sh from the root directory of your project. It'll generate a zip file with your libraries called layer.zip
  3. Update your Lambda Layer using layer.zip
  4. Run sh buildpackage.sh from the root directory of your project. It'll make a zip file with the code for your Lambda Function called lambda_function.zip
  5. Upload lambda_function.zip to your Lambda Function
  6. Add your twitter App keys as environment variables in the Lambda Function
  7. Add a scheduled trigger to your Lambda Function using EventBridge

Limitations

Read this before using the bot:

  • This is free unless you go crazy with it or use custom events for triggering the Lambda Function. Check the AWS Free Tier if you have any questions. Use it at your own risk!
  • Current logic is very simple. The bot will post a random tweet (excluding its last 3 tweets). If you want something more complex, you'll need to add it on your own.

Attributions

The createlambdalayer.sh script comes from this repository.

About

A simple twitter bot using Python and an AWS Lambda function

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 75.3%
  • Shell 24.7%