All of the code described in this post can be found in this Github Repository https://github.com/xoscar/twitter-bot
Since I started writing blog posts again and as I’m a lazy person (Not a new thing). I wanted to have an automated way to showcase my work without having to remember to copy-paste the same twit over and over.
So I said to myself.
“ Why don’t I just write a bot that does that for me and while I build it I can use it to learn CDK and Twitter API as well as blogging about it 🌚”.
So First, I’d like to start with something about my professional background.
I’ve been working with AWS since early 2016, I started using it manually through the console but then that got pretty boring pretty fast. So I began to use the aws-sdk
to automate tasks and create resources.
Then, I worked on a project using the Serverless Framework while it was still in beta. With my previous AWS background, I was able to get most of it as I already understood the basics and to be honest I really loved it! ❤️. Since then I have had several backend & DevOps roles where I have been working closely with AWS.
But now, there is a new kid on the block (CDK) where I can combine both my excitement for Javascript (Typescript) and AWS.
So with that in mind let’s start with the infrastructure setup. For this project we’ll be using the following AWS services:
- Cloudformation. To contain all of the resources we’ll be creating.
- Lambda. This is where our Twitter bot logic will be executed.
- EventBridge. We’ll be using it to execute the lambda function in a Cron job and will send the post information as part of the event.
There are other resources that will be created as part of the lambda function and EventBridge that are not described above like the CloudWatch log groups and IAM roles.
The first step required by CDK is to initialize it by executing:
npm run cdk bootstrap
You’ll need to have a valid set of AWS credentials as this step creates some resources.
Then, we can start by declaring our application stack:
This will be the entry point where we are going to be creating the rest of the resources and will be spreading the configuration across the different child resources.
Next, by using the Construct
extendable class we can add the Lambda function.
The last resource we’ll need to add is the event rules to execute the bot as a cron job and to send the tweet information as part of the event.
Then everything can be wrapped into a CDK.APP context.
We are going to talk about how to run the CDK deployment at the end when we’ll have all of the pieces.
Twitter Bot Implementation
Ok, now we’ll have to swap our DevOps hat for the Backend Developer one 😬.
Before starting to write any code, there are a couple of things that need to be done to have access to use the Twitter API, these are:
The end goal for this is to have access to the API key and the API secret key credentials.
Once we have the credentials, there is a third step that we need to do. We have to get an oauthToken
and an oauthTokenSecret
. To do this, we’ll have to authorize our own application to have access to our Twitter account so we can create posts by following a standard OAuth procedure.
I created a script to avoid doing it manually, you can find it here: https://github.com/xoscar/twitter-bot/blob/main/scripts/getToken.ts
It requires the configuration to be set with your initial app credentials and it will prompt you to open a URL where you’ll need to copy-paste a code.
Note that every step and script that is described in this post is done by using Twitter’s own APIs and procedures combined with your own data. And you should’t share them with anyone.
Ok, so at this point, you should have these credentials:
- API Key.
- API Secret Key.
- OAuth Token.
- OAuth Token Secret.
The next part is to start writing the Lambda function that will be communicating with the API.
We’ll be using the post tweets endpoint described here.
To start, we need to create a way to send the credentials with each request. We can implement a simple OAuth service wrapper that will be using the environment variables from the Lambda resource.
Then, we can write a simple Twitter service that will be interacting directly with the API.
The last thing we need to do is add our handler function that will be listening for the EventBridge events.
Configuration and Deployment
Once we have both the resources and the Twitter bot logic in place we are ready to finalize the configuration and run the deployment.
First, we need to set up the config, this is the template file you’ll find in the repo:
After creating a config.json
file with your own info we can proceed.
Then, we can add a script command to the package.json
to run both the TS transpiler and the CDK deployment.
"scripts": { "build": "tsc --build", "cdk": "cdk", "deploy": "npm run build && cdk deploy", "get-token": "ts-node ./scripts/getToken.ts"}
Now we can just run npm run deploy
and the end result should be something like this:
And the Lambda function should look like this:
Depending on the config you specify for both the cron job and the post text the bot will be posting it for you 😃.
Conclusion
- The CDK framework is a great tool to combine coding standards and patterns to infrastructure resources.
- To use the Twitter API you should understand the basics of OAuth.
- Automating processes is a really fun way of learning.
Thanks for reading, once again you can find the full repo here https://github.com/xoscar/twitter-bot.
If you have any questions feel free to shoot me a message or a tweet at @oreyes_io.
Cheers!