diff --git a/src/main/kotlin/kr/lunaticbum/back/lun/controllers/Telegram.kt b/src/main/kotlin/kr/lunaticbum/back/lun/controllers/Telegram.kt index 6fa5f47..6c06ba7 100644 --- a/src/main/kotlin/kr/lunaticbum/back/lun/controllers/Telegram.kt +++ b/src/main/kotlin/kr/lunaticbum/back/lun/controllers/Telegram.kt @@ -117,59 +117,101 @@ class Telegram { } } } - if (msg.text?.contains(" ") == true) { - - } else { - val req = BumlamaReq(msg.text) - CoroutineScope(Dispatchers.IO).launch { - val fullUrl = - "https://api.telegram.org/${globalEvv.telegramBotKey}/sendMessage?chat_id=${globalEvv.telegramMyId}&text=lama 에게 전송 => ${req.reqMsg}" - logService.log("fullUrl >>> ${fullUrl}") - WebClient.create().get() - .uri(fullUrl) - .retrieve() - .bodyToMono(String::class.java).block() - } - CoroutineScope(Dispatchers.IO).launch { - try { - if (req.reqMsg?.contains("검색") == true) { - val gSearch = "https://www.googleapis.com/customsearch/v1?key=AIzaSyARLXyvmr_554tOy3UCh3naFlZQS3-qQQM&cx=207f328d3ad7242f2&q=${req.reqMsg}&num=5&lr=kr" - WebClient.create().post() - .uri(gSearch) - .body(BodyInserters.fromValue(Gson().toJson(req))) - .retrieve() - .bodyToMono(String::class.java).timeout(Duration.ofSeconds(6000L)).block()?.let { result -> - - } - - } else { - val client = WebClient.create() - client.post() - .uri("https://lama.lunaticbum.kr/api/generate") - .body(BodyInserters.fromValue(Gson().toJson(req))) - .retrieve() - .bodyToMono(String::class.java).timeout(Duration.ofSeconds(6000L)).block()?.let { result -> - Gson().fromJson(result, BumlamaResp::class.java)?.let { sss -> - println(Gson().toJson(sss)) - val fullUrl = "https://api.telegram.org/${globalEvv.telegramBotKey}/sendMessage?chat_id=${globalEvv.telegramMyId}&text=${req.reqMsg}의 대답이 도착했어요.\n\n${sss.response}" - logService.log("fullUrl >>> ${fullUrl}") - WebClient.create().get() - .uri(fullUrl) - .retrieve() - .bodyToMono(String::class.java).block() ?: "FAIL" - } - } - } - } catch (e: Exception) { - e.printStackTrace() - } - - } - } } else if (msg.text?.contains("어디") == true) { msg.from?.id?.let { sendMsg(it.toString()) } } else { + println(msg.text) + val req = BumlamaReq(msg.text) + CoroutineScope(Dispatchers.IO).launch { + val fullUrl = + "https://api.telegram.org/${globalEvv.telegramBotKey}/sendMessage?chat_id=${globalEvv.telegramMyId}&text=lama 에게 전송 => ${req.reqMsg}" + logService.log("fullUrl >>> ${fullUrl}") + WebClient.create().get() + .uri(fullUrl) + .retrieve() + .bodyToMono(String::class.java).block() + } + CoroutineScope(Dispatchers.IO).launch { + try { + if (req.reqMsg?.contains("주변") == true) { + val gSearch = "https://www.googleapis.com/customsearch/v1?key=AIzaSyARLXyvmr_554tOy3UCh3naFlZQS3-qQQM&cx=207f328d3ad7242f2&q=${req.reqMsg}&num=5&lr=kr" + println("gSearch >>> ${gSearch}") + WebClient.create().get() + .uri(gSearch) + .retrieve() + .bodyToMono(GoogleSearchResult::class.java).timeout(Duration.ofMinutes(20L)).block()?.let { result -> + println("gsearch result ==> ${Gson().toJson(result)}") + req.reqMsg = req.reqMsg + "\n\n" + result.items?.map { it.snippet +"\nurl:"+it.link }?.joinToString { "\n" } + val client = WebClient.create() + client.post() + .uri("https://lama.lunaticbum.kr/api/generate") + .body(BodyInserters.fromValue(Gson().toJson(req))) + .retrieve() + .bodyToMono(String::class.java).timeout(Duration.ofMinutes(20L)).block()?.let { result -> + println("generate Result ==> ${result}") + Gson().fromJson(result, BumlamaResp::class.java)?.let { sss -> + println(Gson().toJson(sss)) + val fullUrl = "https://api.telegram.org/${globalEvv.telegramBotKey}/sendMessage?chat_id=${globalEvv.telegramMyId}&text=${req.reqMsg}의 대답이 도착했어요.\n\n${sss.response}" + logService.log("fullUrl >>> ${fullUrl}") + WebClient.create().get() + .uri(fullUrl) + .retrieve() + .bodyToMono(String::class.java).block() ?: "FAIL" + } + } + } + } else if (req.reqMsg?.contains("검색") == true) { + val gSearch = "https://www.googleapis.com/customsearch/v1?key=AIzaSyARLXyvmr_554tOy3UCh3naFlZQS3-qQQM&cx=207f328d3ad7242f2&q=${req.reqMsg}&num=5&lr=kr" + println("gSearch >>> ${gSearch}") + WebClient.create().get() + .uri(gSearch) + .retrieve() + .bodyToMono(GoogleSearchResult::class.java).timeout(Duration.ofMinutes(20L)).block()?.let { result -> + println("gsearch result ==> ${Gson().toJson(result)}") + req.reqMsg = req.reqMsg + "\n\n" + result.items?.map { it.snippet +"\nurl:"+it.link }?.joinToString { "\n" } + val client = WebClient.create() + client.post() + .uri("https://lama.lunaticbum.kr/api/generate") + .body(BodyInserters.fromValue(Gson().toJson(req))) + .retrieve() + .bodyToMono(String::class.java).timeout(Duration.ofMinutes(20L)).block()?.let { result -> + println("generate Result ==> ${result}") + Gson().fromJson(result, BumlamaResp::class.java)?.let { sss -> + println(Gson().toJson(sss)) + val fullUrl = "https://api.telegram.org/${globalEvv.telegramBotKey}/sendMessage?chat_id=${globalEvv.telegramMyId}&text=${req.reqMsg}의 대답이 도착했어요.\n\n${sss.response}" + logService.log("fullUrl >>> ${fullUrl}") + WebClient.create().get() + .uri(fullUrl) + .retrieve() + .bodyToMono(String::class.java).block() ?: "FAIL" + } + } + } + + } else { + val client = WebClient.create() + client.post() + .uri("https://lama.lunaticbum.kr/api/generate") + .body(BodyInserters.fromValue(Gson().toJson(req))) + .retrieve() + .bodyToMono(String::class.java).timeout(Duration.ofMinutes(20L)).block()?.let { result -> + Gson().fromJson(result, BumlamaResp::class.java)?.let { sss -> + println(Gson().toJson(sss)) + val fullUrl = "https://api.telegram.org/${globalEvv.telegramBotKey}/sendMessage?chat_id=${globalEvv.telegramMyId}&text=${req.reqMsg}의 대답이 도착했어요.\n\n${sss.response}" + logService.log("fullUrl >>> ${fullUrl}") + WebClient.create().get() + .uri(fullUrl) + .retrieve() + .bodyToMono(String::class.java).block() ?: "FAIL" + } + } + } + } catch (e: Exception) { + e.printStackTrace() + } + + } } } logService.log("test $httpServletRequest.requestURI") diff --git a/src/main/kotlin/kr/lunaticbum/back/lun/model/TelegramUpdate.kt b/src/main/kotlin/kr/lunaticbum/back/lun/model/TelegramUpdate.kt index 15baae9..77119da 100644 --- a/src/main/kotlin/kr/lunaticbum/back/lun/model/TelegramUpdate.kt +++ b/src/main/kotlin/kr/lunaticbum/back/lun/model/TelegramUpdate.kt @@ -58,7 +58,8 @@ class Message { var text: String? = null @BsonIgnore var entities: ArrayList? = null - var location : String? = null + +// var location : Map? = null } @@ -114,4 +115,30 @@ class TelegramMsgService : MsgService { }) } -} \ No newline at end of file +} + +class GoogleSearchResult { + + var items : ArrayList? = null +} + +typealias GSRMeta = Map + +class GSRItem{ + var kind : String? = null //": "customsearch#result", + var title : String? = null //": "전국 현재 날씨", + var htmlTitle : String? = null //": "전국 현재 날씨", + var link : String? = null //": "https://www.accuweather.com/ko/us/united-states-weather", + var displayLink : String? = null //": "www.accuweather.com", + var snippet : String? = null //": "개인정보 보호정책과 쿠키 사용 정책이 업데이트되었습니다. 이해합니다. ComScore.", + var htmlSnippet : String? = null //": "개인정보 보호정책과 쿠키 사용 정책이 업데이트되었습니다. 이해합니다. ComScore.", + var formattedUrl : String? = null //": "https://www.accuweather.com/ko/us/united-states-weather", + var htmlFormattedUrl : String? = null //": "https://www.accuweather.com/ko/us/united-states-weather", + var pagemap : GSRItemPageMap? = null +} +class GSRItemPageMap { + var cse_thumbnail : ArrayList>? = null + var metatags : ArrayList>? = null + var cse_image : ArrayList>? = null +} +