找不到 SttpBackends + "Error occurred in an application involving default arguments."

Can't find SttpBackends + "Error occurred in an application involving default arguments."

我正在尝试使用 bot4s. I'm pretty much following the example there. Here's the code:

在 Scala 中创建一个极其简单的 Telegram 机器人
package info.jjmerelo.BoBot

import cats.instances.future._
import cats.syntax.functor._

import com.bot4s.telegram.api.RequestHandler
import com.bot4s.telegram.api.declarative.Commands
import com.bot4s.telegram.clients.{FutureSttpClient, ScalajHttpClient}
import com.bot4s.telegram.future.{Polling, TelegramBot}

import scala.util.Try
import scala.concurrent.Future

import com.typesafe.scalalogging.Logger


object BoBot extends TelegramBot
    with Polling
    with Commands[Future] {

  implicit val backend = SttpBackends.default
  def token = sys.env("BOBOT_TOKEN")
  override val client: RequestHandler[Future] = new FutureSttpClient(token)
  val log = Logger("BoBot")
//  val lines = scala.io.Source.fromFile("hitos.json").mkString
//  val hitos = JSON.parseFull( lines )
//  val solo_hitos = hitos.getOrElse( hitos )

  onCommand("hey") { implicit msg =>
    log.info("Hello")
    reply("Conseguí que funcionara").void
  }
}

这是 build.sbt

name := "bobot"

version := "0.0.1"

organization := "info.jjmerelo"

libraryDependencies += "com.bot4s" %% "telegram-core" % "4.4.0-RC2"

val circeVersion = "0.12.3"

libraryDependencies ++= Seq(
  "io.circe" %% "circe-core",
  "io.circe" %% "circe-generic",
  "io.circe" %% "circe-parser"
).map(_ % circeVersion)

libraryDependencies += "com.typesafe.scala-logging" %% "scala-logging" % "3.9.2"
retrieveManaged := true

Circe is for later

无论如何,我成功地编译了大部分,但我仍然得到这两个错误:

[info] compiling 2 Scala sources to /home/jmerelo/Asignaturas/cloud-computing/BoBot/target/scala-2.12/classes ...
[error] /home/jmerelo/Asignaturas/cloud-computing/BoBot/src/main/scala/info/jjmerelo/BoBot.scala:21:26: not found: value SttpBackends
[error]   implicit val backend = SttpBackends.default
[error]                          ^
[error] /home/jmerelo/Asignaturas/cloud-computing/BoBot/src/main/scala/info/jjmerelo/BoBot.scala:23:49: could not find implicit value for parameter backend: com.softwaremill.sttp.SttpBackend[scala.concurrent.Future,Nothing]
[error] Error occurred in an application involving default arguments.
[error]   override val client: RequestHandler[Future] = new FutureSttpClient(token)
[error]                                                 ^
[error] two errors found
[error] (Compile / compileIncremental) Compilation failed
[error] Total time: 5 s, completed 11 nov. 2020 8:19:38

这两个我都搞不懂。缺少 SttpBackends,这很清楚,但是示例中没有任何内容表明需要它,或者就此而言,应该包含什么库。第二个关于默认参数的问题我根本搞不懂,即使我将 token 定义为 String 或者如果我将 def 更改为 val。有什么想法吗?

您的错误信息相互关联。 第一个错误告诉我们编译器找不到具有 SttpBackend 字段的对象 SttpBackends。 第二个告诉我们编译器找不到 implicit backend: SttpBackend 来构造 FutureSttpClient。它需要两个隐式:SttpBackendExecutionContext.

class FutureSttpClient(token : _root_.scala.Predef.String, 
  telegramHost : _root_.scala.Predef.String = { /* compiled code */ })
  (implicit backend : com.softwaremill.sttp.SttpBackend[scala.concurrent.Future, scala.Nothing], 
    ec : scala.concurrent.ExecutionContext) 
  extends com.bot4s.telegram.clients.SttpClient[scala.concurrent.Future] {...}

您可以像bot4s examples那样自己创建它。

如果您尝试在 bot4s 库中找到 SttpBackends 对象,您会在 bot4s examples:

中找到此代码
import com.softwaremill.sttp.okhttp._

object SttpBackends {
  val default: SttpBackend[Future, Nothing] = OkHttpFutureBackend()
}

将此对象添加到您的项目以使其可编译。