The Twitter dynamic name generator is a pet project in which the twitter profile name updates for every minute with the followers count. I have written this article as a journal note of how I did this.
It was done as one of the challenges of #100DaysOfCode. Thanks to Suren and Vadim Bauer for their idea inspiration. Special thanks to Karthikeyan for his guidance.
Tech Stack Includes,
- Twitter lite - to communicate with twitter API
- node.js - script to update the twitter profile name
- AWS Lambda with serverless - to run Cron part
How I did this? What all the steps?
Initially started with the guidance of this article written by Suren. I majorly broke down this task into 3 parts.
- Getting Twitter API key from Twitter Dev account.
- Node js script to update the profile name.
- Cron part to update the profile name for every 1 minute.
Step 1 - Getting twitter API keys
Went to the twitter developers page as mentioned in the article and signed in. By providing the required pieces of information, twitter asked me to verify with my account email. Once verified with the email, my developers account was created. (Hurray!๐) In the Create an app after entering the information like app name, app description and so on, it asked me to clearly explain the reason of how I'm going to use the app. After reviewing the terms, my app was created. Noted down the Consumer API key, Consumer API Secret key and Access token & access token secret keys!
At the end this step I got my Consumer API key, Consumer API Secret key, Access token, Access secret key.
Next interesting part! let's dive into coding!
Step 2 - Script to update the profile name.
Okay, now it coding time. Fire mode on๐ฅ
I chose node.js as I'm familiar with that. From the Suren article I came to know about the twitter-lite.
Started with the simple,
$ npm init
$ npm install twitter-lite
In the index.js file, imported the twitter-lite and update the required keys as we got from the previous step,
const Twitter = require("twitter-lite");
const client = new Twitter({
subdomain: "api", // api is the default
version: "1.1", // version 1.1 is the default
consumer_key: process.env.twitter_consumer_key,
consumer_secret: process.env.twitter_consumer_secret,
access_token_key: process.env.twitter_access_token_key,
access_token_secret: process.env.twitter_access_token_secret,
});
Now comes the actually coding part.
So what is the logic to do this?
- We need to get the follower count using twitter API.
- We need to append the followers count with the name, but with emoji๐ค (Hmm)
- We need to update the new name to profile name using twitter API.
Cool, let's do it!
To get the followers count, used the twitter lite api.
client
.get("account/verify_credentials")
.then((results) => {
const followerCount = results.followers_count;
});
.catch(console.error);
};
Now, some little tricks! I got all the logic but struck with how to update the emoji dynamically. So asked Suren in the twitter. He just replied with, Have JSON with matching emoji. Gotcha, now continue with the journey!
Next simple logics like, get the followers count, convert to string, change to string array, then finally reduce the followers count with mapped emoji.
exports.handler = () => {
client
.get("account/verify_credentials")
.then((results) => {
const followerCount = results.followers_count;
const string = followerCount.toString();
const stringSplit = string.split("");
const followers = stringSplit.reduce((acc, val) => {
return acc + numberMatch[val];
}, "");
const profile_name = `${name} | ${emoji} |" + ${followers}`;
console.log("profile_name: ", profile_name);
})
.catch(console.error);
};
const numberMatch = {
"0": "0๏ธโฃ",
"1": "1๏ธโฃ",
"2": "2๏ธโฃ",
"3": "3๏ธโฃ",
"4": "4๏ธโฃ",
"5": "5๏ธโฃ",
"6": "6๏ธโฃ",
"7": "7๏ธโฃ",
"8": "8๏ธโฃ",
"9": "9๏ธโฃ",
};
Checked with by running,
node index.js
Coool!, now got the console with the new name, appended with the emoji. Now next is, need to update the profile name. Used the same twitter-lite API, to update the profile.
const response = client.post("account/update_profile", {
name: user_name,
});
Ran again to check whether the name gets updated in the twitter profile ( Tik tik tik moments)
Hurray ๐ It works!
Now comes the final part!
Step 3 - Cron to update the profile name for every 1 minute.
Okay! Now all works fine, but how to make this to run for every minute? Here comes my technical guru Karthikeyan into the play. Since he is a serverless expert, he suggested me to run the Cron in AWS Lambda function since I'm already familiar with lambda. Okay! Cool, let's do it. I'm skipping the AWS setup since it takes too long, and diving directly into the Cron part.
service: twitter-scheduler
custom:
twitter_consumer_key: ${ssm:/twitter_consumer_key~true}
twitter_consumer_secret: ${ssm:/twitter_consumer_secret~true}
twitter_access_token_key: ${ssm:/twitter_access_token_key~true}
twitter_access_token_secret: ${ssm:/twitter_access_token_secret~true}
provider:
name: aws
runtime: nodejs12.x
stage: prod
region: ap-south-1
environment:
STAGE: prod
functions:
subscription:
handler: index.handler
environment:
twitter_consumer_key: ${self:custom.twitter_consumer_key}
twitter_consumer_secret: ${self:custom.twitter_consumer_secret}
twitter_access_token_key: ${self:custom.twitter_access_token_key}
twitter_access_token_secret: ${self:custom.twitter_access_token_secret}
events:
- schedule: rate(1 minute)
This cron runs for every 1 minute, and the new profile name is updated dynamically. New profile name is consoled in cloud watch.
I have used service AWS System Manager(SSM) to avoid the exposure of explicit keys.
Finally, my work is done. Here is the link to the source code.
For any doubts, suggestions and feedback contact me in my twitter profile @code_rams. Hope you enjoyed and find this useful. Thanks for reading.
Top comments (3)
Great๐๐ป๐ฅ
This is cool ๐
Thanks so much Ben โฅ๏ธ