The author of the original article, Kristóf Litavecz , learned to program in 2017. There are no major changes to his developer career (yet!), but this story shows that programming skills can be useful in everyday life. Some even argue that the ability to write code is becoming the fourth literacy.
Last summer, my wife and our two children flew from Hungary to California to visit friends. Among all the difficulties of the flight, our airline lost a baby stroller along the way. My numerous attempts to contact the airline via email, Twitter, polite and then not so polite calls to cover our expenses came to nothing. I realized that I had had enough and decided to act differently. I created a Twitter bot that responded to every tweet from the airline's account to remind them of our case, which had already been pending for three months. I did not do this for the purpose of revenge or extortion of money, no, under no circumstances. I just wanted justice and coverage for my lost baby stroller. At this point, I had been studying programming for about a year and actively used the freeCodeCamp community for support and inspiration. What I've done
If you want to copy my bot, simply clone my repository by entering the following line into your terminal: git clone https://github.com/krizsoo/twitterbot Step 2: Create a Twitter account and Twitter app If you don't already have a Twitter account, create it . Once you sign up, you can create a new application that will give you access to the Twitter API and you can generate tweets programmatically.
Once my application was configured, I received the four security keys needed to access the API:
Step 3: Set up the Twitter Bot I needed to tweak the configurations to make sure the Bot does what it needs to do.
- I created a new environment on the Cloud9 dev cloud platform ( you are familiar with it if you took the CS50 course - editor's note );
- Created an account on Twitter;
- Created a simple Twitter bot using Node.js;
- I set it up to randomly tweet one of ten reminders every hour;
- And he launched it.
How to do it
Step 1. Create a new environment in Cloud9 IDE Login to Cloud9 and create a new workspace. Select Node.js as the template.- Consumer Key (API Key) - consumer key
- Consumer Secret (API Secret) is a kind of “login + password” for your application
- Access Token - access token
- Access Token Secret - access token secret
-
Install Node.js dependencies
npm install --save twitter
- Set up your search query
q
- keywords;count
— the number of tweets that the request should return;result_type
— sorting logic, in our case it shows the most recent tweet first;lang
is a language (for example, English)
// Set up your search parameters
var params = {
q: '@lostbabystroller',
count: 1,
result_type: 'recent',
lang: 'en'
};
Step 4: Set up tweets The bot was triggering every hour, and I didn't want it to repeat the same message all the time. So I created about 10 tweets, and the Bot randomly selected a message each time. Important: Johnny Asmar recommends making sure you don't mention people in your answers. This is contrary to Twitter's TOS. Step 5. Set up the frequency of tweets. I configured the Bot to fire every hour. First I created a variable that represents the hour in milliseconds:
//задание временного интервала твитов
var INTERVAL = 1*60*60*1000;
Then I made sure that the Bot was launched correctly:
// Start bot and timer
BotStart();
setInterval(BotStart, INTERVAL);
Step 6. Launch the Bot Once everything was set up, I launched the Bot and waited patiently.
npm run serve
GO TO FULL VERSION