Creating Twitter Bot Using Tweepy

27 July 2020

Image Source: sproutsocial.com

Automating our daily boring tasks is one thing and automating something that you love doing is altogether different thing. Same task I tried to accomplish a task that I love i.e. is being active on social media. Yes I tried automating my activities on twitter by using a popular python library named Tweepy.

Tweepy is a very interesting python library which makes interacting with python developer API's very easy and hides many low level details that can be overwhelming for a beginner who is just starting to use REST API.

Image Source: morioh.com

To get things started first we need to create a developer account with twitter and then you can create an app which will provide following credentials that we have to use while creating bot.

These credentials should be kept secret and should not be shared in any way, its a common mistake to put these details in source code and pushing it to github so these should be used only from environment variables.

e.g.

And just one more thing to get started is that we need to install tweepy library via pip

e.g.

pip3 install tweepy

To get this done quickly you can use my sample bot from github, here is the link.

This bot is currently programmed to like and retweet tweets from specific accounts and this bot also filters tweets based on specific filters real time.

Components used in this bot

There are not many components in this bot but it is extensible enough to be customized for different purposes.

1. Creating auth config for Twitter API

For creating a authenticated connection to twitter API using Tweepy required all four secret values that we get from twitter developer account.

This handles creating OAuth connection with twitter API and also sets rate limit alerts and notifications, twitter has some set API usage limit and if we pass that limit then out api handler waits for that time and then again starts making calls to twitter API.

2. Creating utility functions.

Some utility functions should be created for basic use of this bot , these may include

And this list can be endless as these are infinite use cases of twitter api and using these utility functions we can regulate our bot to trigger some functions based in the results of these functions.

e.g. suppose if we are planning to make a feature so that if a user retweets out tweet then we automatically follow that user, like this we can make multiple features and run them periodically.

3. Stream live data from twitter.

We can use Teeepy's StreamListerner object to stream and filter live data from twitter news feed and based on that we can instruct out bot to take some decisions. This data can also be used to do live analysis to twitter data.

So like this we can create stream object and filter data based on some targeted keywords, what this stream listener is doing is that whenever there is any tweet including a keyword Hacking then it will automatically like and retweet that tweet automatically.

This is just a sample of the capabilities of a twitter bot and much more can be achieved by going through all features of this API. My twitter-bot project is also in phase-1 as I am writing this blog. 

Happy Hacking !!!