Making a twitter bot in the year 2022 with node.js
2022-08-26
Recently, we made the logistic map fractal visualizer app, but, how do we keep USER ENGAGEMENT METRICS up? Gotta make a twitter bot to post fractals, that's how!
I found it somewhat tricky to get this working, because there is a lot of odd lingo out there. There are libraries that may help you, but this post goes through making an OAuth client in node.js to post to the twitter API with fairly unabstracted code. OAuth still confuses me, but if you follow these steps, you can make a bot!
#Step 1
Get your keys. I forget the exact procedure for this but go to https://developer.twitter.com and sign up, register an "app" with them, and poke around for awhile. This github repo provides a couple more details about the process of registering https://github.com/manzt/cron-tweet#create-a-standalone-app-in-the-developer-portal
After sometime, you should get a screen that looks like this. Keep this around for Step 4. Note: I did not have to apply for any elevated access for this article, just the basic level you get from the initial registration! I believe in twitter terms, this article only requires "Essential" access and not any "Elevated" access.
#Step 2
Make a repo with a couple dependencies
#Step 3
Update package.json
with a couple scripts to build these typescript files
(alternatively use ts-node
or similar to run the ts files directly)
And tsconfig.json
file
This compiles the src
folder and outputs js files to the dist
directory, and
we can run the node dist/bot.js
file with node to post the file
#Step 4
Create a .env file with your keys that you found in the above screenshot corresponding to the box1 (which gives you API_KEY/API_SECRET, sometimes also referred to as consumer keys in other documents) and box2 (which gives you ACCESS_TOKEN and ACCESS_TOKEN_SECRET) from the above screenshot
API_KEY="xxxxxxxxxxxxxxxxxxxxxxxxx"
API_SECRET="yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy"
ACCESS_TOKEN="aaaaaaaaaaaaaaaaaaa-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
ACCESS_TOKEN_SECRET="bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
Important: Also add the .env to your .gitignore, you don't want to commit this to github!
#Step 5
Make the bot! Create src/bot.ts
#Step 6
Now that you have the bot, you can run it locally with yarn post
(runs the
"post" script in package.json
) to test it out
#Step 7
Now to create a github action to run the bot.
Navigate to your repo's settings page, then go to Secrets->Actions and paste the keys one by one, see below screenshot
#Step 8
Create a file with the path .github/workflows/post.yml
in your repo to make
the post
You can use a cron/scheduled workflow in the github action, which the below example does https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#schedule
The below github action posts every 5 hours on the hour (see https://crontab.guru/ for more info on cron syntax)
#Conclusion
See https://github.com/cmdcolin/twitter_fractal_bot for working example. I could
have, in retrospect, used a library like twit
(https://www.npmjs.com/package/twit) but this code sample is not substantially
more complicated than using the twit
library.
You can also adapt this to post only when you push to your repo, or release a new version from your repo!
#Footnote 1: Examples of odd lingo or stumbling blocks you might come across
- Bearer token - The Bearer token is an alternative method of authenticating, but it cannot be used to post tweets
- Twitter API v1 vs v2 - We use a mix of v1 and v2, it's just the different URLs that we are posting to and can be mixed in our example
- Consumer key vs API key - they are the same thing in our example. If you look closely at the box 1 in the screenshot it says "Consumer keys" and then gives you an API key below *
#Footnote 2: Posting on release with a github action
Replace the cron section of the github action with
on: release