Reply Tweet Automatically Using Loop

Viewed 69

my goal is to reply to a certain tweet using tweepy with some pause (time.sleep()) the reply format is status and the value of i at the end. but my code isn't working and i don't know why. (nb: it only replies once and doesn't print the report)

import tweepy
import random
import datetime
import time


def spam():
    for i in range(amount):
        send = str(status) + " " + str(i+1)
        pause = random.randint(5,12)

        auth = tweepy.OAuthHandler("xxxx", "xxxx")
        auth.set_access_token("xxxx-xxxx", "xxxx")
        api = tweepy.API(auth)
        api.update_status(send, in_reply_to_status_id = tweetid , auto_populate_reply_metadata=True)
        print("succes, pause =",pause+"s\t",datetime.datetime.now()) # for report
        time.sleep(pause)

amount = int(input("amount>> "))
status = input("status>> ")
tweetid = input("id>> ")

spam()
1 Answers

As far as I know, Twitter blocks tweets that are similar to one another. Only one such tweet can go out. Even I'd tried varying the time between tweets and also tried to change a character or two, bit still it got blocked. Nothing wrong with your code, it's Twitter's Defence Mechanism that has kicked in to prevent spam. Also be careful, your API access might also get blocked if you try to spam.

Also you don't need to authenticate every time you run through the loop, you can do it only once and get the api object and you can use it in the loop as below

import tweepy
import random
import datetime
import time


def spam():
  auth = tweepy.OAuthHandler("xxxx", "xxxx")
  auth.set_access_token("xxxx-xxxx", "xxxx")
  api = tweepy.API(auth)
  for i in range(amount):
    send = str(status) + " " + str(i+1)
    pause = random.randint(5,12)
    api.update_status(send, in_reply_to_status_id = tweetid , auto_populate_reply_metadata=True)
    print("succes, pause =",pause+"s\t",datetime.datetime.now()) # for report
    time.sleep(pause)

amount = int(input("amount>> "))
status = input("status>> ")
tweetid = input("id>> ")

spam()
Related