前提条件開始する前に、以下を用意してください。
- 承認済みの App を持つ 開発者アカウント
- User Access Token (OAuth 2.0 PKCE、
dm.writeおよびdm.readスコープ付き)
1対1メッセージを送信する
1
宛先ユーザーの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 エンドポイントを使ってメディアをアップロードします。
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
}
}
自分が送信したメッセージだけを削除でき、他の参加者が送信したメッセージは削除できません。
必要なスコープ
| Scope | 説明 |
|---|---|
dm.write | メッセージを送信および削除する |
dm.read | 会話を閲覧する (dm.write と併用必須) |
tweet.read | 一部の expansions に必須 |
users.read | ユーザー expansions に必須 |
次のステップ
DM ルックアップ
DM 会話を取得
連携ガイド
基本概念とベストプラクティス
APIリファレンス
エンドポイントの詳細ドキュメント
サンプルコード
実行可能なコード例