メインコンテンツへスキップ

X API を使用した会話スレッド

X 上の会話の展開を見てみると、1 件の Post が複数の会話スレッドのきっかけとなり、参加者が増えるにつれて各スレッドの長さや複雑さが増していくことがあります。Posts 間の関係を特定し、会話スレッドを把握する機能は、X API v2 のペイロードおよび検索機能に含まれています。ある Post への投稿(返信)やその返信への返信には、それぞれに conversation_id が設定され、会話を開始した元の Post の Post ID と一致します。 特定の Post への返信と、その返信へのさらに別の返信は、すべて単一の元の Post から派生した同一の会話に含まれます。どれだけ多くの返信スレッドが分岐しても、会話のきっかけとなった元の Post に対して共通の conversation_id を共有します。X API v2 を使用すると、会話スレッド全体を取得・再構築でき、発言内容や会話・アイデアがどのように展開していくかを把握できます。

会話スレッドの例

以下は、返信への返信を含む、5人の異なる人物が参加する会話スレッドの例です。
{
  "data": [
    {
      "conversation_id": "1279944223114900000",
      "in_reply_to_user_id": "1102323333",
      "author_id": "63044444",
      "created_at": "2020-07-06T15:58:10.000Z",
      "id": "1280169177479744444",
      "referenced_tweets": [
        {
          "type": "replied_to",
          "id": "1280155225706433333"
        }
      ],
      "text": "@ThirdPerson333 @OriginalPerson000 3番目の返信への返信!"
    },
    {
      "conversation_id": "1279944223114900000",
      "in_reply_to_user_id": "3001960000",
      "author_id": "1102323333",
      "created_at": "2020-07-06T15:02:44.000Z",
      "id": "1280155225706433333",
      "referenced_tweets": [
        {
          "type": "replied_to",
          "id": "1279944223114900000"
        }
      ],
      "text": "@OriginalPerson000 3番目の返信"
    },
    {
      "conversation_id": "1279944223114900000",
      "in_reply_to_user_id": "3001960000",
      "author_id": "199562222",
      "created_at": "2020-07-06T15:02:36.000Z",
      "id": "1280155190306340864",
      "referenced_tweets": [
        {
          "type": "replied_to",
          "id": "1279944223114900000"
        }
      ],
      "text": "@OriginalPerson000 2番目の返信"
    }
  ],
  "includes": {
    "users": [
      {
        "name": "オリジナルの人",
        "id": "3001960000",
        "username": "OriginalPerson000"
      },
      {
        "name": "1番目の人",
        "id": "179201111",
        "username": "FirstPerson111"
      }
    ]
  },
  "meta": {
    "newest_id": "1280169177479744444",
    "oldest_id": "1279945722494811111",
    "result_count": 4
  }
}
tweet.fields パラメータとしての conversation_id の取得 v2 の endpoint で返されるすべての Posts について conversation_id を取得するには、リクエストパラメータに tweet.fields=conversation_id を追加します。conversation_id は、その会話の返信スレッドを開始した元の Post の Post ID です。同じ返信スレッド内のすべての Posts(過去の返信スレッドから派生して作成されたスレッドを含む)は、同一の conversation_id を持ちます。

conversation_id パラメータを指定したリクエスト

curl --request GET \
  --url 'https://api.x.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 $ACCESS_TOKEN'

応答

{
  "data": [
    {
      "id": "1225917697675886593",
      "text": "@TwitterEng *ahem* https://t.co/aroJHt2zQ1",
      "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             金曜日に本番環境へ            \n             デプロイするな            \n                                  \n|___________| \n(\\__/)",
        "created_at": "2019-11-29T20:51:47.000Z",
        "author_id": "2244994945",
        "conversation_id": "1200517737669378053"
      }
    ]
  }
}

conversation_id をフィルター演算子として使用する

conversation_id は、Recent search における検索 query パラメーターとして、または Filtered stream のルール内の演算子として使用できます。これにより、Filtered stream ではリアルタイムに、または Search Tweets では逆時系列で、会話スレッド全体の Posts が返されます。また、この演算子は Posts counts と併用して、会話内の Posts の件数を取得することもできます。

conversation_id での query リクエスト

curl --request GET \
  --url 'https://api.x.com/2/tweets/search/recent?query=conversation_id:1279940000004973111&tweet.fields=in_reply_to_user_id,author_id,created_at,conversation_id' \
  --header 'Authorization: Bearer $ACCESS_TOKEN'

応答

注: 検索結果のPostsは新しい順(時系列の逆順)で表示されます。
{
  "data": [
    {
      "id": "1280169000000704333",
      "text": "@attributeisland @iterationjoe なんて美しい生き物たち!Happy #seaturtleweek",
      "conversation_id": "1279940000004973111",
      "public_metrics": {
        "retweet_count": 0,
        "reply_count": 0,
        "like_count": 7,
        "quote_count": 0
      }
    }
  ],
  "meta": {
    "newest_id": "1280169000000704333",
    "oldest_id": "1279940000004973111",
    "result_count": 5
  }
}
I