Python Twitter Bot

buzonliao
Python 101
Published in
2 min readOct 13, 2023
Photo by Microsoft Bing Image Creator

Boost your Twitter presence and engagement with this Python Twitter bot script. You can increase your social media impact by following your followers back and liking tweets related to a specific keyword. Please note that you’ll need to install ‘tweepy’ and create a Twitter API to make this script work. Run it on your local machine for optimal results.

Key Features:

  1. Follow Your Followers: The script encourages you to reciprocate by following back your followers, creating a sense of community on your Twitter account.
  2. Like Searched Tweets: Stay engaged with your Twitter community by liking tweets related to a particular keyword. This boosts your engagement and keeps you updated on the latest trends in your niche.

Twitter Bot

  • Follow your followers back
  • Like the searched tweets
#You will need to PIP INSTALL tweepy for this to work and also create a twitter API. Run this on your own machine, not in this Repl. 
import tweepy
import time

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)
api = tweepy.API(auth)

user = api.me()
print (user.name) #prints your name.
print (user.screen_name)
print (user.followers_count)

search = "python"
numberOfTweets = 2

def limit_handle(cursor):
while True:
try:
yield cursor.next()
except tweepy.RateLimitError:
time.sleep(1000)

#Be nice to your followers. Follow everyone!
for follower in limit_handle(tweepy.Cursor(api.followers).items()):
if follower.name == 'Usernamehere':
print(follower.name)
follower.follow()


# Be a narcisist and love your own tweets. or retweet anything with a keyword!
for tweet in tweepy.Cursor(api.search, search).items(numberOfTweets):
try:
tweet.favorite()
print('I like that')
except tweepy.TweepError as e:
print(e.reason)
except StopIteration:
break

Reference

https://docs.tweepy.org/en/stable/

--

--