A Node script to create Dynamic Twitter Header
This project is created to create a Twitter Bot in NodeJS that fetches 3 latest followers using Twitter API V2 and create a Dynamic Twitter Header and upload it to Twitter. Dotenv, Axios, Sharp, Cron NPM packages are also used in this project.CronJob is run once every 15 MINUTES in order to avoid making too many requests to Twitter API and not to run out of dyno hours on Heroku. ๐ฆ Feel free to reach me at Twitter ๐ค
- NPM
- Twitter API Client
- Dotenv
- Axios
- Sharp
- Twitter Develeoper Portal
- Cron
- Jimp
- Heroku
npm install
node index.js // to start generating header
-
- Create an application on
Twitter Developer Portal
- Apply for
Elevated access
if you don't have one
- Create an application on
-
- Install NPM using
npm init -y
to skip questions. - Create
index.js
to test node.
- Install NPM using
-
- Install Twitter API V2 package using
npm i twitter-api-v2
. - Create a
twitterClient.js
module which connects to Twitter API V2. - Install Dotenv package using
npm install dotenv --save
. - Create a project at
https://developer.twitter.com/en/portal/projects/new
. - Create a
.env
file and copy API Keys to that file. - Create a
testTweet()
method insideindex.js
to test API. - Check Twitter App if it works, it should tweet the string inside method(IT WORKS ๐)
- Delete or comment
testTweet()
method because it worked and is no longer needed.
- Install Twitter API V2 package using
-
- Create a
twitterController.js
module which collects data from Twitter API. - Get Twitter ID from Tweeterid
- Create
getFollowers()
method intwitterController.js
which returns 3 latest followers - Test
getFollowers()
method(IT WORKS ๐) - Create a wrapper function
generateHeader()
inindex.js
which allows us to fetch followers, their images, save images and update our header dynamically.
- Create a
-
- Create
getProfileImageUrl()
method intwitterController.js
which returns profile image URL from followers. - Create
imageController.js
module which contains methods to manipulate images. - Install Axios package using
npm i axios
, which performs our HTTP requests. - Install Sharp package using
npm i sharp
, which modifies fetched images and save it to our File System. - Create
saveImage()
method inimageController.js
which fetches rounded profile picture of followers using axios and save them to our images folder. - Call
getProfileImageUrl()
andsaveImage()
methods inindex.js
module. - Test
generateHeader()
wrapper function and check if images are fetched(IT WORKS ๐ฅณ)
- Create
-
- Install Jimp package using
npm install --save jimp
, which will help us create Dynamic Twitter Header. - Import required dependencies on top of
imageController.js
module. - Create a
createHeader()
method inimageController.js
module which createsdynamicTwitterHeader.png
usingheaderTemplate.png
and fetched profile picture of our followers. - Test
createHeader()
method and check if dynamicTwitterHeader.png is successfuly created(IT WORKS ๐)
- Install Jimp package using
-
- Create a
updateHeader()
method intwitterController.js
module which uploads dynamicTwitterHeader.png to Twitter. - Test
updateHeader()
method and check if dynamicTwitterHeader.png is successfuly uploaded to Twitter(IT WORKS wooohoooo ๐ฅณ)
- Create a
-
- Install Cron package using
npm install cron
, to create a CronJob which generates dynamicTwitterHeader.png at a specified time. - Import const CronJob = require("cron").CronJob dependency on top of
index.js
. - Create a new object job from the CronJob class which calls
generateHeader()
wrapper function once per minute. - Test
job
object and check if dynamicTwitterHeader.png is successfuly created and uploaded to Twitter(IT WORKS yeehaw ๐) - Change CronJob to run once every 15 MINUTES in order to avoid making too many requests to Twitter API and not to run out of dyno hours on Heroku.
- Install Cron package using
-
- Signed up for Heroku free tier(500 Dyno Hours/ month)
- Followed this medium article by Rushikesh Mhetre to Deploy our bot on Heroku and have it run remotely
- Add a
Procfile
to repo andworker: node index.js
as a command. DisableWeb: npm start
dyno and enableworker: node index.js
dyno in Resources tab under Dashboard to start generating header every 15 minutes ๐
-
- Only keep profile pictures of 3 latest followers and delete the rest.
-
Tutorials
-
Twitter
-
Canva
-
NPM Packages
-
Deploy to Heroku
โKeep your face always toward the sunshine - and shadows will fall behind you.โ
โ Walt Whitman
๐ฆ ๐งถ โ