Hey guys, Welcome to the part 7 of twitter bot. In the previous article we have learnt how to create the Auto like twitter bot. Now we will learn how to build an useful bot i.e. Retweet bot. When we want to retweet every tweet which contain a string related to something which we want, instead of searching every tweet and retweeting them, we can just use this Retweet bot to do all the work for us.
Code of Retweet twitter bot:
import tweepy auth=tweepy.OAuthHandler(consumer_key,consumer_secret) #enter your keys here auth.set_access_token(access_token,access_token_secret) #enter your keys here api=tweepy.API(auth) #to establish connection public_tweets=api.home_timeline() #assigned our twitter home timeline
information to the variable public_tweets.
search_string="VARUN DEEPAK"#String you want to search numberoftweets=15 #number of tweets you want to retweet
for tweet in tweepy.Cursor(api.search,search_string).items(numberoftweets):
try:
tweet.retweet()
print(" I retweeted the tweet") #print statement for us to know that bot has
retweeted the tweets successfullyexcept tweepy.TweepError as e:
print(e.reason) #exception of tweep error and also prints the error reasonexcept StopIteration:
break #exception for iteration error and breaks the iteration
In the above code, the bot searches for the tweets containing the word "VARUN DEEPAK" and retweets them but it only retweets 15 tweets containing that word as we have mentioned in the code.
This is the end of Twitter Bot. For more interesting articles please visit my profile.
Comments