Fun with the Node, Express, and Twitter!

Last week, I studied NodeJS. I was struggling to wrap my head around node and thought to myself, there must be a better way. Well there is! Express to the rescue!! I wanted to explore more with node server code and decided to get some practice with API’s to boot. I googled around and found the Twitter API and thought this could be a fun place to start.

After more in depth research, I found out I could make a bot for Twitter. I imagined what I could do with it and the possibilities were almost endless. I could have it tweet random people, correct their spelling mistakes, or send inspirational quotes. I decided to make one that will respond to people who mention me in a tweet. We’re going to build one together!

Let’s get started

We’re only going to focus on server side code. I’m also going to be using ES6 syntax and using Promises to handle the asynchronous behaviour. No front end code or databases are necessary to get a basic bot to do some things.

The first thing we’re going to do is start with making a node based server. We’re going to use Express to achieve this. ExpressJS is a framework for NodeJS. It’s very fast and can get a node server up and running with fewer lines of code than plain node.

Here is a basic server. There is awesome documentation from Express’ website which you can find here. https://expressjs.com/en/starter/hello-world.html

My Server:

There is awesome documentation from Express’ website which you can find out how to make a basic server here. https://expressjs.com/en/starter/hello-world.html

Congratulations! We’ve made a basic server. If you go to the URL localhost:3000 or http://127.0.0.1:3000, you should see a blank page that says something like Hello World or whatever text you decided to send in the response.

Next,

Let’s head on over to Twitter to get some API keys Go to the Twitter App Center and create a new app.

  • Give your app a name, this is what @mentions will go to.
  • Give your app a basic description, whatever you want.
  • Give your app a website, I just put down my personal blog.
  • Don’t worry about callback for now, not necessary.
  • Sign the agreement.

DON’T SHARE YOU KEYS WITH ANYONE ONLINE!!! They can easily take control of them. I’d recommend you store them in another file and source them in or set them to an environment variable. Wikipedia can explain more about that https://en.wikipedia.org/wiki/Environment_variable .

Also note that Twitter only allows 15 queries per 15 minutes. So try not to go overboard when you’re testing your bot.

Now let’s setup Twitter.

In your terminal, ‘npm install — save twitter’. You can also use ‘npm install — save twit’. They are basically the same thing for what we’ll need.

Sweet! Now let’s handle the information we’ll get from twitter. Remember, our goal is to respond to tweets we’re mentioned at. The data we get from Twitter is huge! It comes in an array and we’re going to extract only what we want.

First let’s make an array that stores the filtered data we want. Then we’re going to make a function gets the tweets from Twitter and stores the extracted data in the array.

Here’s where we’re at right now.

Nice!! Your bot will now get up to 10 of the latest mentions you’ve had on Twitter!

Finally, let’s respond to the mentions in one fell swoop!

Let’s make another function to handle this method and then invoke it inside of the getMentions method.

Here is the final code.

Let’s go to Twitter and see what happens.

Success! I tweeted myself and my bot tweeted back at me since I was the latest to mention :)

Final Thoughts:

There are many other possibilities for this bot. Later on, I plan on building out a front-end so that I can manage my bot easier. I also would like to be able to have more customization of the message. I’d even like it to wake up and tweet automatically. I’d also like to incorporate a database at some point so we can keep a history of the bots activity. Once I get all that up and running, I’ll come back to this and send a link to play around with it.

This project showed me how easy it is to use Express and I love that it exists. This would’ve required more effort in NodeJS alone. Having to learn NodeJS made me appreciate Express that much more. This is a very powerful tool and I’ll be using it more frequently in future projects.

I had very little issues with getting requests from Twitter. I did end up in a loop though where I made 88 requests and timed myself out from Twitter. Overall I had fun learning about the process of bots and social media. Although challenging at times, it gave me much more appreciation to the modules, frameworks, and libraries out there to make my life easier. I hope to deliver something even more awesome soon!