Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions build.mill
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ object library {
val catsEffect = "2.5.5"
val zio = "2.1.26"
val catsEffect3 = "3.7.0"
val zhttp = "2.0.0-RC10"
val zioHttp = "3.11.3"
val zioInteropCats = "23.1.0.13"
val sttp = "4.0.26"
val scalaTest = "3.2.20"
Expand Down Expand Up @@ -70,7 +70,7 @@ object library {
val sttpOkHttp = mvn"com.softwaremill.sttp.client4::okhttp-backend::${Version.sttp}"
val hammock = mvn"com.pepegar::hammock-core::${Version.hammock}"
val zio = mvn"dev.zio::zio::${Version.zio}"
val zhttp = mvn"io.d11::zhttp::${Version.zhttp}"
val zioHttp = mvn"dev.zio::zio-http::${Version.zioHttp}"
val zioInteropCats = mvn"dev.zio::zio-interop-cats::${Version.zioInteropCats}"
}

Expand Down Expand Up @@ -264,7 +264,7 @@ object examples extends Module {
super.mvnDeps() ++ Seq(
library.asyncHttpClientBackendZio,
library.zio,
library.zhttp,
library.zioHttp,
library.zioInteropCats,
library.catsEffect3
)
Expand Down
31 changes: 16 additions & 15 deletions examples/src-zio/CommandsWebhookBot.scala
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import zio._
import zhttp.http._
import zio.http._
import com.bot4s.telegram.api.declarative.{ Commands, RegexCommands }
import com.bot4s.telegram.methods.SetWebhook
import zhttp.service.Server
import zhttp.service.server.ServerChannelFactory
import zhttp.service.EventLoopGroup
import com.bot4s.telegram.models.Update
import com.bot4s.telegram.cats.TelegramBot
import sttp.client4.Backend
Expand All @@ -24,15 +21,21 @@ class CommandsWebhookBot(token: String, backend: Backend[Task], private val star
import com.bot4s.telegram.marshalling._
val webhookUrl = "https://XXXX.eu.ngrok.io"

private def callback = Http.collectZIO[Request] { case req @ Method.POST -> !! =>
for {
body <- req.bodyAsString
update <- ZIO.attempt(fromJson[Update](body))
handler <- receiveUpdate(update, None)
} yield Response.ok
}
private def callback = Routes(
Method.POST / Root -> handler { (req: Request) =>
for {
body <- req.body.asString
update <- ZIO.attempt(fromJson[Update](body))
handler <- receiveUpdate(update, None)
} yield Response.ok
}
)

private def server = Server.port(8081) ++ Server.app(callback)
private def server =
Server
.install(callback.sandbox)
.flatMap(port => ZIO.logInfo(s"Server started on $port") *> ZIO.never)
.provide(Server.defaultWithPort(8081))
override def run() =
started.updateZIO { isStarted =>
for {
Expand All @@ -46,9 +49,7 @@ class CommandsWebhookBot(token: String, backend: Backend[Task], private val star
}
} yield response
} *>
server.make
.flatMap(start => ZIO.logInfo(s"Server started on ${start.port}") *> ZIO.never)
.provide(ServerChannelFactory.auto, EventLoopGroup.auto(1), Scope.default)
server

// String commands.
onCommand("/hello") { implicit msg =>
Expand Down
Loading