사전 요구 사항시작하기 전에 다음이 필요합니다:
- 승인된 App이 있는 developer account
- 사용자 Access Token (
dm.write및dm.readscope가 포함된 OAuth 2.0 PKCE)
1:1 메시지 전송
1
수신자의 user ID 확인
메시지를 보낼 사용자의 user 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")
# Send a one-to-one message
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" });
// Send a one-to-one message
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
참가자 정의
그룹에 포함할 모든 사용자(본인 제외)의 user 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")
# Create a group conversation
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" });
// Create a group conversation
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")
# Add message to existing conversation
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" });
// Add message to existing conversation
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")
# Send message with media
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" });
// Send message with media
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")
# Delete a message
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" });
// Delete a message
const response = await client.dm.deleteMessage("1582103724607971332");
console.log(`Deleted: ${response.data?.deleted}`);
{
"data": {
"deleted": true
}
}
본인이 전송한 메시지만 삭제할 수 있으며, 다른 참가자의 메시지는 삭제할 수 없습니다.
필수 scope
OAuth 2.0 PKCE를 사용할 때 access token에 다음 scope가 포함되어야 합니다:| Scope | 설명 |
|---|---|
dm.write | 메시지 전송 및 삭제 |
dm.read | 대화 읽기 (dm.write와 함께 필수) |
tweet.read | 일부 expansion에 필요 |
users.read | 사용자 expansion에 필요 |
다음 단계
DM 조회
DM 대화 조회하기
통합 가이드
핵심 개념 및 모범 사례
API 레퍼런스
전체 endpoint 문서
샘플 코드
동작하는 코드 예제