This project is a Twitter bot that monitors air quality in various American cities and posts alerts when the air quality changes. The bot is implemented in Python, containerized with Docker, and deployed on Google Cloud Run.
- Fetches air quality data for 75 U.S. cities
- Generates maps with air quality overlays
- Posts tweets with air quality alerts and maps
- Runs as a Flask service on Google Cloud Run
- Python 3.12
- Docker
- Google Cloud account
- Twitter Developer account
- AirNow API key
- Redis instance
The following environment variables need to be set:
X_API_KEY
: Twitter API keyX_API_KEY_SECRET
: Twitter API key secretX_ACCESS_TOKEN
: Twitter access tokenX_ACCESS_TOKEN_SECRET
: Twitter access token secretREDIS_HOST
: Redis hostREDIS_PORT
: Redis portREDIS_PASSWORD
: Redis passwordAIRNOW_API_URL
: AirNow API URLAIRNOW_API_KEY
: AirNow API keySRC_DIR
: Source directory (set to/app
in the Dockerfile)
app.py
: Main Flask applicationalert.py
: Core logic for fetching AQI data and posting tweetsmap.py
: Functions for generating maps with AQI overlaysconstants.py
: Contains data for American citiesDockerfile
: Instructions for building the Docker imagerequirements.txt
: List of Python dependencies
- Clone the repository
- Set up the required environment variables
- Build the Docker image:
docker build -t air-quality-bot .
- Deploy to Google Cloud Run:
gcloud run deploy --image air-quality-bot --platform managed
The bot exposes a single endpoint:
/run-aqi-tweet
(POST): Triggers the AQI data fetch and tweet process
You can set up a Cloud Scheduler job to hit this endpoint at regular intervals to automate the tweeting process.