사전 준비 사항시작하기 전에 다음이 필요합니다.
- 승인된 App이 있는 개발자 계정
dm.write및dm.readscope를 가진 User Access Token (OAuth 2.0 PKCE)
일대일 메시지 보내기
1
받는 사람의 사용자 ID 가져오기
메시지를 보내려는 사용자의 user ID가 필요합니다. 이 ID는 사용자 조회 엔드포인트에서 확인할 수 있습니다.
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")
# 일대일 메시지 보내기
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" });
// 일대일 메시지 보내기
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
}
}
본인이 보낸 메시지만 삭제할 수 있으며, 다른 참가자가 보낸 메시지는 삭제할 수 없습니다.
필요한 scope
| Scope | 설명 |
|---|---|
dm.write | 메시지 전송 및 삭제 |
dm.read | 대화 읽기 (dm.write와 함께 필요) |
tweet.read | 일부 expansions에 필요 |
users.read | 사용자 expansions에 필요 |
다음 단계
DM 조회
DM 대화 조회
통합 가이드
핵심 개념과 모범 사례
API 참조 문서
전체 엔드포인트 문서
샘플 코드
동작하는 코드 예제