In this article we are going to build a twitter bot using twitter’s API and python with tweepy library. A bot which can do following things.
- Follow users who following you
- Follow Users based on Search
- Retweet Tweets based on search
- Favorite tweets based on search
So first of all if we need to use twitter api then we have to create app in twitter. you can follow below mentioned steps and easily create our app in twitter
Ref : https://iag.me/socialmedia/how-to-create-a-twitter-app-in-8-easy-steps/
Once your app is created you will get below mentioned keys which you need to keep a note of it.
- Consumer Key
- Consumer Secret
- OAuth Access Token
- OAuth Access Token Secret
We are going to use these keys while making our tweebot to authenticate with our twitter account. So let’s start with installation of our required libraries.
# install tweepy pip install tweepy
We have installed tweepy library which can help us to connect our python code with twitter account and get information from account. So Let’s start with our python code now.
import tweepy # create function to follow back def followback(noofusers): for follower in tweepy.Cursor(api.followers).items(noofusers): follower.follow() print("Followed %s followers" % (noofusers))
This followback is function which takes no of users as argument and it allows user to follow their followers back.
# follow based on search term def follow(search, nooftweets): for tweet in tweepy.Cursor(api.search, search).items(nooftweets): try: #follow tweet.user.follow() print("--------------------------") print(tweet.user.name) print("--------------------------") except tweepy.TweepError as e: print(e.reason) except StopIteration: break
This follow function allow users to follow based on particular search term (ex. if i want to follow people who tweets for #machinelearning )
# retweet allow us to retweet tweets based on search term def Retweet(search, nooftweets): for tweet in tweepy.Cursor(api.search, search).items(nooftweets): try: #Retweet tweet.retweet() print("--------------------------") print("Retweeted tweet") print("--------------------------") except tweepy.TweepError as e: print(e.reason) except StopIteration: break
This Retweet function allows users to retweet based on particular search terms.
# mark tweets as facvourite based on search term def MarkFav(search, nooftweets): for tweet in tweepy.Cursor(api.search, search).items(nooftweets): try: #favourtie tweet.favorite() print("--------------------------") print("favorite tweet") print("--------------------------") except tweepy.TweepError as e: print(e.reason) except StopIteration: break
This MarkFav function allows users to mark tweets as favorite based on their search terms. (ex. i want to mark tweets as favorite which has #python term)
Now we are all set with functions for our functionality, Let’s now add authentication code which allow us to connect with twitter account.
# Get your keys and token from apps.twitter.com. Make app and get keys consumer_key = '' consumer_secret = '' access_token = '' access_token_secret = '' auth = tweepy.OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret) # make object of API api = tweepy.API(auth) # get user details user = api.me() # print users information print("Welcome "+user.name + " From " + user.location)
This code includes authentication with twitter using keys which we get from apps.twitter.com and get current logged in users details like user.name and user.location to verify if our authentication works perfect.
Let’s make starting point for our code which can allow users to select their options and no of users as argument and provide suitable results.
print("Please select any option from below") print(""" 0. Follow users who following you 1. Follow Users based on Search 2. Retweet Tweets based on search 3. Favourite tweets based on search """) val = input("Enter your Select ? ex.1 \n") if val == "0": nooffollower = input("Enter no of users to follow ?\n") followback(int(nooffollower)) elif val == "1": nooftweets = input("Enter no of tweets ?\n") search = input("Enter search term ?\n") follow(search, int(nooftweets)) elif val == "2": nooftweets = input("Enter no of tweets ?\n") search = input("Enter search term ?\n") Retweet(search, int(nooftweets)) elif val == "3": nooftweets = input("Enter no of tweets ?\n") search = input("Enter search term ?\n") MarkFav(search, int(nooftweets)) print("\n\n Thank you for using Tweebot !")
That’s All. Our bot is ready to run. save code in one tweebot.py file and run as per below command
python tweebot.py
Full code can be found on mentioned github repository https://github.com/akashsenta13/TweeBot