前提条件始める前に、以下が必要です。
- 承認済みの App を持つ developer account
- User Access Token(
dm.writeとdm.readスコープを持つ OAuth 2.0 PKCE)
1 対 1 のメッセージを送信
1
受信者の user ID を取得
メッセージを送りたい相手のユーザー ID が必要です。これは User lookup endpoint から取得できます。
2
メッセージを送信
cURL
curl -X POST "https://api.x.com/2/dm_conversations/with/9876543210/messages" \
-H "Authorization: Bearer $USER_ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d '{"text": "Hello! This is a message from the API."}'
from xdk import Client
client = Client(bearer_token="YOUR_USER_ACCESS_TOKEN")
# 1 対 1 のメッセージを送信
response = client.dm.send_message(
participant_id="9876543210",
text="Hello! This is a message from the API."
)
print(f"Message sent: {response.data.dm_event_id}")
print(f"Conversation: {response.data.dm_conversation_id}")
import { Client } from "@xdevplatform/xdk";
const client = new Client({ accessToken: "YOUR_USER_ACCESS_TOKEN" });
// 1 対 1 のメッセージを送信
const response = await client.dm.sendMessage({
participantId: "9876543210",
text: "Hello! This is a message from the API.",
});
console.log(`Message sent: ${response.data?.dm_event_id}`);
console.log(`Conversation: ${response.data?.dm_conversation_id}`);
3
レスポンスを確認
{
"data": {
"dm_conversation_id": "1234567890-9876543210",
"dm_event_id": "1582103724607971332"
}
}
グループ会話を作成
1
参加者を定義
グループに含めたい全員のユーザー ID を集めます(自分は除きます)。
2
最初のメッセージ付きでグループを作成
cURL
curl -X POST "https://api.x.com/2/dm_conversations" \
-H "Authorization: Bearer $USER_ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"conversation_type": "Group",
"participant_ids": ["944480690", "906948460078698496"],
"message": {"text": "Welcome to our new group!"}
}'
from xdk import Client
client = Client(bearer_token="YOUR_USER_ACCESS_TOKEN")
# グループ会話を作成
response = client.dm.create_conversation(
conversation_type="Group",
participant_ids=["944480690", "906948460078698496"],
message={"text": "Welcome to our new group!"}
)
print(f"Group created: {response.data.dm_conversation_id}")
import { Client } from "@xdevplatform/xdk";
const client = new Client({ accessToken: "YOUR_USER_ACCESS_TOKEN" });
// グループ会話を作成
const response = await client.dm.createConversation({
conversationType: "Group",
participantIds: ["944480690", "906948460078698496"],
message: { text: "Welcome to our new group!" },
});
console.log(`Group created: ${response.data?.dm_conversation_id}`);
3
会話 ID を受け取る
{
"data": {
"dm_conversation_id": "1582103724607971328",
"dm_event_id": "1582103724607971332"
}
}
dm_conversation_id を保存しておきます。既存の会話にメッセージを追加
自分が既に参加している会話にメッセージを送信します。cURL
curl -X POST "https://api.x.com/2/dm_conversations/1582103724607971328/messages" \
-H "Authorization: Bearer $USER_ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d '{"text": "Adding another message to the conversation."}'
from xdk import Client
client = Client(bearer_token="YOUR_USER_ACCESS_TOKEN")
# 既存の会話にメッセージを追加
response = client.dm.send_message_to_conversation(
dm_conversation_id="1582103724607971328",
text="Adding another message to the conversation."
)
print(f"Message sent: {response.data.dm_event_id}")
import { Client } from "@xdevplatform/xdk";
const client = new Client({ accessToken: "YOUR_USER_ACCESS_TOKEN" });
// 既存の会話にメッセージを追加
const response = await client.dm.sendMessageToConversation(
"1582103724607971328",
{ text: "Adding another message to the conversation." }
);
console.log(`Message sent: ${response.data?.dm_event_id}`);
メディア付きメッセージを送信
1
メディアをアップロード
まず、Media Upload endpoint を使用してメディアをアップロードします。
2
メディア添付付きで送信
cURL
curl -X POST "https://api.x.com/2/dm_conversations/with/9876543210/messages" \
-H "Authorization: Bearer $USER_ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"text": "Check out this image!",
"attachments": [{"media_id": "1234567890123456789"}]
}'
from xdk import Client
client = Client(bearer_token="YOUR_USER_ACCESS_TOKEN")
# メディア付きメッセージを送信
response = client.dm.send_message(
participant_id="9876543210",
text="Check out this image!",
attachments=[{"media_id": "1234567890123456789"}]
)
print(f"Message with media sent: {response.data.dm_event_id}")
import { Client } from "@xdevplatform/xdk";
const client = new Client({ accessToken: "YOUR_USER_ACCESS_TOKEN" });
// メディア付きメッセージを送信
const response = await client.dm.sendMessage({
participantId: "9876543210",
text: "Check out this image!",
attachments: [{ mediaId: "1234567890123456789" }],
});
console.log(`Message with media sent: ${response.data?.dm_event_id}`);
メッセージを削除
自分が送信したメッセージを削除します。cURL
curl -X DELETE "https://api.x.com/2/dm_events/1582103724607971332" \
-H "Authorization: Bearer $USER_ACCESS_TOKEN"
from xdk import Client
client = Client(bearer_token="YOUR_USER_ACCESS_TOKEN")
# メッセージを削除
response = client.dm.delete_message("1582103724607971332")
print(f"Deleted: {response.data.deleted}")
import { Client } from "@xdevplatform/xdk";
const client = new Client({ accessToken: "YOUR_USER_ACCESS_TOKEN" });
// メッセージを削除
const response = await client.dm.deleteMessage("1582103724607971332");
console.log(`Deleted: ${response.data?.deleted}`);
{
"data": {
"deleted": true
}
}
削除できるのは自分が送信したメッセージのみで、他の参加者のメッセージは削除できません。
必要なスコープ
OAuth 2.0 PKCE を使用する場合、Access Token には以下のスコープが必要です。| Scope | Description |
|---|---|
dm.write | メッセージの送信と削除 |
dm.read | 会話の読み取り(dm.write と併用が必要) |
tweet.read | 一部の expansions で必要 |
users.read | user expansions で必要 |
次のステップ
DM lookup
DM の会話を取得
統合ガイド
主要な概念とベストプラクティス
API リファレンス
エンドポイントの完全なドキュメント
サンプルコード
動作するコード例