From d1969766b1e926679739eeaf0fe9980ab1c2ee7d Mon Sep 17 00:00:00 2001 From: Pavel Kachalouski Date: Fri, 18 May 2018 20:36:44 +0200 Subject: [PATCH] Started working on actors: update and dialog. --- .idea/modules/root-build.iml | 2 +- .idea/modules/root.iml | 103 +++++++++--------- build.sbt | 1 + project/Dependencies.scala | 1 + .../eu/xeppaka/bot1/TelegramBotServer.scala | 6 +- .../eu/xeppaka/bot1/actors/UpdateActor.scala | 39 +------ .../eu/xeppaka/bot1/actors/UpdateActor2.scala | 42 +++++++ src/main/scala/example/Hello.scala | 9 -- src/test/scala/example/HelloSpec.scala | 9 -- src/test/scala/example/MarshalSpec.scala | 37 ------- 10 files changed, 102 insertions(+), 147 deletions(-) create mode 100644 src/main/scala/eu/xeppaka/bot1/actors/UpdateActor2.scala delete mode 100644 src/main/scala/example/Hello.scala delete mode 100644 src/test/scala/example/HelloSpec.scala delete mode 100644 src/test/scala/example/MarshalSpec.scala diff --git a/.idea/modules/root-build.iml b/.idea/modules/root-build.iml index f493bcb..a93460e 100644 --- a/.idea/modules/root-build.iml +++ b/.idea/modules/root-build.iml @@ -1,5 +1,5 @@ - + diff --git a/.idea/modules/root.iml b/.idea/modules/root.iml index 435b95b..351b328 100644 --- a/.idea/modules/root.iml +++ b/.idea/modules/root.iml @@ -15,6 +15,58 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -24,56 +76,5 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/build.sbt b/build.sbt index b601b8f..5d77978 100644 --- a/build.sbt +++ b/build.sbt @@ -12,6 +12,7 @@ lazy val root = (project in file(".")). libraryDependencies ++= Seq( scalaTest % Test, akka, + akkaTyped, akkaHttp, akkaStream, vkapi, diff --git a/project/Dependencies.scala b/project/Dependencies.scala index be9ca0f..c741389 100644 --- a/project/Dependencies.scala +++ b/project/Dependencies.scala @@ -3,6 +3,7 @@ import sbt._ object Dependencies { lazy val scalaTest = "org.scalatest" %% "scalatest" % "3.0.5" lazy val akka = "com.typesafe.akka" %% "akka-actor" % "2.5.12" + lazy val akkaTyped = "com.typesafe.akka" %% "akka-actor-typed" % "2.5.12" lazy val akkaHttp = "com.typesafe.akka" %% "akka-http" % "10.1.1" lazy val akkaStream = "com.typesafe.akka" %% "akka-stream" % "2.5.12" lazy val vkapi = "com.vk.api" % "sdk" % "0.5.12" diff --git a/src/main/scala/eu/xeppaka/bot1/TelegramBotServer.scala b/src/main/scala/eu/xeppaka/bot1/TelegramBotServer.scala index 21918a4..aa6ef02 100644 --- a/src/main/scala/eu/xeppaka/bot1/TelegramBotServer.scala +++ b/src/main/scala/eu/xeppaka/bot1/TelegramBotServer.scala @@ -13,8 +13,8 @@ import akka.http.scaladsl.unmarshalling.Unmarshal import akka.http.scaladsl.{ConnectionContext, Http, HttpExt, HttpsConnectionContext} import akka.stream.ActorMaterializer import akka.util.ByteString -import eu.xeppaka.bot1.actors.UpdateActor -import eu.xeppaka.bot1.actors.UpdateActor.ReceivedUpdate +import eu.xeppaka.bot1.actors.UpdateActor2 +import eu.xeppaka.bot1.actors.UpdateActor2.ReceivedUpdate import javax.net.ssl.{KeyManagerFactory, SSLContext, TrustManagerFactory} import scala.collection.immutable @@ -38,7 +38,7 @@ class TelegramBotServer(botId: String, port: Int, httpsContext: Option[HttpsConn "pkcloud", port, connectionContext = httpsContext.getOrElse(http.defaultClientHttpsContext)) - private val updateActor = actorSystem.actorOf(UpdateActor.props(botUri, http)) + private val updateActor = actorSystem.actorOf(UpdateActor2.props(botUri, http)) println(s"webhook path: $webhookUri") diff --git a/src/main/scala/eu/xeppaka/bot1/actors/UpdateActor.scala b/src/main/scala/eu/xeppaka/bot1/actors/UpdateActor.scala index dd9ff07..601156d 100644 --- a/src/main/scala/eu/xeppaka/bot1/actors/UpdateActor.scala +++ b/src/main/scala/eu/xeppaka/bot1/actors/UpdateActor.scala @@ -1,42 +1,7 @@ package eu.xeppaka.bot1.actors -import akka.actor.{Actor, ActorLogging, Props} -import akka.http.scaladsl.HttpExt -import akka.http.scaladsl.model.{ContentTypes, HttpEntity, HttpMethods, HttpRequest} -import akka.util.ByteString -import eu.xeppaka.bot1.TelegramEntities.SendMessage -import eu.xeppaka.bot1.{BotUri, TelegramEntities} - -class UpdateActor(botUri: BotUri, http: HttpExt) extends Actor with ActorLogging { - import UpdateActor.ReceivedUpdate - - override def receive: Receive = { - case ReceivedUpdate(update) => processUpdate(update) - } - - private def processUpdate(update: TelegramEntities.Update) = { - log.info(s"Received update: $update") - if (update.message.isDefined) { - processMessage(update.message.get) - } - } - - private def processMessage(message: TelegramEntities.Message) = { - import io.circe._, io.circe.generic.auto._, io.circe.syntax._ - - log.info("Received message from: {}", message.from) - val sendMessage = SendMessage(message.chat.id, s"Привет, ${message.from.get.first_name}") - val printer = Printer.noSpaces.copy(dropNullValues = true) - val json = printer.pretty(sendMessage.asJson) - val request = HttpRequest(HttpMethods.POST, uri = botUri.sendMessage, entity = HttpEntity.Strict(ContentTypes.`application/json`, ByteString(json))) - http.singleRequest(request) - } -} +import eu.xeppaka.bot1.TelegramEntities object UpdateActor { - - case class ReceivedUpdate(update: TelegramEntities.Update) - - def props(botUri: BotUri, http: HttpExt): Props = Props(new UpdateActor(botUri, http)) - + final case class ProcessUpdate(update: TelegramEntities.Update) } diff --git a/src/main/scala/eu/xeppaka/bot1/actors/UpdateActor2.scala b/src/main/scala/eu/xeppaka/bot1/actors/UpdateActor2.scala new file mode 100644 index 0000000..535262b --- /dev/null +++ b/src/main/scala/eu/xeppaka/bot1/actors/UpdateActor2.scala @@ -0,0 +1,42 @@ +package eu.xeppaka.bot1.actors + +import akka.actor.{Actor, ActorLogging, Props} +import akka.http.scaladsl.HttpExt +import akka.http.scaladsl.model.{ContentTypes, HttpEntity, HttpMethods, HttpRequest} +import akka.util.ByteString +import eu.xeppaka.bot1.TelegramEntities.SendMessage +import eu.xeppaka.bot1.{BotUri, TelegramEntities} + +class UpdateActor2 extends Actor with ActorLogging { + import UpdateActor2.ReceivedUpdate + + override def receive: Receive = { + case ReceivedUpdate(update) => processUpdate(update) + } + + private def processUpdate(update: TelegramEntities.Update) = { + log.info(s"Received update: $update") + if (update.message.isDefined) { + processMessage(update.message.get) + } + } + + private def processMessage(message: TelegramEntities.Message) = { + import io.circe._, io.circe.generic.auto._, io.circe.syntax._ + +// log.info("Received message from: {}", message.from) +// val sendMessage = SendMessage(message.chat.id, s"Привет, ${message.from.get.first_name}") +// val printer = Printer.noSpaces.copy(dropNullValues = true) +// val json = printer.pretty(sendMessage.asJson) +// val request = HttpRequest(HttpMethods.POST, uri = botUri.sendMessage, entity = HttpEntity.Strict(ContentTypes.`application/json`, ByteString(json))) +// http.singleRequest(request) + } +} + +object UpdateActor2 { + + case class ReceivedUpdate(update: TelegramEntities.Update) + + def props(): Props = Props(new UpdateActor2()) + +} diff --git a/src/main/scala/example/Hello.scala b/src/main/scala/example/Hello.scala deleted file mode 100644 index 80ea40a..0000000 --- a/src/main/scala/example/Hello.scala +++ /dev/null @@ -1,9 +0,0 @@ -package example - -object Hello extends Greeting with App { - println(greeting) -} - -trait Greeting { - lazy val greeting: String = "hello" -} diff --git a/src/test/scala/example/HelloSpec.scala b/src/test/scala/example/HelloSpec.scala deleted file mode 100644 index 56f5e66..0000000 --- a/src/test/scala/example/HelloSpec.scala +++ /dev/null @@ -1,9 +0,0 @@ -package example - -import org.scalatest._ - -class HelloSpec extends FlatSpec with Matchers { - "The Hello object" should "say hello" in { - Hello.greeting shouldEqual "hello" - } -} diff --git a/src/test/scala/example/MarshalSpec.scala b/src/test/scala/example/MarshalSpec.scala deleted file mode 100644 index 0c62e8c..0000000 --- a/src/test/scala/example/MarshalSpec.scala +++ /dev/null @@ -1,37 +0,0 @@ -package example - -import akka.http.scaladsl.unmarshalling.Unmarshal -import akka.stream.Materializer -import akka.util.ByteString -import eu.xeppaka.bot1.TelegramEntities -import eu.xeppaka.bot1.TelegramEntities.{Chat, Message, User} -import org.scalatest.FlatSpec - -import scala.concurrent.Await -import scala.concurrent.ExecutionContext.Implicits.global -import scala.concurrent.duration._ - -case class Text(t: Option[String], t1: Option[String]) - -class MarshalSpec extends FlatSpec { - - "Circle marshal/unmarshal" should "work" in { - import io.circe._, io.circe.generic.auto._, io.circe.parser._, io.circe.syntax._ - case class Large2(p1: Int, p2: Int, p3: Int, p4: Int, p5: Int, p6: Int, p7: Int, p8: Int, p9: Int, p10: Int, p11: Int, p12: Int, p13: Int, p14: Int, p15: Int, p16: Int, p17: Int, p18: Int, p19: Int, p20: Int, p21: Int, p22: Int, p23: Int) - - val f = Large2(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23) - - val json = f.asJson.noSpaces - println(json) - } - - "Telegram message marshal/unmarshal" should "work" in { - import io.circe._, io.circe.generic.auto._, io.circe.syntax._ - - implicit val printer: Printer = Printer.noSpaces.copy(dropNullValues = true) - val c = Chat(555666, "userChat") - val m = Message(message_id = 111222, from = None, date = 111333, chat = c) - val json = printer.pretty(m.asJson) - println(json) - } -}