Two Ways to Make a Twitter Bot

Twitter Bots can be useful, funny, or annoying. And they can be coded in Ruby or Python. Choice your language.

In recent years, bots have become controversial on Twitter. They’re pointed out as responsible for creating fake support to political groups and manipulated trends in this social network. In other cases, bots are used to spread spam. 

If a user wants to be anonymous or doesn’t want to share its profile photo, also it’s pointed out as a bot. However, bots can have other purposes and be useful or funny accounts. 

Some ideas for creating a bot

Yes, spam and harassment are not the only or best use for bots. Here, some ideas that you can pick for making your own bot on Twitter.

Users can ask to @WhatTheFare how much will a taxi fare be, mixing up Uber and Twitter API (unfortunately, this project is not working anymore).

Other bots can be highly creative as @Mothgenerator which tweets an imaginary moth created with JavaScript.

And other bots share public images took by satellites as @discovr_epic.

Setting your Twitter account as a developer

The first step for making a bot is setting an account as a developer. 

  1. Just go to developer.twitter.com 
  2. Select a user or create a new one
  3. Add details (is it personal use or for an organization)
  4. Explain why do you want to create a bot
  5. Accept terms of use

Create a Twitter App

Once in a developer account, create a Twitter App. In this step, you will

  1. Select an App name
  2. Give an Application description
  3. Add a website URL
  4. Explain the purpose of the Application
  5. Also, you can add other non-mandatory details such as privacy policy URL, call-back URL, and terms of service URL.

Now, you can choose a development environment for creating a Twitter bot. Here, three options.

 First way: Twitter Bot with Tweepy

Tweepy is a Python package with a set of classes and methods that represents Twitter’s models and API endpoints. Install Tweepy with Python package manager PIP.

  1. Import the tweepy package
  2. Set the authentication credentials
  3. Create a new tweepy.API object
  4. Use the api object to call the Twitter API
import tweepy

# Authenticate to Twitter
auth = tweepy.OAuthHandler("CONSUMER_KEY", "CONSUMER_SECRET")
auth.set_access_token("ACCESS_TOKEN", "ACCESS_TOKEN_SECRET")

# Create API object
api = tweepy.API(auth)

# Create a tweet
api.update_status("Hello Tweepy")

Check how to use Tweepy here

https://realpython.com/twitter-bot-python-tweepy/import tweepy

# Authenticate to Twitter
auth = tweepy.OAuthHandler("CONSUMER_KEY", "CONSUMER_SECRET")
auth.set_access_token("ACCESS_TOKEN", "ACCESS_TOKEN_SECRET")

# Create API object
api = tweepy.API(auth)

# Create a tweet
api.update_status("Hello Tweepy")

Check how to use Tweepy here

https://realpython.com/twitter-bot-python-tweepy/

Check how to use Tweepy here

Second way: Gem Twitter in Ruby

In Ruby, you can use the gem Twitter for creating a bot.

gem install twitter

require 'twitter'

client = Twitter::REST::Client.new do |config|
 config.consumer_key = "YOUR_CONSUMER_KEY"
 config.consumer_secret = "YOUR_CONSUMER_SECRET"
 config.access_token = "YOUR_ACCESS_TOKEN"
 config.access_token_secret = "YOUR_ACCESS_SECRET"
end

Check the documentation of the Twitter gem here

Deploy a Twitter bot

Once you have written the code of your bot, you can deploy it in a cloud server, like Docker, Heroku, or AWS. There some rules that you should know:

  1. If you tweet so often, the bot can be banned between 5 to 15 minutes for using the API.
  2. Automatic replies to keywords are not allowed anymore

Check here some Open-Source projects of Twitter Bots, so you can get inspiration.