Search code examples
scalaspring-data-jpascala-catscats-effect

How execute spring repository method in for comprehension with implicit


I want to save data from telegram api in for comprehension type with implicit, but have an error

Error:(61, 9) type mismatch;
 found   : cats.effect.IO[Unit]
 required: scala.concurrent.Future[?]
      _ <- IO(userRepository.save(User(msg.from.get.id, msg.from.get.username.get)))

The code in TelegramBot example, which use info.mukel.telegrambot4s 3.0.9 library.

  onCommand("/hello") { implicit msg =>
    for {
      _   <- reply(s"Hello ${msg.from.get.firstName}")
      _ <- IO(userRepository.save(User(msg.from.get.id, msg.from.get.username.get)))
    } yield ()
  }

I tried to delete reply and add, this code compiled, but saving (inside IO) didn`t execute

  onCommand("/hello") { implicit msg =>
    for {
      res <- IO(userRepository.save(User(msg.from.get.id, msg.from.get.username.get)))
    } yield res
  }

Is it possible to resolve this problem?


Solution

  • Try

    onCommand("/hello") { implicit msg =>
      for {
        _ <- IO.fromFuture(IO.pure(reply(s"Hello ${msg.from.get.firstName}")))
        _ <- IO(userRepository.save(User(msg.from.get.id, isBot = true, msg.from.get.username.get)))
      } yield ()
    }