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.
- Just go to developer.twitter.com
- Select a user or create a new one
- Add details (is it personal use or for an organization)
- Explain why do you want to create a bot
- Accept terms of use
Create a Twitter App
Once in a developer account, create a Twitter App. In this step, you will
- Select an App name
- Give an Application description
- Add a website URL
- Explain the purpose of the Application
- 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.
- Import the tweepy package
- Set the authentication credentials
- Create a new tweepy.API object
- 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/
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:
- If you tweet so often, the bot can be banned between 5 to 15 minutes for using the API.
- Automatic replies to keywords are not allowed anymore
Check here some Open-Source projects of Twitter Bots, so you can get inspiration.