채널 내 메시징
이 페이지에서는 메시지 발송/조회 및 기타 메시지와 관련된 API가 간략하게 기술되어 있습니다. 번역 기능이 필요할 경우아래 내용을 확인하여 주십시오.
1. 채널 내 메시지 목록 조회
채널 내 메시지 목록을 조회할 수 있습니다.
const resp = await client.getMessages({
    channelId: 'my_channel',
    order: 'latest', // default: 'latest'. Use 'oldest' to order by oldest messages first
    limit: 10, // how many messages to fetch, default: 20, max: 50
});
if (resp.hasNext) {
    const lastMessageId = resp.messages[resp.messages.length - 1].id;
    
    const moreResp = await client.getMessages({
        channelId: 'my_channel',
        lastMessageId: lastMessageId,
        limit: 10,
    });
}Response
{
  "messages": [
    {
      "id": "5f7d5e407d138500010000ce",
      "channelId": "my_channel",
      "userId": "user456",
      "username": "user456",
      "profileImageUrl": "http://cdn.test.com/456.jpg",
      "type": "text",
      "text": "Hello",
      "data": {},
      "fileUrl": "http://cdn.test.com/123.jpg",
      "mentions": [],
      "parentMessage": {}, // 회신대상 메시지
      "parentMessageId": "", // 회신대상 메시지 ID
      "reactions": {"happy": ["user1", "user2"]},
      "createdAt": 1583921400
    }
  ],
  "hasNext": false
}2. 채널 내 파일 메시지 목록 조회
채널 내 파일 메시지 목록만 따로 조회가 가능합니다.
const resp = await client.getFileMessages({
    channelId: 'my_channel',
    limit: 10,
});3. 메시지 발송
채널에 메시지를 발송할 수 있습니다.
await client.sendMessage({
    channelId, 
    type: 'text', 
    text: '안녕하세요?', 
    data: {imageUrl: 'http://image.cdn.com/123.jpg'},
    mentions: ['user1', 'user2'] // optional: 특정 사용자 멘션
});const resp = await client.sendMessage({
    channelId, 
    type: 'text', 
    text: '안녕하세요?', 
    data: {imageUrl: 'http://image.cdn.com/123.jpg'},
    translationTargetLanguages: ['en', 'de'],
});
const message = resp.message;
console.log(message.translations);<input type="file" onchange="sendFile(this)" >
<script>
    async function sendFile(input) {
        // 파일 업로드
        await client.sendMessage({
            channelId, 
            type: 'text', 
            text: '안녕하세요?', 
            data: {meta: 'my meta data'},
            file: input.files[0], // 업로드 가능한 최대 파일 사이즈는 15MB입니다.
        });
    }
</script>await client.sendMessage({
    channelId, 
    type: 'custom', 
    fileUrl: 'http://somedomain.com/파일경로.jpeg'
});4. 메시지 조회
단일 메시지를 조회할 수 있습니다.
await client.getMessage({
    channelId, 
    messageId: 'someMessageId',
});5. 메시지 회신
회신 하고자 하는 메시지를 지정할 수 있습니다.
await client.sendMessage({
    channelId, 
    type: 'text', 
    text: '안녕하세요?', 
    parentMessageId: 'anotherMessageIdThatIamReplyingTo',
    data: {imageUrl: 'http://image.cdn.com/123.jpg'},
});6. 메시지 읽음 확인
메시지 읽음 확인 API를 제공합니다.
await client.markAsRead({channelId: 'my_channel'}); 7. 메시지 리액션
메시지 리액션 API를 제공합니다.
// reaction 추가
await client.addMessageReaction({
    channelId: 'my_channel', 
    messageId: 'my_message',
    reaction : 'happy',
});
// reaction 삭제
await client.removeMessageReaction({
    channelId: 'my_channel', 
    messageId: 'my_message',
    reaction : 'happy',
});
// 메시지 객체에 있는 reaction 조회
const msgListResp = await client.getMessages({channelId: 'someChannelId'});
console.log(msgListResp.messages[0].reactions);
/*
{
    "happy": [
        "user1",
        "user2"
    ],
    "sad": [
        "user3"
    ]
}
*/8. 메시지 번역
await client.translateMessage({
    channelId: 'my_channel', 
    messageId: 'target_messageId',
    targetLanguages: ['en', 'de'],
}); 9. 특정 메시지 안 읽은 사용자 수 확인
특정 메시지를 읽지 않은 사용자 수를 확인할 수 있는 기능을 제공합니다.
const channelGetResp = await client.getChannel({channelId: 'demo channel'});
const channelObject = channelGetResp.channel;
const msgResp = await client.getMessages({channelId: 'my channel'});
const messageObject = msgResp.messages[0]; // assume that message array is not empty in this example
const msgUnreadCount = client.getMessageUnreadCount({channel: channelObject, message: messageObject});
console.log(`msgUnreadCount: ${msgUnreadCount}`); // integer result
10. 메시지 삭제
메시지 삭제를 위한 API를 제공합니다.
await client.deleteMessage({channelId: 'my_channel', messageId: 'target_messageId'}); Last updated
