I'm a complete noob when it comes to writing bots, so forgive me for my ignorance. I'd like to write a bot that automatically replies to a specific user. Every time they tweet, I want my bot to reply. I know for a fact that it's possible, I just don't know how to do it. I don't even know where to start. I created my Twitter app, I got the token access token codes, but what do I do next? Any help would be greatly appreciated.
I'm assuming at this point you already authenticated into tweepy.
toReply = "someonesTwitterName" #user to get most recent tweet
api = tweepy.API(auth)
#get the most recent tweet from the user
tweets = api.user_timeline(screen_name = toReply, count=1)
for tweet in tweets:
api.update_status("#" + toReply + " This is what I'm replying with", in_reply_to_status_id = tweet.id)
Now if you want to create a bot you will have to check for new tweets every n seconds, if they have a tweet you haven't replied to yet, you would then reply to that tweet using something like this
Related
I am using tweepy for communicating with the twitter API using python.
I am trying to get a users latest tweet using api.user_timeline(id = user_id, exclude_replies=True, count = 1)[0]
But sometimes this returns 0 tweets and sometimes it works thats why I don't know what could be the problem.
Why does this happen?
I want to write bot that searches for tweets after the bot starts. If the tweet has been tweeted before the bot starts working, the bot should ignore it. I have this code right now:
I am using Python and tweepy.
for tweet in tweepy.Cursor(api.mentions_timeline,
since_id=since_id).items():
new_since_id = max(tweet.id, new_since_id)
I don't know how to implement it, I thought about use time function and compare it with the time right now, but I don't know hot to get the time of the tweet.
Twitter's API doesn't allow for searching for tweets by time. You would have to compare the tweet's created_at attribute to the time at which your bot started.
Yesterday I wrote a twitter bot with Python that takes the most recent tweet from Donald Trump, translates it over in Google Translate 45 times, and tweets back at him the final translation in English. Everything works, except the fact that I now need to add some sort of "listener" at the beginning of the code to automatically detect when he tweets so that the rest of the code can do its magic. I've been looking over the internet for some time now and I can't seem to find any sort of event handler that would allow the script to detect when he tweets. So that's why I've come to you guys. Is there any way to use Tweepy or other Python libraries to actively detect when someone tweets? I'll include my code so you guys can see what I want to happen exactly when he does Tweet. It's annotated so hopefully it's not too complicated to understand. Thanks!
import tweepy
from googletrans import Translator
#Keys for accessing the Twitter API
consumer_key = 'PLACEHOLDER'
consumer_secret = 'PLACEHOLDER'
access_token = 'PLACEHOLDER'
access_token_secret = 'PLACEHOLDER'
#Setting up authentification
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
#Scrapes my timeline for DT's latest tweet. I want this to be done AUTOMATICALLY when he tweets.
tweet = api.user_timeline(screen_name = 'realDonaldTrump', count = 1, include_rts = False, tweet_mode = 'extended')
#Translates the text from the .json file that is pulle from the previous line using the Google translate library.
for status in tweet:
translator = Translator()
translation = translator.translate(translation.text, 'mn')
translation = translator.translate(status._json["full_text"], 'ja')
#There are more translations in the actual code, but to reduce the length and complexity, I've taken those out. They don't matter to the specific question.
#Include his handle in a message so that the tweet is tweeted back at him once the translation is complete.
message = ('#realDonaldTrump', translation.text)
#Tweets the message back at him under that specific tweet using it's ID.
send = api.update_status(message, status._json["id"])
I just want the code to be able to scrape my timelines for one of DT's tweets in real time. Thanks!
To Automate your script, you might need to push it into a production server and then create a cron job that will run the script at given regular intervals. To run it locally, I use 1. NGROK - Exposes your localhost addresses and ports to the outside world ie it gives you a way to reach your localhost from the internet, and 2. Invictify - This allows you to run your scripts at a schedule. Also, the script as it is will need a web service to be triggered. Use Flask or FastAPI to create endpoints that call the script when triggered.
I'm using twitter python tools for extracting twitter information. And I'm lost when it comes to a the very simple task of getting tweets from a user.
search_results = twitter_api.search.tweets(q="", user="daguilaraguilar", count=10)
tweets = search_results['statuses']
for tweet in tweets:
print tweet['text']
But I'm getting this error message at last of HTTP 400 response:
details: {"errors":[{"code":25,"message":"Query parameters are missing."}]}
You did not define your query. According to the twitter api, it is required. Looking at the docs for the python api wrapper, I see these steps for getting the tweets of a specific user. You can probably define a since variable also according to the twitter api:
# Get your "home" timeline
t.statuses.home_timeline()
# Get a particular friend's timeline
t.statuses.friends_timeline(id="billybob")
Currently am working on a retweet function using twitter API's. While the feature works fine, but who those tweets that are not retweetable i get this error, "sharing is not permissable for this status (Share validations failed)".
So i need a way to determine if a particular tweet is retweetable or not using the JSON returned by the statuses/:id API. Is it possible??
Based on this i need to place a retweet button only for those tweets that are eligible for retweet.
In original twitter website, i see the following tweets are not retweetable.
My own tweets
Tweets of my followers who had set it to protected.
Please explain more about this to me.
If someone have protected his tweets, it means his tweets can only be seen by his followers. If you retweet these tweets it may break this protection. So twitter do not let you retweet these tweets
If you want to check whether a user have protected his tweets or not, you can try this API:
http://api.twitter.com/version/users/show.format
The result of this API contains a field name "protected", if "protected" is set to true, you can't retweet his tweets
Document is here: https://dev.twitter.com/docs/api/1/get/users/show