Create Your First Twitter Bot Using Python 2022

Twitter Bot are awesome tools to help track trends, retweet keywords, and follow and like a post that meets our criteria.

Today we will be learning how to create a Bot using tweepy module and Twitter API.

The first thing we must do is go to https://developer.twitter.com/ and create an account.

After creating an account, we request an API; make sure you give a good reason that does not go against twitter’s rules.

If your application is successful, you will be furnished with an API, Private key, Access Token, and Secret Key.

Once you have these keys, open your favorite editor, and let’s start building.

Create a file called Bot.py and write the following; please keep in mind that python is critical about indentation.

import tweepy

API_KEY = ‘ ’
API_SECRET_KEY = ‘ ’
ACCESS_TOKEN = ‘ ’
ACCESS_SECRET_TOKEN = ‘ ’

We import the tweepy library, which you can read more about in the documentation here.

We create variables for our keys which we got from Twitter, and fill them with our keys.

auth = tweepy.OAuthHandler(API_KEY, API_SECRET_KEY0)
auth.set_access_token(ACCESS_TOKEN, ACCESS_SECRET_TOKEN)
api = tweepy.API(auth)

The following lines above tell twitter where and how to authentic our access and how to treat such GetRequest

The last line tells Twitter our auth protocol, which is standard practice for all APIs.

FILE = “id.text”

def retrieve_id(file):
____f_read = open(file. ‘r’)
____last_seen_id = int(f_read.read().strip())
____f_read.close()
____return last_seen_id

def store_id(id, file):
____f_write = open(file, “w”)
____f_write.write(str(id))
____f_write.close()
____return

The first function we called is the retrieve function, this is to tell our Bot to pick the Id of every post that we find interesting or meets our criteria we call an argument file in the function.

This opens the Id of every account that meets our criteria; take the Id of the account, stores it

The second function stores the ID in a string on a new file called FILE which we created above.

last_seen_id = retrieve_id(FILE)
mentions = api.mentions_timeline(last_seen_id, tweet_mode = “extended”)

This line takes the saved id in our file, which we used in the next line to use a function from tweepy called api.mention_timeline, we pass last_seen_id into the argument as this helps keep updated on who is tweeting; extended means to show the whole body of the tweet.

for mentions = api.mentions_timeline(mentions):
____if “solidity”,”ETHDEV”,”Reactjs” in mention.full_text:
____store_id(last_seen_id, FILE)
____api.update_status(‘@’+mention.user.screen_name + ‘The future is here learning is important’ , mentions)
____#print(‘replied to @’ + mention.user.screen_name)

This is the fun part, this point, we say of the for which is a declarative function in the variable we created, we say if the following keywords are found in the tweet, we should comment the following “The future is here, learning is important”

This therefore will comment the above on all tweet that has the following keywords “Solidity”, “ETHDEV”, “Reactjs”.

Awesome right!!!

We just created our first Bot and it is ready to rumble, try it and tell me what you think in the comment below.

you can find the complete repo on Github here

Leave a Comment

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