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

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

X 上で会話がどのように展開するかを見ると、1つの Post が複数の会話スレッドを生み出し、参加者が増えるにつれて各スレッドは長さと複雑さを増していきます。Post 間の関係を特定し、会話スレッドを把握する機能は、X API v2 のペイロードおよび検索機能に組み込まれています。ある Post への返信(reply)や、その返信への返信として Post が投稿される場合、各返信には定義された conversation_id が付与され、会話を開始した元の Post の Post ID と一致します。 特定の Post への返信や、それらの返信への返信はすべて、単一の元の Post を起点とする会話に含まれます。どれだけ多くの返信スレッドが派生しても、会話のきっかけとなった元の Post に対応する共通の conversation_id を共有します。X API v2 を使用すると、会話スレッド全体を取得して再構築でき、何が語られ、会話やアイデアがどのように展開していくかを把握できます。

会話スレッドの例

以下は、5人の異なる参加者が関わり、返信への返信が1件含まれている会話スレッドの例です。
{
  "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": "最初の投稿者",
        "id": "179201111",
        "username": "FirstPerson111"
      }
    ]
  },
  "meta": {
    "newest_id": "1280169177479744444",
    "oldest_id": "1279945722494811111",
    "result_count": 4
  }
}
tweet.fields パラメータとしての conversation_id の取得 v2 エンドポイントで返されるすべての Post について conversation_id を取得するには、リクエストパラメータに tweet.fields=conversation_id フィールドを追加します。conversation_id フィールドは常に、その会話の返信スレッドにおける元の Post の Post ID です。以前の返信スレッドから派生した返信スレッドを含め、同じ返信スレッド内のすべての Post は同一の 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             don't push            \n             to prod on            \n               Fridays                  \n|___________| \n(\\__/)",
        "created_at": "2019-11-29T20:51:47.000Z",
        "author_id": "2244994945",
        "conversation_id": "1200517737669378053"
      }
    ]
  }
}

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

conversation_id は、直近検索の検索クエリパラメータとして、または filtered stream のルール内の演算子として使用できます。これにより、filtered stream を通じてリアルタイムで、あるいは search Tweets による逆時系列で、会話スレッド全体の Post が返されます。また、この演算子を Posts counts と併用することで、会話内の Post の件数を取得できます。

conversation_id でのクエリリクエスト

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'

レスポンス

注: 検索したPostの結果は新しい順(逆時系列)で表示されます。
{
  "data": [
    {
      "id": "1280169000000704333",
      "text": "@attributeisland @iterationjoe なんて美しい生き物でしょう!ハッピー #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
  }
}