From 2b14f0bcc5d890a190aac98871e907a95dd42104 Mon Sep 17 00:00:00 2001 From: handlessmidas Date: Mon, 2 Mar 2020 18:37:11 +0300 Subject: [PATCH] Added random cat generation --- build.sbt | 6 ++++- src/main/scala/bot/BotStarter.scala | 40 +++++++++++++++++++++++++---- 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/build.sbt b/build.sbt index 2350a09..548dca0 100644 --- a/build.sbt +++ b/build.sbt @@ -4,4 +4,8 @@ version := "0.1" scalaVersion := "2.12.9" -libraryDependencies += "com.bot4s" %% "telegram-core" % "4.4.0-RC2" \ No newline at end of file +libraryDependencies ++= Seq( + "com.bot4s" %% "telegram-core" % "4.4.0-RC2", + "com.softwaremill.sttp" %% "json4s" % "1.7.2", + "org.json4s" %% "json4s-native" % "3.6.0" +) \ No newline at end of file diff --git a/src/main/scala/bot/BotStarter.scala b/src/main/scala/bot/BotStarter.scala index 565a9a9..45802b5 100644 --- a/src/main/scala/bot/BotStarter.scala +++ b/src/main/scala/bot/BotStarter.scala @@ -1,5 +1,6 @@ package bot +import bot.BotStarter.Service import cats.instances.future._ import cats.syntax.functor._ import com.bot4s.telegram.api.RequestHandler @@ -7,7 +8,8 @@ import com.bot4s.telegram.api.declarative.Commands import com.bot4s.telegram.clients.{FutureSttpClient, ScalajHttpClient} import com.bot4s.telegram.future.{Polling, TelegramBot} import com.bot4s.telegram.models.{Message, User} -import com.softwaremill.sttp.SttpBackendOptions +import com.softwaremill.sttp.{SttpBackendOptions, sttp} +import com.softwaremill.sttp.json4s.asJson import com.softwaremill.sttp.okhttp.{OkHttpBackend, OkHttpFutureBackend} import slogging.{LogLevel, LoggerConfig, PrintLoggerFactory} @@ -17,8 +19,12 @@ import scala.collection.mutable.Queue import scala.concurrent.duration.Duration import scala.concurrent.{Await, ExecutionContext, Future} import scala.io.Source +import scala.util.Random +import scala.io.Source +import com.softwaremill.sttp._ +import com.softwaremill.sttp.json4s._ -class BotStarter(override val client: RequestHandler[Future]) extends TelegramBot +class BotStarter(override val client: RequestHandler[Future], val service: Service) extends TelegramBot with Polling with Commands[Future] { @@ -29,7 +35,7 @@ class BotStarter(override val client: RequestHandler[Future]) extends TelegramBo case None => reply("Register error").void case Some(user) => { registeredUsers += user - reply(s"You're registered.\n Your id is ${user.id}").void + reply(s"You're registered.\nYour id is ${user.id}").void } } } @@ -41,19 +47,43 @@ class BotStarter(override val client: RequestHandler[Future]) extends TelegramBo } reply(usersString).void } + + onCommand("/cat") { implicit msg => + service.getCat().flatMap(reply(_)).void + } } object BotStarter { + + implicit val serialization = org.json4s.native.Serialization + + case class Response(data: List[Data]) + case class Data(link: String) + + class Service(implicit val backend: SttpBackend[Future, Nothing]) { + implicit val ec: ExecutionContext = ExecutionContext.global + val request: RequestT[Id, Response, Nothing] = sttp + .header("Authorization", "Client-ID 2a47c24862afdf7") + .get(uri"https://api.imgur.com/3/gallery/search?q=cats") + .response(asJson[Response]) + + def getCat() = backend.send(request).map { response => + scala.util.Random.shuffle(response.unsafeBody.data).head.link + } + } + def main(args: Array[String]): Unit = { implicit val ec: ExecutionContext = ExecutionContext.global - implicit val backend = OkHttpFutureBackend( + implicit val backend: SttpBackend[Future, Nothing] = OkHttpFutureBackend( SttpBackendOptions.Default.socksProxy("ps8yglk.ddns.net", 11999) ) val fileSource = Source.fromFile("token.txt") val token = fileSource.mkString fileSource.close() - val bot = new BotStarter(new FutureSttpClient(token)) + + val service: Service = new Service() + val bot = new BotStarter(new FutureSttpClient(token), service) Await.result(bot.run(), Duration.Inf) } } \ No newline at end of file