Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a Twitter thread with Tweepy on Python

I'm creating a Twitter bot to share information about Covid-19 cases where I live, but I'm trying to organize all the information in a single thread

By "thread" I mean a "Twitter Thread": many tweets created together to make it readable and concise

I'm using Tweepy in Python, but I can't find a way to do that. I can post a single tweet (by using api.update_status), but I can't create a full thread by adding new tweets the the first one.

That's my first StackOverflow question, so I hope it's good enough to be understandable

Thank you

like image 925
Hallsand Avatar asked Mar 03 '23 09:03

Hallsand


1 Answers

I recommend you to have a look at https://developer.twitter.com/en/docs/tweets/post-and-engage/api-reference/post-statuses-update . In Tweepy when you call update_status, it will return a Status object, so it should be a case of doing something like the following logic:

original_tweet = api.update_status(status=question_text)

reply1_tweet = api.update_status(status=reply1_text, 
                                 in_reply_to_status_id=original_tweet.id, 
                                 auto_populate_reply_metadata=True)

reply2_tweet = api.update_status(status=reply2_text, 
                                 in_reply_to_status_id=reply1_tweet.id, 
                                 auto_populate_reply_metadata=True)

The original_tweet variable will hold the reference to the first tweet and as you call 'api.update_status' (named reply1_tweet) for the second time, you need to have as a parameter the id of the post immediately above this one on the thread logic (which in this case is the original_tweet).

The explanation above refers to this specific part in_reply_to_status_id=original_tweet.id.

Well, I don't know if the explanation is clear enough, I hope it helped...

like image 54
PC_paulo Avatar answered Mar 11 '23 07:03

PC_paulo