Twitter Bot for Dummies — #PeaceDay Hackathon

Discovering Node.js, Command Line and APIs…scary words.

This is a tutorial to create a Twitter Bot for non-developer like myself. If you love Lego or cooking, carry on reading.

Out of curiosity, I just completed my 1st hackathon #Peacehack at House of Vans London last weekend.

How to Promote Peace with a Twitter Bot?

Our aim was to counterbalance the negativity on Twitter by amplifying peaceful/ positive messages.

The Idea

Conflicts starts with negative emotions such as Frustrations, Anger, Fear. How could we make peace messages as impactful as fear messages?

Reply to Hate with Humour. Spread Hope.

We will not solve world issues with a Twitter bot, with that in mind…

Meet Hipeacebot

A bot that fights hate by retweeting positive messages #peaceDay and reply to negative messages with a one-liner joke.

On this journey, I was super lucky to team up with an awesome junior developer (Bill Sheikh) who helped me code the reply part.

Meet Hipeacebot created with Node.js, you could find the code on my Github repo

RESULTS

My bot got more followers than my personal Twitter account…

My bot was live!

It feels incredible to see so many messages streaming through my Terminal. Felt like Ms Robot!However Sh** goes wrong when you try new things…

WTF Nigel follows a peace bot?

We tracked #Peaceday and got way too many messages!

Bill reached 300 tweets in few minutes. I launched later and quickly reached 80 tweets so I quickly press CTRL-C to stop it…but my Twitter app account still got suspended.

We didn’t had the time to set up a retweet limitation…so my account got suspended after 2 hours.

Bad jokes will get you suspended

My bot also posted too many jokes… bad ones…I had to manually delete some of them.

The one-liner-joke library is not for everyone…

So if anyone got extra time to fix the code, feel free to go check out my Github repo.


HOW TO CREATE A TWITTER BOT?

OPTION 1 : You don’t want to code

Before diving into Node.js, Streaming API, etc…for people with zero coding skills, you could have a try at this easy tool: http://cheapbotsdonequick.com it’s fun and rewarding!

Sign in with your Bot Twitter account (make sure you have a phone number linked to the account)
once your Twitter account is linked, you should see this script. Replace the words [“this could be a tweet”] by [“fish slapping dance”] for example and scroll down
Click on the“refresh” button to see what happens and start playing with it! Make sure you don’t spam people by setting your post frequency (every 3 hours for instance)

To get you inspired, example of stuff you could create with that tool:

Credit goes to Freddie for this one!

This is fun but limited in scale as the bot will randomly post pre-written messages (or images) with no real interaction.


OPTION 2: You want to code (or at least try)

Cool, I was also really curious to see if I could make something with my basic Javascript and front-end skills.

On this journey, I was super lucky to team up with an awesome junior developer (Bill Sheikh) who helped me code the reply.

IDEATION (1h)

How to amplify the message of Peace? How to avoid echo chamber? What does Peace mean? To promote Peace is equivalent to avoid conflicts. How does conflicts start? It start with Division, Frustration and Fears. So how to prevent that?

Fear is powerful because it’s a strong emotion. How could we make peace messages as impactful as fear messages?

My initial idea was to make the bot transform negative message into positive ones...with a touch of irony.

For instance:

User1 says:
[#hipeacebot I hate my neighbours, they are so annoying!]
Bot reply:
[Hi @User1, did you mean: “My neighbours are lovely human beings, they could potentially be awesome!.”]

I wanted the bot to recognise key words categorised as negative (libraries of bad words) and replace them with positive ones (libraries of antonyms). I had no clue how to do it but I knew it would be feasible.

Initial coding direction:

  1. Look for #Hipeacebot in Twitter conversation.
  2. Mention people who tweeted with #Hipeacebot.
  3. The reply will print out the initial message with a twist:
  • Recognize the negative key words to be replaced.
  • Replace the keywords by positive ones.
  • Post the reply.

DEVELOPMENT & ITERATIONS (5h)

Time to roll up our sleeves!

I find coding is like cooking, you need to find the right ingredients and practise over and over again. After 20min searching online, I found this awesome tutorial from techknights.org that became my anchor.

YOUR INGREDIENTS:

  • Node.js
  • Text Editor (I use Atom)
  • Basic Javascript understanding (function, var, arrays, etc…)
  • Twitter Bot Account (linked with a phone number)
  • Twitter App Account
  • Command Line
  • A curious, positive and resilient mindset (you’ll fail a lot but when you manage to make it work, it’s like seeing a double rainbow🌈🌈)

PREPARATION:

STEP 1: Download Node.js

Go to https://nodejs.org/en/ and download the v6.11.3 version.

STEP 2: Set up your Twitter App Account

  • Once, the account is created, note down the 4 info below:
  1. API Key
  2. API Secret
  3. Access Token
  4. Access Token Secret
Make sure the Access Level is “Read and Write”, then click “Create my access Token”

STEP 3: Create your app.js file

  • Create on your Desktop a new folder called twitterbot.
  • Go in that folder twitterbot, and create a new Javascript file with your Text Editor. Name it app.js and save it.

STEP 4: Install the Node Package Manager (npm)

Ok now time to use the Command Line (I didn’t know how to use it before attending this hackathon, it’s surprisingly easy to understand).

  1. Open your Terminal prompt.
  2. You want to go to the Desktop, so type cd Desktop and press enter.
  3. Good, now you want to access to the file you just created, so type cd twitterbot
  4. Now you want to install the Node Package Manager in the twitterbot folder, type npm install twitter in the terminal. (This twitter package will be used to interact with Twitter’s API. In our case, Twitter’s API will let us interact with Twitter via Javascript code).
  5. Wait a little and you should see in your twitterbot folder a file called node_modules.

Cool, you just used the Command Line!

STEP 5: Set up the Twitter Package and Streaming API

To make it simple, it allows you to listen what’s going on Twitter (“data”) and track a keyword ({track:'#keyword'}) to trigger an action.

  1. We want to create an object to allow you to identify and access to the Twitter app. Open yourapp.js file in the text editor and set up your Twitter package: var Twitter = new TwitterPackage(secret);
  2. Now set up the Streaming API, which is really cool and powerful as it lets you listen in real-time what’s happening on Twitter!
Twitter.stream('statuses/filter', {track: '#keyword'}, function(stream) {
stream.on('data', function(tweet) {
console.log(tweet.text);
});

stream.on('error', function(error) {
console.log(error);
});
});

Congrats! your bot could now track any Twitter conversation #keyword and reply something.

Now time to take over and code your own reply.

To run the javascript file (app.js), type node app.js in the terminal and enter. To stop it CTRL+C.

COOKING & ITERATION:

As I said I’m not a Dev, so Bill help me make the magic happen but we brainstormed together.

Playing the devil’s advocate, I realised people could fool the bot by typing positive words knowing the bot will use antonyms. It was also hard to make the bot create meaningful sentences using antonyms library .We didn’t want a “Terminator scenario”…we were stuck!

Discovering twitter-stream-sentiment was my double rainbow

During lunch break, I heard about someone using Azure Stream Analytics, a “sentiment analysis”. What’s that? Basically a tool to detect in real-time “sentiment” in the Twitter streaming data. Powerful sh**! It’s wasn’t free so we looked around to find some open-source tool we could use and found sentiment module.

ITERATION AND FINAL RESULT

So now the idea pivoted, why don’t we analyse the tweet? If it’s a positive message we just retweet it (we amplify the message), if it’s a negative message we reply with a random joke.

I suggested to allocate a value for different degree of positivity (from 0 to 1) but we didn’t got the time. So we went full binary.

Our bot looked like this:

NB: never share your Keys and Tokens access

Hope your bot is still alive!

Was this article useful to you?