From fc13f50d0e5dbd4b63c2d7c1a5f16479ac9b3c95 Mon Sep 17 00:00:00 2001 From: ff137 Date: Wed, 8 Nov 2023 15:49:06 +0200 Subject: [PATCH 1/7] :art: --- src/xyz/didx/SignalBot.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/xyz/didx/SignalBot.scala b/src/xyz/didx/SignalBot.scala index ff9d7db..c7d99ff 100644 --- a/src/xyz/didx/SignalBot.scala +++ b/src/xyz/didx/SignalBot.scala @@ -59,7 +59,7 @@ case class SignalBot(backend: SttpBackend[IO, Any]): def verify(pin: String): IO[Either[Exception, String]] = val request = basicRequest .contentType("application/json") - .body(s"""{"pin": $pin""") + .body(s"""{"pin": $pin}""") .post(uri"${signalConf.signalUrl}/verify/${signalConf.signalPhone}") val response = request.send(backend) response.map(c => From d571580bd25026dfced4d1b881455173b1f3fb0c Mon Sep 17 00:00:00 2001 From: ff137 Date: Wed, 8 Nov 2023 15:49:25 +0200 Subject: [PATCH 2/7] :sparkles: add methods for start and stop typing indicator --- src/xyz/didx/SignalBot.scala | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/xyz/didx/SignalBot.scala b/src/xyz/didx/SignalBot.scala index c7d99ff..73a1fb2 100644 --- a/src/xyz/didx/SignalBot.scala +++ b/src/xyz/didx/SignalBot.scala @@ -115,3 +115,39 @@ case class SignalBot(backend: SttpBackend[IO, Any]): .flatten .sequence ) + + def startTyping(userNumber: String): IO[ + Either[ResponseException[String, Error], Unit] + ] = + val request = basicRequest + .contentType("application/json") + .response(asJson[List[SignalMessage]]) + .body(s"""{"recipient": $userNumber}""") + .put( + uri"${signalConf.signalUrl}/v1/typing-indicator/${signalConf.signalPhone}" + ) + + val response = request.send(backend) + response map (r => + r.body match + case Left(error) => Left(error) + case Right(_) => Right(IO.unit) + ) + + def stopTyping(userNumber: String): IO[ + Either[ResponseException[String, Error], Unit] + ] = + val request = basicRequest + .contentType("application/json") + .response(asJson[List[SignalMessage]]) + .body(s"""{"recipient": $userNumber}""") + .delete( + uri"${signalConf.signalUrl}/v1/typing-indicator/${signalConf.signalPhone}" + ) + + val response = request.send(backend) + response map (r => + r.body match + case Left(error) => Left(error) + case Right(_) => Right(IO.unit) + ) From 341250a4f4e51490697d7d396beb7f78459e2bb5 Mon Sep 17 00:00:00 2001 From: ff137 Date: Wed, 8 Nov 2023 15:49:53 +0200 Subject: [PATCH 3/7] :sparkles: add start and stop typing indicator to for comprehension in response processing --- src/xyz/didx/ConversationPollingHandler.scala | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/src/xyz/didx/ConversationPollingHandler.scala b/src/xyz/didx/ConversationPollingHandler.scala index a2cf9f0..54a7dea 100644 --- a/src/xyz/didx/ConversationPollingHandler.scala +++ b/src/xyz/didx/ConversationPollingHandler.scala @@ -154,21 +154,22 @@ class ConversationPollingHandler(using logger: Logger[IO]): // Retrieve the current state of the user, defaulting to Onboarding if not present val currentState = userStates.getOrElse(userPhone, ChatState.Onboarding) - val (response, nextState) = AiHandler.getAiResponse( - input = message.text, - conversationId = userPhone, - state = currentState, - telNo = Some(userPhone) - ) - - // Update the state map with the new state for this user - userStates.update(userPhone, nextState) - - val signalMessage: SignalSimpleMessage = SignalSimpleMessage(userPhone, message.name, response) - for { - sendResult: String <- processAndRespond(signalMessage, signalBot) - } yield sendResult + _ <- signalBot.startTyping(userPhone) + (response, nextState) = AiHandler.getAiResponse( + input = message.text, + conversationId = userPhone, + state = currentState, + telNo = Some(userPhone) + ) + signalMessage = SignalSimpleMessage(userPhone, message.name, response) + _ <- signalBot.stopTyping(userPhone) + sendResult: String <- processAndRespond(signalMessage, signalBot) + } yield { + // Update the state map with the new state for this user + userStates.update(userPhone, nextState) + sendResult + } private def processAndRespond( k: SignalSimpleMessage, From 94a1825be6ceb7a43531deb9cd5a40a1da9a86e9 Mon Sep 17 00:00:00 2001 From: ff137 Date: Wed, 8 Nov 2023 16:13:41 +0200 Subject: [PATCH 4/7] :arrow_up: update sttp.client3 --- project.scala | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/project.scala b/project.scala index 8cb132e..81dfca1 100644 --- a/project.scala +++ b/project.scala @@ -17,9 +17,9 @@ //> using dep dev.profunktor::redis4cats-log4cats:1.5.2 //> using dep com.softwaremill.sttp.client4::core:4.0.0-M6 -//> using dep com.softwaremill.sttp.client3::core:3.9.0 -//> using dep com.softwaremill.sttp.client3::circe:3.9.0 -//> using dep com.softwaremill.sttp.client3::async-http-client-backend-cats:3.9.0 +//> using dep com.softwaremill.sttp.client3::core:3.9.1 +//> using dep com.softwaremill.sttp.client3::circe:3.9.1 +//> using dep com.softwaremill.sttp.client3::async-http-client-backend-cats:3.9.1 //> using dep de.brendamour:jpasskit:0.3.3 //> using dep com.outr::scribe:3.12.2 From d1af90b29e6f1b2bb79ba5dbc611527969ffa68b Mon Sep 17 00:00:00 2001 From: ff137 Date: Wed, 8 Nov 2023 16:15:27 +0200 Subject: [PATCH 5/7] :art: fix types --- src/xyz/didx/ConversationPollingHandler.scala | 34 ++++++------------- 1 file changed, 10 insertions(+), 24 deletions(-) diff --git a/src/xyz/didx/ConversationPollingHandler.scala b/src/xyz/didx/ConversationPollingHandler.scala index 54a7dea..3caa600 100644 --- a/src/xyz/didx/ConversationPollingHandler.scala +++ b/src/xyz/didx/ConversationPollingHandler.scala @@ -143,12 +143,12 @@ class ConversationPollingHandler(using logger: Logger[IO]): userMessages: List[SignalSimpleMessage], signalBot: SignalBot ): EitherT[IO, Exception, List[String]] = - userMessages.traverse(getResponseFromUserMessage(_, signalBot)) + userMessages.traverse(msg => EitherT(getResponseFromUserMessage(msg, signalBot))) private def getResponseFromUserMessage( message: SignalSimpleMessage, signalBot: SignalBot - ): EitherT[IO, Exception, String] = + ): IO[Either[Exception, String]] = val userPhone = message.phone // Retrieve the current state of the user, defaulting to Onboarding if not present @@ -164,30 +164,16 @@ class ConversationPollingHandler(using logger: Logger[IO]): ) signalMessage = SignalSimpleMessage(userPhone, message.name, response) _ <- signalBot.stopTyping(userPhone) - sendResult: String <- processAndRespond(signalMessage, signalBot) + sendResult <- signalBot.send( + SignalSendMessage( + List[String](), + message.text, + signalConf.signalPhone, + List(message.phone) + ) + ) } yield { // Update the state map with the new state for this user userStates.update(userPhone, nextState) sendResult } - - private def processAndRespond( - k: SignalSimpleMessage, - signalBot: SignalBot - ): EitherT[IO, Exception, String] = - k match { - case m: SignalSimpleMessage if m.text.toLowerCase().contains("https://maps.google.com") => - EitherT.fromEither[IO](Left(new Exception("Not implemented yet"))) - - case _ => - EitherT( - signalBot.send( - SignalSendMessage( - List[String](), - k.text, - signalConf.signalPhone, - List(k.phone) - ) - ) - ) - } From 969057cdbcfd7b5d73a176c3e2bd33bffea5f355 Mon Sep 17 00:00:00 2001 From: ff137 Date: Thu, 9 Nov 2023 12:38:43 +0200 Subject: [PATCH 6/7] fix no response type expected from 204 code --- src/xyz/didx/SignalBot.scala | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/xyz/didx/SignalBot.scala b/src/xyz/didx/SignalBot.scala index 73a1fb2..12beca5 100644 --- a/src/xyz/didx/SignalBot.scala +++ b/src/xyz/didx/SignalBot.scala @@ -117,11 +117,10 @@ case class SignalBot(backend: SttpBackend[IO, Any]): ) def startTyping(userNumber: String): IO[ - Either[ResponseException[String, Error], Unit] + Either[String, Unit] ] = val request = basicRequest .contentType("application/json") - .response(asJson[List[SignalMessage]]) .body(s"""{"recipient": $userNumber}""") .put( uri"${signalConf.signalUrl}/v1/typing-indicator/${signalConf.signalPhone}" @@ -135,11 +134,10 @@ case class SignalBot(backend: SttpBackend[IO, Any]): ) def stopTyping(userNumber: String): IO[ - Either[ResponseException[String, Error], Unit] + Either[String, Unit] ] = val request = basicRequest .contentType("application/json") - .response(asJson[List[SignalMessage]]) .body(s"""{"recipient": $userNumber}""") .delete( uri"${signalConf.signalUrl}/v1/typing-indicator/${signalConf.signalPhone}" From 384045e8ba999d2706228268ae3a5baf88b7d5fa Mon Sep 17 00:00:00 2001 From: ff137 Date: Thu, 9 Nov 2023 12:38:51 +0200 Subject: [PATCH 7/7] fix message text being sent --- src/xyz/didx/ConversationPollingHandler.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/xyz/didx/ConversationPollingHandler.scala b/src/xyz/didx/ConversationPollingHandler.scala index 3caa600..dfaa6a0 100644 --- a/src/xyz/didx/ConversationPollingHandler.scala +++ b/src/xyz/didx/ConversationPollingHandler.scala @@ -167,7 +167,7 @@ class ConversationPollingHandler(using logger: Logger[IO]): sendResult <- signalBot.send( SignalSendMessage( List[String](), - message.text, + signalMessage.text, signalConf.signalPhone, List(message.phone) )