Started working on actors: update and dialog.
This commit is contained in:
@@ -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")
|
||||
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
42
src/main/scala/eu/xeppaka/bot1/actors/UpdateActor2.scala
Normal file
42
src/main/scala/eu/xeppaka/bot1/actors/UpdateActor2.scala
Normal file
@@ -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())
|
||||
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
package example
|
||||
|
||||
object Hello extends Greeting with App {
|
||||
println(greeting)
|
||||
}
|
||||
|
||||
trait Greeting {
|
||||
lazy val greeting: String = "hello"
|
||||
}
|
||||
@@ -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"
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user