I have built a streamlit app with pretty nice looking charts. I wanted to create a Twitter bot that can generate and post them on-demand.
- Container:
main.py
: all the code that needs to run on a schedule.utils.py
: utility functions to generate charts.Dockerfile
: image with chromedriver to run selenium (required to generate altair png exports).
- Build: Google Cloud Build
- Image storage: Google Artifact Registry
- Run: Google Cloud Run Jobs
- Scheduling: Google Cloud Scheduler
- Google Cloud account set up with a project created in it.
- Twitter developer account set up with an app already created. You need Consumer and Access tokens/secrets.
git clone https://github.com/hugolmn/dividend-chart-bot.git
cd dividend-chart-bot
- Install the gcloud CLI following these instructions
- Enable the Artifact Registry API.
- Create a registry Now, to create an image and store it into the registry, you can run the following commands from your code workspace:
gcloud builds submit --tag <YOUR_REGION>-docker.pkg.dev/<YOUR_PROJECT_ID>/<YOUR_REGISTRY>/<YOUR_IMAGE_NAME>:latest
- Search "Cloud Run" > Select "Jobs" tab > "Create Job".
- For "Container image URL": select your registry in "Artifact Registry" > Select your image name > Select your image version
- If you always want to run on the lastest image version, replace the ending "@sha:******" by ":latest". in the "Container image URL".
- Select your job name, region, task capacity etc.
- In the "Variables & Secrets" tab, create the environment variables "api_key", "api_secret", "access_token", "access_token_secret", with the appropriate values from your Twitter developer account. You can also use secrets (free tier is limited to 6 secrets).
- Create your job.
- Enable the Cloud Scheduler API if needed.
- Create a job following these instructions.
Congratulations! Your bot is now all set!