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!
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.
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
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