twitter bot result

#Tutorial Making a Twitter Bot in Python

I am studying the capabilities of Artificial Intelligence to improve the National Security and Cyber Security. For performing my first test and examen, I am using A.I for growth hacking.

Growth hacking is a process of rapid experimentation across marketing channels and product development to identify the most effective, efficient ways to grow a business.

JOAQUIN PHOENIX as Theodore in the romantic drama "HER," directed by Spike Jonze, a Warner Bros. Pictures release.
JOAQUIN PHOENIX as Theodore in the romantic drama “HER,” directed by Spike Jonze, a Warner Bros. Pictures release.

This new area is directly connected to mass surveillance domain. I am exploring this new field of security so much controverted but really interesting. My first step is the creation of a social botnet on twitter, capable of gathering data about the #cybersecurity tag. So here is a quick tutorial on how to create a TwitterBot.

A Twitterbot is a program that integrates with the Twitter platform, automatically posting, retweeting, liking, or following other users.

My article is referred to the Digital Ocean one. The quotes come from there.

STEP 1: The account.

I create a new account on twitter, just in case it will end to be banned for botting.

It is important to exercise care when creating Twitterbots, as not only are harassment and spamming not OK, but they will cause your Twitterbot’s account to be suspended for violating Twitter’s Terms of Service.

I used a new address email and new phone number to be able to activate the “widget” option in the Twitter settings. It will be needed to use the Twitter API in our program.

We need know to create or app. Digital Ocean did a really good article on how to create your Twitter app. Follow the link or create yours if you know how to do it.

STEP 2: The host.

You setup your twitter account, you have your app. You need now a decent environment to run your bot. First thing, you should use a Linux or Unix based host. Your host needs to be connected and online 24 hours a day.

  1. Use virtual machine on your PC  and let your pc turn on.
  2. Use a raspberry pi.
  3. Use a VPS.

I am using a dedicated VPS for hosting my bots. It’s accessible from everywhere.

If you don’t know how to create a virtual machine, follow this link.
If you don’t know how to setup a Linux host, follow this tutorial.

STEP 3: The bot.

Here is my code of my TwitterBot. It is able to retweet, follow and like if certain conditions are present:

  1. The tweet contains #cybersecurity
  2. The tweet is not a retweet
  3. The user has less than 1000 followers
  4. The tweet doesn’t contain “RT”
  5. The language is English

Bellow the code of the main code: main.py

import tweepy
import tweepy
import time
# Import our Twitter credentials from credentials.py
from conf import consumer_key, consumer_secret, access_token, access_token_secret
# Access and authorize our Twitter credentials from credentials.py
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
# For loop to iterate over tweets with #cybersecurity, limit to 10
try:
    for tweet in tweepy.Cursor(api.search,q='#cybersecurity',lang = "en", include_rts=False).items(10):
        try:
            followers = tweet.user.followers_count
                if followers < 1000 :
                    if "RT" not in tweet.text and not tweet.retweeted:
                        print('Tweet by: @%s' % tweet.user.screen_name)
                        print('Followed by: ~%s' % tweet.user.followers_count)
                        # Favorite the tweet
                        tweet.favorite()
                        # Retweet
                        tweet.retweet()
                        print('Favorited and retweet the tweet')
                        # Follow the user who tweeted
                        tweet.user.follow()
                        print('Followed the user')
                        # Add a timeout for not spamming and alerting twitter.
                        time.sleep(180)
        except Exception as e:
            print e.message
except tweepy.error.TweepError as e:
    print e 
except Exception as e:
    print e

And below the sample for adding your twitter credentials: conf.py

consumer_key = ''
consumer_secret = ''
access_token = ''
access_token_secret = ''

STEP 4: The result.

twitter bot result
Twitter bot results after few days of working.

A few days later, I had 28 followers. I noticed I can convert 10% of the people I am following on followers and without doing any human interaction. It’s really interesting. I also received an email from Twitter telling my account has been probably hacked.

Account locked by Twitter
Account locked by Twitter.

I reset the password and the bot is still running for probably few more days.

Original Article.

Leave a Reply

Your email address will not be published. Required fields are marked *