I have been working on a Twitter Bot which replies to people with "im" in their message with "Hi ___, I'm Dad!".
I used to have it working before, but then my computer died and lost all of it's files.
I wrote the original bot a couple years ago, and I haven't looked at Tweepy in a while.
I was pretty sure I had it 100% figured out - and no errors popped up, but the bot isn't working and I don't know why.
I can log in just fine, but something is wrong with the actual replying part.
Can someone help me out?
import tweepy as tt
import time
#login credentials twitter account
consumer_key = '-NOT GOING TO PUT THE ACTUAL KEY IN-'
consumer_secret = '-NOT GOING TO PUT THE ACTUAL KEY IN-'
access_token = '-NOT GOING TO PUT THE ACTUAL KEY IN-'
access_secret = '-NOT GOING TO PUT THE ACTUAL KEY IN-'
#login
auth = tt.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
api = tt.API(auth)
search_results = api.search("r'\bi\'?m\s+(.*)',re.IGNORECASE,text", count=100)
user = api.me()
print(user.name)
#reply
for tweet in search_results:
# answer to the hashtag tweet
if len(reply) > 0:
c=message.content
c=c.replace("im ","")
answer="@"+user+" Hi " + c + ", I'm Dad!"
print ("Reply:",answer)
# tweet and pause not to stress twitter
twitter.update_status(status=answer,in_reply_to_status_id=id)
time.sleep(300) #every 5 minutes
if you wish to continue using the r(egular)e(xpression) module, you might want to import re
but as far as I know, and also based on answers of others here on stackoverflow, Twitter doesn't support searching of tweets using RE (as of the moment). I can attest to this, since I'm gathering tweets weekly, and re searches fail.
try using api.update_status( ... )
instead of twitter.update_status( ... )
. take note that update_status()
is a method of the api
object. also, please do note that your id
is uninstantiated or has no initial value.
Here's a snippet from my twitter gathering script for further context, in my implementation, I save rows of tweets in a csv using csvwriter.
for tweet in tweepy.Cursor(api.search, q='twitter').items():
csvWriter.writerow([tweet.text.encode('utf8'),
tweet.user.screen_name, tweet.created_at,
tweet.retweet_count, tweet.favorite_count,
tweet.user.location.encode('utf8')], tweet.user.id)
Like you I also tried using RE a few years back, (and just recently), I could confirm that RE's not yet supported in twitter searches/queries. (Sad I know :/ ). But that's where data / tweet preprocessing comes in.
Another point i'd like to make is, you can't retrieve the total number of replies a tweet gets (i'm assuming that like me you're using a standard api (not premium or enterprise).. see this link for context about the reply_count tweet feature.
for your case, I would suggest the following code to 'search' your intended tweets, I used tweepy's cursor to do an api.search, followed by the q(uery). the query value is basically like the 'search' bar of twitter. although my solution isn't actually your solution, you can pick up from my code if you like.
import tweepy as tt
import time
import sys
reload(sys)
sys.setdefaultencoding('utf8')
#login credentials twitter account
consumer_key = '-NOT GOING TO PUT THE ACTUAL KEY IN-'
consumer_secret = '-NOT GOING TO PUT THE ACTUAL KEY IN-'
access_token = '-NOT GOING TO PUT THE ACTUAL KEY IN-'
access_secret = '-NOT GOING TO PUT THE ACTUAL KEY IN-'
#login
auth = tt.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
api = tt.API(auth)
search_query = "hi I'm Dad"
user = api.me()
print(user.name)
max_tweets = 100
for tweet in tweepy.Cursor(api.search, q=search_query).items(max_tweets):
c=tweet.text.encode('utf8')
c=c.replace("im ","")
answer="@"+tweet.user.screen_name+" Hi " + c + ", I'm Dad!"
print ("Reply:",answer)
api.update_status(status=answer,in_reply_to_status_id=tweet.id)
time.sleep(300) #every 5 minutes
/ogs