플러터
Sendbird 메시지 분류 (customType을 사용한 분류)
sam-ss
2024. 3. 6. 22:34
Sendbird SDK를 사용하는데 이미지는 이미지끼리, 문서는 문서끼리 검색해야 해서 찾아보게 되었다.
설명은 Flutter 기준으로 작성했다.
Message Type
Sendbird SDK에는 3가지 타입 메시지가 존재한다.
UserMessage
- 텍스트 메시지
FileMessage
- Binary file 메시지
- 이미지 포함
AdminMessage
- system message로 사용
- 유저가 보내는것이 아니다.
여기서 UserMessage, FileMessage을 사용.
초기 셋팅
메시지 검색을 위해서는 sendbird 홈페이지 에서 Message Search를 켜줘야 한다.
- setting → chat → feature → Message search ON
- ON 하지 않으면 아래 에러를 볼 수 있다.
BadRequestException(400108): Not authorized. "This premium feature is deactivated.
Please contact sales team [sales@sendbird.com](<mailto:sales@sendbird.com>)."
기존 프로젝트 가져와서 사용
- 테스트를 위한것이라 기존의 프로젝트 사용
- https://github.com/sendbird/sendbird-chat-sample-flutter
- 프로젝트에 AppID 적용(AppID는 대시보드에서 확인 가능)
메시지 분류 시도
방법 1
Text로 찾는 방법
파일명이나 이미지명에 특정한 키워드를 추가해 찾을 수 있도록 한다.
- 파일 마지막에 custom prefix를 추가한다.
- ex : image_prefix.png
final query = MessageSearchQuery(keyword: "_prefix")
..channelUrl = channelID
..limit = 10;
문제점
- 파일명을 수정하는 것이라 아름답지 않다.
- 사용자가 특정 prefix를 메시지에 입력하면 예외가 생길 수 있다.
- 만약 파일이 아니라 텍스트 메시지라면 추가적인 코드가 많아지고, 문제가 생길 가능성이 커진다.
방법 2
메시지를 보낼때 extendedMessage, metaArrays추가 정보를 넣어 검색이 가능하도록 한다.
- 보내는 코드
UserMessageCreateParams query = UserMessageCreateParams(
message: textEditingController.value.text,
)
..extendedMessage = {'key': value}
..metaArrays = [
MessageMetaArray(key: 'meta1', value: ['value1']),
MessageMetaArray(key: 'meta2', value: ['value2_1', 'value2_2']),
];
- 받는 코드
// 방법 1
final query = MessageSearchQuery(keyword: "")
..channelUrl = channelID
..targetFields = ["meta1"]
..limit = 10;
// 방법 2
final query = MessageSearchQuery(keyword: "")
..channelUrl = channelID
..limit = 10;
문제점
- 메시지를 어떻게 보내도 값이 들어오지 않는다.
- Flutter 라이브러리 문제인것 같다.
방법 3
메시지를 보낼때 customType에 값을 추가 할 수 있는데 customType을 추가하고, 이 값을 사용해서 검색 한다.
- 보내는 코드('Notes' 라는 customType 추가)
UserMessageCreateParams query = UserMessageCreateParams(
message: textEditingController.value.text,
)..customType = 'Notes';
- 받는 코드(Advanced search에서 Logical operators을 사용)
final query = MessageSearchQuery(keyword: "custom_type:Notes")
..channelUrl = channelID
..advancedQuery = true
..limit = 10;
문서에 있는 예제를 보고 확인한 방법
- https://sendbird.com/docs/chat/platform-api/v3/message/message-search/message-search-overview#2-advanced-search
- (message_text:apple OR custom_type:juice) AND (message_text:orange OR custom_type:salad) 예시가 써있는데 이걸 보고서 설마..custom_type은 이렇게 넣어야 하나? 하고 써봤다.
- customType 으로 검색이 가능한 것 확인
- 예시에는 message_text, custom_type 으로 검색이 가능한데 추가적으로 어떤것이 있는지 못 찾았다.
3번의 방법으로 메시지 분류 하기로 정함