How do i reply to a certain tweet using Tweepy? - python

I am trying to reply to create a code to reply to simply reply to a (given) tweet, i only know that api.update_status tweets but not how to reply to one, i did some researches but all are outdated and "in_reply_to_status_id" doesn't seem to work anymore
api = tweepy.API(auth)
api.update_status

In order to reply to a tweet using the Twitter API v2, you need to use the in_reply_to_tweet_id. This works as follows.
import tweepy
client = tweepy.Client(consumer_key='YOUR_CONSUMER_KEY',
consumer_secret='YOUR_CONSUMER_SECRET',
access_token='YOUR_ACCESS_TOKEN',
access_token_secret='YOUR_ACCESS_TOKEN_SECRET')
client.create_tweet(text='Some reply', in_reply_to_tweet_id=42)
This is described in the documentation here.
in_reply_to_tweet_id (Optional[Union[int, str]]) – Tweet ID of the Tweet being replied to.
Edit: If you are using API v1.1, then you need to include #username in the status text, where username is the author of the referenced Tweet. This is described here.
Hence, a working example looks as follows.
comment = '#usernameOfTweet Test Reply'
res = api.update_status(comment, in_reply_to_status_id=42)
It is also possible to use auto_populate_reply_metadata=True as discussed in this Github issue.
comment = 'Test reply'
api.update_status(comment, in_reply_to_status_id=42, auto_populate_reply_metadata=True)
This is also described in the documentation.
auto_populate_reply_metadata – If set to true and used with in_reply_to_status_id, leading #mentions will be looked up from the original Tweet, and added to the new Tweet from there.

Related

Get conversation ID with Tweepy

Basically I want to get the conversation_id if the Tweet is a reply to another Tweet. So I can get the list of replies to each other to analyze.
My code:
class Listener(StreamingClient):
def on_response(self, response):
print(response)
listener = Listener(auth['bearer_token'])
listener.sample(expansions=['in_reply_to_user_id'], tweet_fields=['conversation_id'])
When using this, I only get the user_id to which it is replying, but I cannot get any type of conversation_id.
I have a slight feeling I am missing something essential.
From the relevant FAQ section about this in Tweepy's documentation:
If you are simply printing the objects and looking at that output, the string representations of API v2 models/objects only include the default fields that are guaranteed to exist.
The objects themselves still include the relevant data, which you can access as attributes or by subscription.

Tweepy api.get_status().in_reply_to_status_id_str returns None

No matter what tweet I use when I call myTweet.text it returns the text for the tweet I want but when I call myTweet.in_reply_to_status_id_str it returns None even if there are replies to the tweet.
Am I using it wrong? This is the only way I can find online of how to get the replies to a tweet. Thank you in advance for your help, the code:
api = tweepy.API(auth)
myTweet = api.get_status('id')
print(myTweet)
print(myTweet.text)
print('---', myTweet.in_reply_to_status_id_str)
The in_reply_to_status_id_str attribute will contain the string representation of the original Tweet’s ID if the represented Tweet is a reply. See https://developer.twitter.com/en/docs/tweets/data-dictionary/overview/tweet-object.
Twitter's API does not have a direct method/endpoint to retrieve replies to a specific Tweet.
Instead, you'll have to use the search APIs and filter the results manually.

Gmail API: Python Email Dict appears to be Missing Keys

I'm experiencing a strange issue that seems to be inconsistent with google's gmail API:
If you look here, you can see that gmail's representation of an email has keys "snippet" and "id", among others. Here's some code that I use to generate the complete list of all my emails:
response = service.users().messages().list(userId='me').execute()
messageList = []
messageList.extend(response['messages'])
while 'nextPageToken' in response:
pagetoken = response['nextPageToken']
response = service.users().messages().list(userId='me', pageToken=pagetoken).execute()
messageList.extend(response['messages'])
for message in messageList:
if 'snippet' in message:
print(message['snippet'])
else:
print("FALSE")
The code works!... Except for the fact that I get output "FALSE" for every single one of the emails. 'snippet' doesn't exist! However, if I run the same code with "id" instead of snippet, I get a whole bunch of ids!
I decided to just print out the 'message' objects/dicts themselves, and each one only had an "id" and a "threadId", even though the API claims there should be more in the object... What gives?
Thanks for your help!
As #jedwards said in his comment, just because a message 'can' contain all of the fields specified in documentation, doesn't mean it will. 'list' provides the bare minimum amount of information for each message, because it provides a lot of messages and wants to be as lazy as possible. For individual messages that I want to know more about, I'd then use 'messages.get' with the id that I got from 'list'.
Running get for each email in your inbox seems very expensive, but to my knowledge there's no way to run a batch 'get' command.

How to use Tweepy to retweet with a comment

So i am stuck trying to figure out how to retweet a tweet with a comment, this was added to twitter recently.
this is when you click retweet and add a comment to the retweet and retweet it.
basically this is what i am talking about :
i was looking at the api and count find a method dedicated to this. And even the retweet method does not have a parameter where i can pass text.
So i was wondering is there a way to do this?
Tweepy doesn't have functionality to retweet with your own text, but what you can do is make a url like this https://twitter.com/<user_displayname>/status/<tweet_id> and include it with the text you want comment. It's not a retweet but you are embedding the tweet in your new tweet.
user_displayname - display name of person, whose tweet you are retweeting
tweet_id - tweet id of tweet you are retweeting
September 2021 Update
Tweepy does have the functionality to quote retweet. Just provide the url of the tweet you want to quote into attachment_url of the API.update_status method.
Python example:
# Get the tweet you want to quote
tweet_to_quote_url="https://twitter.com/andypiper/status/903615884664725505"
# Quote it in a new status
api.update_status("text", attachment_url=tweet_to_quote_url)
# Done!
In the documentation, there is a quote_tweet_id parameter in create_tweet method.
You can create a new tweet with the tweet ID of the tweet you want to quote.
comment = "Yep!"
quote_tweet = 1592447141720780803
client = tweepy.Client(bearer_token=access_token)
client.create_tweet(text=comment, quote_tweet_id=quote_tweet, user_auth=False)

Reply to Tweet with Tweepy - Python

I can't seem to get tweepy to work with replying to a specific tweet:
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)
### at this point I've grabbed the tweet and loaded it to JSON...
tweetId = tweet['results'][0]['id']
api.update_status('My status update',tweetId)
The api says it takes optional parameters and in_reply_to_status_id is the first, but it seems to be ignoring it altogether. This script will post an updated status, but it does not link it as a reply to the tweetId that I'm passing.
API for reference: http://code.google.com/p/tweepy/wiki/APIReference#update_status
Anyone have any ideas? I feel like I'm missing something simple here...
Thanks in advance.
Just posting the solution so no someone else suffers the way I did.
Twitter updated the API and added an option named auto_populate_reply_metadata
All you need to do is set that to true, and the leave the rest as should be. Here is a sample:
api.update_status(status = 'your reply', in_reply_to_status_id = tweetid , auto_populate_reply_metadata=True)
Also, the status_id is the long set of digits at the end of the tweet URL. Good Luck!
I ran into the same problem, but luckily I found the solution. You just need to include the user's screen_name in the tweet:
api.update_status('#<username> My status update', tweetId)
You can also do
api.update_status("my update", in_reply_to_status_id = tweetid)
Well then, it was something simple. I had to specify who the tweet was directed towards using the # notation.
api.update_status('My status update #whoIReplyTo',tweetId)
I discovered that I had to include the tweet's ID string (rather than actual ID number) when specifying the tweet that I was replying to
api.update_status('#whoIReplyTo my reply tweet',tweetIdString)
This seems to be a bug in Tweepy – even if you make a call to api.update_status with the correct parameters set,
api.update_status(status=your_status, in_reply_to_status=tweet_to_reply_to.id)
the tweet will not be a reply. In order to get a reply, you need to mention the user you want to reply to AND specify the correct in_reply_to_status id.
reply_status = "#%s %s" % (username_to_reply_to, your_status)
api.update_status(status=reply_status, in_reply_to_status=tweet_to_reply_to.id)
Keep in mind though – Tweepy and Twitter's servers still enforce a maximum number of 140 characters, so make sure you check that
len(reply_status) <= 140
Again, I think this is a bug because on the Twitter app, you can reply without embedding the username of the person to whom you're replying.
reply_status = "#%s %s" % (tweet.user.screen_name, "type your reply here")
api.update_status(status=reply_status, in_reply_to_status_id=tweet.id)
this is the last correct form, I just test it a few minutes ago

Categories

Resources