Http client tests. Telegram entities started.
This commit is contained in:
@@ -12,6 +12,7 @@ lazy val root = (project in file(".")).
|
||||
scalaTest % Test,
|
||||
akka,
|
||||
akkaHttp,
|
||||
akkaHttpSprayJson,
|
||||
akkaStream,
|
||||
vkapi
|
||||
)
|
||||
|
||||
@@ -4,6 +4,7 @@ object Dependencies {
|
||||
lazy val scalaTest = "org.scalatest" %% "scalatest" % "3.0.5"
|
||||
lazy val akka = "com.typesafe.akka" %% "akka-actor" % "2.5.12"
|
||||
lazy val akkaHttp = "com.typesafe.akka" %% "akka-http" % "10.1.1"
|
||||
lazy val akkaHttpSprayJson = "com.typesafe.akka" %% "akka-http-spray-json" % "10.1.1"
|
||||
lazy val akkaStream = "com.typesafe.akka" %% "akka-stream" % "2.5.12"
|
||||
lazy val vkapi = "com.vk.api" % "sdk" % "0.5.12"
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import akka.actor.Actor
|
||||
|
||||
class DialogActor extends Actor {
|
||||
private val dialogId = UUID.randomUUID()
|
||||
private var userId: Option[Int] = None
|
||||
|
||||
override def receive: Receive = {
|
||||
case 1 =>
|
||||
@@ -13,5 +14,5 @@ class DialogActor extends Actor {
|
||||
}
|
||||
|
||||
object DialogActor {
|
||||
|
||||
case class Start()
|
||||
}
|
||||
76
src/main/scala/eu/xeppaka/bot1/TelegramBot.scala
Normal file
76
src/main/scala/eu/xeppaka/bot1/TelegramBot.scala
Normal file
@@ -0,0 +1,76 @@
|
||||
package eu.xeppaka.bot1
|
||||
|
||||
import akka.actor.ActorSystem
|
||||
import akka.http.scaladsl.Http
|
||||
import akka.http.scaladsl.server.Directives._
|
||||
import akka.http.scaladsl.server.{Route, RouteResult}
|
||||
import akka.stream.ActorMaterializer
|
||||
|
||||
import scala.concurrent.{ExecutionContextExecutor, Future}
|
||||
import scala.io.StdIn
|
||||
import TelegramEntities._
|
||||
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport
|
||||
import akka.http.scaladsl.model.{HttpRequest, StatusCodes}
|
||||
import akka.http.scaladsl.server.directives.LoggingMagnet
|
||||
import akka.http.scaladsl.unmarshalling.Unmarshal
|
||||
import spray.json._
|
||||
|
||||
import scala.util.{Failure, Success}
|
||||
|
||||
trait JsonSupport extends SprayJsonSupport with DefaultJsonProtocol {
|
||||
implicit object getMeFormat extends RootJsonFormat[GetMe] {
|
||||
override def write(obj: GetMe): JsValue = ???
|
||||
|
||||
override def read(json: JsValue): GetMe =
|
||||
json.asJsObject.getFields("id", "is_bot", "first_name", "username") match {
|
||||
case Seq(JsNumber(id), JsBoolean(isBot), JsString(firstName), JsString(userName)) => GetMe(id.toInt, isBot, firstName, userName)
|
||||
case _ => throw new DeserializationException("GetMe deserialize error")
|
||||
}
|
||||
}
|
||||
implicit val responseFormat = jsonFormat2(Response[GetMe])
|
||||
}
|
||||
|
||||
class TelegramBot extends JsonSupport {
|
||||
|
||||
def run(): Unit = {
|
||||
implicit val actorSystem: ActorSystem = ActorSystem("telegram-bot")
|
||||
implicit val materializer: ActorMaterializer = ActorMaterializer()
|
||||
implicit val executionContext: ExecutionContextExecutor = actorSystem.dispatcher
|
||||
|
||||
val bindingFuture = Http().bindAndHandle(botRoutes(), "localhost", 8080)
|
||||
|
||||
StdIn.readLine()
|
||||
|
||||
bindingFuture
|
||||
.flatMap(_.unbind())
|
||||
.onComplete(_ => actorSystem.terminate())
|
||||
}
|
||||
|
||||
def printRequestMethodAndResponseStatus(req: HttpRequest)(res: RouteResult): Unit = {
|
||||
println(req)
|
||||
println(res)
|
||||
}
|
||||
|
||||
def botRoutes()(implicit actorSystem: ActorSystem, materializer: ActorMaterializer, executionContext: ExecutionContextExecutor): Route = {
|
||||
path("test") {
|
||||
get {
|
||||
logRequestResult(LoggingMagnet(_ => printRequestMethodAndResponseStatus)) {
|
||||
onComplete(getBotInfo()) {
|
||||
case Success(res) => complete(res.ok.toString)
|
||||
case Failure(ex) => complete(StatusCodes.InternalServerError, "Boooom!")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
def getBotInfo()(implicit actorSystem: ActorSystem, materializer: ActorMaterializer, executionContext: ExecutionContextExecutor): Future[Response[GetMe]] = {
|
||||
Http().singleRequest(HttpRequest(uri = "https://api.telegram.org/bot570855144:AAEv7b817cuq2JJI9f2kG5B9G3zW1x-btz4/getMe")).flatMap(Unmarshal(_).to[Response[GetMe]])
|
||||
}
|
||||
}
|
||||
|
||||
object TelegramBot {
|
||||
def main(args: Array[String]): Unit = {
|
||||
new TelegramBot().run()
|
||||
}
|
||||
}
|
||||
43
src/main/scala/eu/xeppaka/bot1/TelegramEntities.scala
Normal file
43
src/main/scala/eu/xeppaka/bot1/TelegramEntities.scala
Normal file
@@ -0,0 +1,43 @@
|
||||
package eu.xeppaka.bot1
|
||||
|
||||
object TelegramEntities {
|
||||
|
||||
case class Response[T](ok: Boolean,
|
||||
// description: Option[String],
|
||||
// errorCode: Option[Int],
|
||||
result: T)
|
||||
|
||||
case class GetMe(id: Int, isBot: Boolean, firstName: String, userName: String)
|
||||
|
||||
case class Update(updateId: Int,
|
||||
message: Option[Message],
|
||||
editedMessage: Option[Message],
|
||||
channelPost: Option[Message],
|
||||
editedChannelPost: Option[Message])
|
||||
|
||||
case class User(id: Int,
|
||||
isBot: Boolean,
|
||||
firstName: String,
|
||||
lastName: Option[String],
|
||||
userName: Option[String],
|
||||
languageCode: Option[String])
|
||||
|
||||
case class Message()
|
||||
|
||||
case class ChatPhoto(smallFileId: String, bigFileId: String)
|
||||
|
||||
case class Chat(id: Int,
|
||||
chatType: String,
|
||||
title: Option[String],
|
||||
userName: Option[String],
|
||||
firstName: Option[String],
|
||||
lastName: Option[String],
|
||||
allMembersAreAdministrators: Option[Boolean],
|
||||
photo: Option[ChatPhoto],
|
||||
description: Option[String],
|
||||
inviteLink: Option[String],
|
||||
pinnedMessage: Option[Message],
|
||||
stickerSetName: Option[String],
|
||||
canSetStickerSet: Option[Boolean]
|
||||
)
|
||||
}
|
||||
1
urls.txt
1
urls.txt
@@ -1,2 +1,3 @@
|
||||
https://oauth.vk.com/authorize?client_id=6472591&scope=friends&redirect_uri=https://oauth.vk.com/blank.html&display=mobile&v=5.74&response_type=token
|
||||
https://oauth.vk.com/authorize?client_id=6472591&scope=friends&redirect_uri=https://oauth.vk.com/blank.html&display=mobile&v=5.74&response_type=code
|
||||
https://api.telegram.org/bot570855144:AAEv7b817cuq2JJI9f2kG5B9G3zW1x-btz4/getMe
|
||||
|
||||
Reference in New Issue
Block a user