Skip to content

Commit 13c4aa0

Browse files
authored
Merge pull request #15 from johnspade/methods-fabric
Use the Methods fabric
2 parents c0be7f0 + 6211d3d commit 13c4aa0

34 files changed

+2904
-2645
lines changed

README.md

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Currently the following backends are supported:
1515
- circe
1616
- uPickle (MsgPack binary serialization, to be used for storing API messages in DB and sending over network)
1717

18-
You may want to start with [Api.scala](telegramium-core/src/main/scala/telegramium/bots/client/Api.scala) and [EchoBot.scala](telegramium-examples/src/main/scala/telegramium/bots/examples/EchoBot.scala).
18+
You may want to start with [Methods.scala](telegramium-core/src/main/scala/telegramium/bots/client/Methods.scala) and [EchoBot.scala](telegramium-examples/src/main/scala/telegramium/bots/examples/EchoBot.scala).
1919

2020
### How to use
2121
Create the dependency by adding the following lines to your build.sbt:
@@ -25,6 +25,47 @@ libraryDependencies += "io.github.apimorphism" %% "telegramium-core" % "1.49.0"
2525
libraryDependencies += "io.github.apimorphism" %% "telegramium-high" % "1.49.0"
2626
```
2727

28+
Imports:
29+
```scala
30+
import telegramium.bots.high._
31+
import telegramium.bots.high.implicits._
32+
```
33+
34+
Use the `Methods` fabric to create requests. You will need an instance of the `BotApi` class to execute them:
35+
```scala
36+
BlazeClientBuilder[F](ExecutionContext.global).resource.use { httpClient =>
37+
implicit val api: Api[F] = BotApi(http, baseUrl = s"https://api.telegram.org/bot$token")
38+
val bot = new MyLongPollBot()
39+
bot.start()
40+
}
41+
```
42+
43+
#### Long polling
44+
```scala
45+
class MyLongPollBot[F[_]: Sync: Timer]()(implicit api: Api[F]) extends LongPollBot[F](api) {
46+
override def onMessage(msg: Message): F[Unit] =
47+
Methods.sendMessage(chatId = ChatIntId(msg.chat.id), text = "Hello, world!").exec.void
48+
}
49+
```
50+
51+
`LongPollBot` and `WebhookBot` extend the `Methods` trait so you can call `sendMessage` directly.
52+
53+
#### Webhooks
54+
```scala
55+
class MyWebhookBot[F[_]: ConcurrentEffect: ContextShift: Timer](port: Int, url: String, path: String)(
56+
implicit api: Api[F]
57+
) extends WebhookBot[F](api, port, url, path) {
58+
override def onMessage(msg: Message): F[Unit] =
59+
sendMessage(chatId = ChatIntId(msg.chat.id), text = "Hello, world!").exec.void
60+
}
61+
```
62+
63+
You can also perform a request to the Bot API while [sending an answer to the webhook](https://core.telegram.org/bots/api#making-requests-when-getting-updates):
64+
```scala
65+
override def onMessageReply(msg: Message): F[Option[Method[_]]] =
66+
Sync[F].pure(Some(sendMessage(chatId = ChatIntId(msg.chat.id), text = "Hello, world!")))
67+
```
68+
2869
### Versioning
2970

3071
X.Y.Z where

build.sbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ lazy val `telegramium-core` = project
5757
lazy val `telegramium-high` = project
5858
.dependsOn(`telegramium-core`)
5959
.settings(settings: _*)
60-
.settings(libraryDependencies ++= Dependencies.telegramiumCore)
60+
.settings(libraryDependencies ++= Dependencies.telegramiumHigh)
6161

6262
lazy val `telegramium-examples` = project
6363
.dependsOn(

project/Dependencies.scala

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,49 +3,62 @@ import sbt._
33
object Dependencies {
44

55
object V {
6-
val monix = "3.1.0"
7-
val catsCore = "2.1.1"
6+
val monix = "3.1.0"
7+
val catsCore = "2.1.1"
88
val catsEffect = "2.1.3"
9-
val circe = "0.13.0"
10-
val http4s = "0.21.3"
11-
val slf4j = "1.7.26"
12-
val logback = "1.2.3"
13-
val uPickle = "0.8.0"
9+
val circe = "0.13.0"
10+
val http4s = "0.21.3"
11+
val slf4j = "1.7.26"
12+
val logback = "1.2.3"
13+
val uPickle = "0.8.0"
14+
val scalatest = "3.2.0"
15+
val testcontainers = "0.37.0"
16+
val mockServerClient = "5.10.0"
17+
val scalamock = "4.4.0"
1418
}
1519

16-
val monix = "io.monix" %% "monix" % V.monix
17-
val catsCore = "org.typelevel" %% "cats-core" % V.catsCore
20+
val monix = "io.monix" %% "monix" % V.monix
21+
val catsCore = "org.typelevel" %% "cats-core" % V.catsCore
1822
val catsEffect = "org.typelevel" %% "cats-effect" % V.catsEffect
23+
val scalatest = "org.scalatest" %% "scalatest" % V.scalatest % Test
24+
val scalamock = "org.scalamock" %% "scalamock" % V.scalamock % Test
1925

2026
val circe = Seq(
2127
"io.circe" %% "circe-core" % V.circe,
22-
"io.circe" %% "circe-parser" % V.circe,
28+
"io.circe" %% "circe-parser" % V.circe
2329
)
2430

25-
val http4s = Seq(
26-
"org.http4s" %% "http4s-dsl" % V.http4s,
31+
val http4sClient = Seq(
2732
"org.http4s" %% "http4s-circe" % V.http4s,
28-
"org.http4s" %% "http4s-blaze-server" % V.http4s,
29-
"org.http4s" %% "http4s-blaze-client" % V.http4s,
33+
"org.http4s" %% "http4s-blaze-client" % V.http4s
34+
)
35+
36+
val http4sServer = Seq(
37+
"org.http4s" %% "http4s-dsl" % V.http4s,
38+
"org.http4s" %% "http4s-blaze-server" % V.http4s
3039
)
3140

3241
val logger = Seq(
3342
"org.slf4j" % "slf4j-api" % V.slf4j,
3443
"org.slf4j" % "slf4j-simple" % V.slf4j,
35-
"ch.qos.logback" % "logback-classic" % V.logback,
44+
"ch.qos.logback" % "logback-classic" % V.logback
3645
)
3746

3847
val uPickle = Seq(
3948
"com.lihaoyi" %% "upickle" % V.uPickle,
40-
"com.lihaoyi" %% "upack" % V.uPickle,
49+
"com.lihaoyi" %% "upack" % V.uPickle
50+
)
51+
52+
val testcontainers = Seq(
53+
"com.dimafeng" %% "testcontainers-scala-scalatest" % V.testcontainers % Test,
54+
"com.dimafeng" %% "testcontainers-scala-mockserver" % V.testcontainers % Test,
55+
"org.mock-server" % "mockserver-client-java" % V.mockServerClient % Test
4156
)
4257

43-
val common = Seq(
44-
catsCore,
45-
catsEffect,
46-
) ++ circe ++ http4s ++ uPickle
58+
val common: Seq[ModuleID] = Seq(catsCore) ++ circe ++ uPickle
4759

4860
val telegramiumCore: Seq[ModuleID] = common
49-
val telegramiumHigh: Seq[ModuleID] = common
50-
val telegramiumExam: Seq[ModuleID] = common ++ logger ++ Seq(monix)
61+
val telegramiumHigh: Seq[ModuleID] = common ++ Seq(catsEffect, monix % Test, scalatest, scalamock) ++ http4sServer ++
62+
http4sClient ++ testcontainers ++ logger
63+
val telegramiumExam: Seq[ModuleID] = common ++ logger ++ Seq(catsEffect, monix) ++ http4sClient
5164
}

telegramium-core/src/main/scala/telegramium/bots/HandleUpdateReq.scala

Lines changed: 0 additions & 46 deletions
This file was deleted.

telegramium-core/src/main/scala/telegramium/bots/HandleUpdateRes.scala

Lines changed: 0 additions & 3 deletions
This file was deleted.

telegramium-core/src/main/scala/telegramium/bots/WebhookService.scala

Lines changed: 0 additions & 5 deletions
This file was deleted.

telegramium-core/src/main/scala/telegramium/bots/WebhookServiceHttp4sBackend.scala

Lines changed: 0 additions & 31 deletions
This file was deleted.

0 commit comments

Comments
 (0)