Skip to content
Open
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: 5 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ version := "0.1"

scalaVersion := "2.12.9"

libraryDependencies += "com.bot4s" %% "telegram-core" % "4.4.0-RC2"
libraryDependencies ++= Seq(
"com.bot4s" %% "telegram-core" % "4.4.0-RC2",
"com.softwaremill.sttp" %% "json4s" % "1.7.2",
"org.json4s" %% "json4s-native" % "3.6.0"
)
40 changes: 35 additions & 5 deletions src/main/scala/bot/BotStarter.scala
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package bot

import bot.BotStarter.Service
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 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}

Expand All @@ -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] {

Expand All @@ -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
}
}
}
Expand All @@ -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)
}
}