5

I am working on a project where the conversation of a twitter user needs to be retrieved. For example i want to get all the replies of this tweet of BBC World Service. Using the REST API v1.1 i can get the timeline (tweet, re-tweet) of a twitter user. But i did not find any documentation/working work around on fetching replies of a specific tweet. Is there any work around on getting the replies of a specific tweet at all?

hasan3050
  • 195
  • 2
  • 10

2 Answers2

21

There is no API call to get replies to a specific tweet. You can, however, cheat!

Using the Search API you can construct a search query which is:

  • In reply to @bbcworldservice.
  • Occurred after the tweet was posted.
  • Optionally, before a specific date / time.

So, in this case, something like

https://api.twitter.com/1.1/search/tweets.json?
    q=%23bbcworldservice&
    since_id=489366839953489920&
    count=100

You'll get a list of Tweets (up to 100). You will then need to search them for in_reply_to_status_id_str and see if it matches the status you're looking for.

Terence Eden
  • 14,034
  • 3
  • 48
  • 89
  • 1
    This is really solid, and better than anything I could have come up with. – DWRoelands Jul 18 '14 at 14:55
  • As of 2017, the problem with this method is that conversations in twitter can contain tweets where the actual creator of the tweet (the root tweet, that is) is not mentioned. In such cases the `q=%23user` method will only return tweets from the conversation where the `@user` is mentioned which might create potential gaps in the conversation while displaying to the user. But, on the other hand this is still the only way to get replies to a tweet. – Anjan Biswas Nov 14 '17 at 04:06
  • This solution gets messages from the last 7 days.https://twittercommunity.com/t/tweet-conversation-is-not-returning-the-tweets/154235 – Bruno Quaresma May 17 '21 at 18:57
2

The TwitterAPI v2 allows you to retrieve the entire conversation thread using just the conversation_id in search. (In v1.1 you had to write custom code to build it)

Replies to a given Tweet, as well as replies to those replies, are all included in the conversation stemming from the single original Tweet. Regardless of how many reply threads result, they will all share a common conversation_id to the original Tweet that sparked the conversation. Using the Twitter API v2, you have the ability to retrieve and reconstruct an entire conversation thread, so that you can better understand what is being said, and how conversations and ideas evolve.

Example:

curl --request GET \
  --url 'https://api.twitter.com/2/tweets?ids=1225917697675886593&tweet.fields=author_id,conversation_id,created_at,in_reply_to_user_id,referenced_tweets&expansions=author_id,in_reply_to_user_id,referenced_tweets.id&user.fields=name,username' \
  --header 'Authorization: Bearer $BEARER_TOKEN' 

Response will be like

{
    "data": [
        {
            "id": "1225917697675886593",
            "text": "@TwitterEng",
            "created_at": "2020-02-07T23:02:10.000Z",
            "author_id": "2244994945",
            "in_reply_to_user_id": "6844292",
            "conversation_id": "1225912275971657728",
            "referenced_tweets": [
                {
                    "type": "quoted",
                    "id": "1200517737669378053"
                },
                {
                    "type": "replied_to",
                    "id": "1225912275971657728"
                }
            ]
        }
    ],
    "includes": {
        "users": [
            {
                "username": "TwitterDev",
                "name": "Twitter Dev",
                "id": "2244994945"
            },
            {
                "username": "TwitterEng",
                "name": "Twitter Engineering",
                "id": "6844292"
            }
        ],
        "tweets": [
            {
                "id": "1200517737669378053",
                "text": "| ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄|\n             don't push            \n             to prod on            \n               Fridays                  \n|___________| \n(\\__/)  ||\n(•ㅅ•) ||\n/   づ",
                "created_at": "2019-11-29T20:51:47.000Z",
                "author_id": "2244994945",
                "conversation_id": "1200517737669378053"
            },
            {
                "id": "1225912275971657728",
                "text": "Note to self: Don't deploy on Fridays",
                "created_at": "2020-02-07T22:40:37.000Z",
                "author_id": "6844292",
                "conversation_id": "1225912275971657728"
            }
        ]
    }
}

For more info checkout twitter API Conversation

Ojasv singh
  • 474
  • 8
  • 19
  • 1
    Does anyone know why the above request returns only two tweets given there are *hundreds* of replies to this tweet? https://twitter.com/TwitterEng/status/1225912275971657728 – Alex Spurling Jan 15 '21 at 22:12
  • 2
    @AlexSpurling it could be because the api retrieving only the tweets from last week. `The recent search endpoint allows you to programmatically access filtered public Tweets posted over the last week, and is available via the Standard and Academic Research product tracks.` – Mohammad Mustafa May 23 '21 at 19:27