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"
|
||||
}
|
||||
Reference in New Issue
Block a user