Skip to content

Commit 45033d2

Browse files
authored
Merge pull request #56 from BusanVibe/dev
챗봇&게스트 수정
2 parents 396ccdb + 65487a5 commit 45033d2

File tree

10 files changed

+114
-19
lines changed

10 files changed

+114
-19
lines changed

src/main/kotlin/busanVibe/busan/domain/chat/controller/ChatRestController.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,12 @@ class ChatRestController(
2828
채팅 전송 API 입니다. 해당 API 호출 시 메시지가 전송되고, 채팅 웹소켓에 연결된 유저들에게 메시지가 전송됩니다.
2929
메시지의 길이는 최대 200글자입니다.
3030
31-
type : ['CHAT', 'BOT']
31+
type : ['CHAT', 'BOT_RESPONSE', 'BOT_REQUEST']
3232
3333
메시지가 '/' 로 시작하면 챗봇 답변을 응답합니다.
3434
일반 채팅은 웹소켓으로 메시지를 전송하고, 챗봇은 웹소켓 메시지를 전송하지 않습니다.
3535
따라서 일반 채팅은 웹소켓으로 받은 메시지로 활용하고, 챗봇에게 받은 답변은 해당 API의 응답 결과를 활용해주세요.
36+
3637
""")
3738
fun sendMessage(@Valid @RequestBody chatMessage: ChatMessageSendDTO): ApiResponse<ChatMessageReceiveDTO> {
3839
val receiveDTO = chatMongoService.saveAndPublish(chatMessage)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package busanVibe.busan.domain.chat.enums
22

33
enum class MessageType {
4-
CHAT, BOT
4+
CHAT, BOT_REQUEST, BOT_RESPONSE
55
}

src/main/kotlin/busanVibe/busan/domain/chat/service/ChatMongoService.kt

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import busanVibe.busan.global.apiPayload.code.status.ErrorStatus
1313
import busanVibe.busan.global.apiPayload.exception.GeneralException
1414
import org.slf4j.Logger
1515
import org.slf4j.LoggerFactory
16+
import org.springframework.beans.factory.annotation.Value
1617
import org.springframework.data.domain.Pageable
1718
import org.springframework.data.redis.listener.ChannelTopic
1819
import org.springframework.stereotype.Service
@@ -24,7 +25,9 @@ class ChatMongoService(
2425
private val redisPublisher: RedisPublisher,
2526
private val topic: ChannelTopic,
2627
private val userRepository: UserRepository,
27-
private val openAiService: OpenAiService
28+
private val openAiService: OpenAiService,
29+
@Value("\${image.chat-bot}")
30+
private val chatBotImage: String
2831
) {
2932

3033
val log: Logger = LoggerFactory.getLogger(ChatMongoService::class.java)
@@ -46,9 +49,9 @@ class ChatMongoService(
4649
val message = chatMessage.message.trim()
4750

4851
// 받은 메세지로 타입 구분
49-
// '/'로 시작하면 챗봇(BOT)
52+
// 일반 메시지는 CHAT, 챗봇 요청은 BOT_REQUEST
5053
val type: MessageType = if(message[0] == '/'){
51-
MessageType.BOT
54+
MessageType.BOT_REQUEST
5255
}else{
5356
MessageType.CHAT
5457
}
@@ -64,16 +67,32 @@ class ChatMongoService(
6467
time = now
6568
)
6669

67-
// 일반 채팅일 경우 채팅 기록 저장
70+
// 사용자가 보낸 메시지 저장
6871
chatMongoRepository.save(chat)
6972

70-
// 유저들에게 웹소켓으로 전달할 메시지의 DTO 생성
73+
// 유저들에게 웹소켓으로 전달할 메시지의 DTO 생성 - CHAT
7174
val receiveDto = buildReceiveDto(type, currentUser, chat, now)
7275

7376
// 일반 채팅일 경우에만 유저들에게 웹소켓 메시지 보냄
74-
if(type == MessageType.CHAT) {
77+
if(receiveDto.type == MessageType.CHAT) {
7578
redisPublisher.publish(topic, receiveDto)
7679
}
80+
81+
// OpenAI 챗봇 요청
82+
if (receiveDto.type == MessageType.BOT_RESPONSE) {
83+
// openai 요청
84+
openAiService.chatToOpenAI(chat.message)
85+
// 챗봇 대답 저장
86+
chatMongoRepository.save(
87+
ChatMessage(
88+
type = MessageType.BOT_RESPONSE,
89+
userId = currentUser.id,
90+
message = receiveDto.message,
91+
time = chat.time
92+
)
93+
)
94+
}
95+
7796
return receiveDto
7897
}
7998

@@ -97,11 +116,11 @@ class ChatMongoService(
97116
val message = openAiService.chatToOpenAI(chat.message)
98117
ChatMessageReceiveDTO(
99118
name = "챗봇",
100-
imageUrl = null,
119+
imageUrl = chatBotImage,
101120
message = message,
102121
time = timestamp,
103-
type = type,
104-
userId = -1
122+
type = MessageType.BOT_RESPONSE,
123+
userId = currentUser.id
105124
)
106125
}
107126
}

src/main/kotlin/busanVibe/busan/domain/common/BaseEntity.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ import lombok.Getter
77
import org.springframework.data.annotation.CreatedDate
88
import org.springframework.data.annotation.LastModifiedDate
99
import org.springframework.data.jpa.domain.support.AuditingEntityListener
10-
import java.time.Instant
1110
import java.time.LocalDateTime
12-
import java.time.ZoneId
1311

1412
@EntityListeners(AuditingEntityListener::class)
1513
@MappedSuperclass

src/main/kotlin/busanVibe/busan/domain/user/service/UserCommandService.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import busanVibe.busan.global.apiPayload.code.status.ErrorStatus
1212
import busanVibe.busan.global.apiPayload.exception.GeneralException
1313
import busanVibe.busan.global.apiPayload.exception.handler.ExceptionHandler
1414
import busanVibe.busan.global.config.security.JwtTokenProvider
15-
import io.lettuce.core.KillArgs.Builder.user
1615
import org.slf4j.LoggerFactory
1716
import org.springframework.beans.factory.annotation.Qualifier
1817
import org.springframework.beans.factory.annotation.Value
@@ -35,7 +34,10 @@ class UserCommandService(
3534
private val userRepository: UserRepository,
3635
private val userConverter: UserConverter,
3736
private val jwtTokenProvider: JwtTokenProvider,
38-
private val passwordEncoder: PasswordEncoder
37+
private val passwordEncoder: PasswordEncoder,
38+
@Value("\${image.guest}")
39+
private val guestImage: String
40+
3941
) {
4042

4143
@Value("\${spring.kakao.client-id}")
@@ -49,8 +51,8 @@ class UserCommandService(
4951
fun guestLogin(): UserLoginResponseDTO.LoginDto {
5052

5153
val email = UUID.randomUUID().toString().substring(0,7) + "@busanvibe.com"
52-
val nickname = UUID.randomUUID().toString().substring(0, 7)
53-
val profileImageUrl: String? = null
54+
val nickname = "guest" + UUID.randomUUID().toString().substring(0, 4)
55+
val profileImageUrl: String? = guestImage
5456

5557
return isNewUser(email, nickname, profileImageUrl, LoginType.GUEST)
5658
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package busanVibe.busan.global.config.mongo
2+
3+
import org.springframework.core.convert.converter.Converter
4+
import org.springframework.data.convert.ReadingConverter
5+
import org.springframework.stereotype.Component
6+
import java.time.LocalDateTime
7+
import java.time.ZoneId
8+
import java.util.Date
9+
10+
@Component
11+
@ReadingConverter
12+
class DateToLocalDateTimeKstConverter: Converter<Date, LocalDateTime> {
13+
14+
override fun convert(source: Date): LocalDateTime =
15+
source.toInstant().atZone(ZoneId.of("Asia/Seoul")).toLocalDateTime()
16+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package busanVibe.busan.global.config.mongo
2+
3+
import org.springframework.core.convert.converter.Converter
4+
import org.springframework.data.convert.WritingConverter
5+
import org.springframework.stereotype.Component
6+
import java.sql.Timestamp
7+
import java.time.LocalDateTime
8+
import java.time.ZoneId
9+
import java.util.Date
10+
11+
@Component
12+
@WritingConverter
13+
class LocalDateTimeToDateKstConverter: Converter<LocalDateTime, Date> {
14+
15+
override fun convert(source: LocalDateTime): Date =
16+
Date.from(source.atZone(ZoneId.of("Asia/Seoul")).toInstant())
17+
18+
}

src/main/kotlin/busanVibe/busan/global/config/mongo/MongoConfig.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import org.springframework.boot.autoconfigure.mongo.MongoProperties
66
import org.springframework.context.annotation.Bean
77
import org.springframework.context.annotation.Configuration
88
import org.springframework.data.mongodb.core.MongoTemplate
9+
import org.springframework.data.mongodb.core.convert.MongoCustomConversions
910
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories
1011

1112
@Configuration
@@ -24,4 +25,15 @@ class MongoConfig(
2425
return MongoTemplate(mongoClient(), mongoProperties.database)
2526
}
2627

28+
@Bean
29+
fun customConversions(
30+
localDateTimeConverter: LocalDateTimeToDateKstConverter,
31+
dateToLocalDateTimeConverter: DateToLocalDateTimeKstConverter,
32+
) = MongoCustomConversions (
33+
listOf(
34+
localDateTimeConverter,
35+
dateToLocalDateTimeConverter
36+
)
37+
)
38+
2739
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package busanVibe.busan.global.config.mongo
2+
3+
import org.springframework.context.annotation.Bean
4+
import org.springframework.context.annotation.Configuration
5+
import org.springframework.core.convert.converter.Converter
6+
import org.springframework.data.mongodb.config.AbstractMongoClientConfiguration
7+
import org.springframework.data.mongodb.core.convert.MongoCustomConversions
8+
import java.time.LocalDateTime
9+
import java.time.ZoneId
10+
import java.util.Date
11+
12+
//@Configuration
13+
//class MongoTimeConfig: AbstractMongoClientConfiguration() {
14+
//
15+
// @Bean
16+
// override fun customConversions(): MongoCustomConversions {
17+
// return MongoCustomConversions(
18+
// listOf(
19+
// Converter<LocalDateTime, Date> { Date.from(it.atZone(ZoneId.of("Asia/Seoul")).toInstant()) },
20+
// Converter<Date, LocalDateTime> { it.toInstant().atZone(ZoneId.of("Asia/Seoul")).toLocalDateTime() }
21+
// )
22+
// )
23+
// }
24+
//
25+
// override fun getDatabaseName(): String {
26+
// TODO("Not yet implemented")
27+
// }
28+
//}

src/main/resources/application.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,6 @@ openai:
6464
tourAPI:
6565
key: ${TOUR_API_KEY}
6666

67-
68-
67+
image:
68+
chat-bot: ${IMAGE_CHATBOT}
69+
guest: ${IMAGE_GUEST}

0 commit comments

Comments
 (0)