Compare commits
1 Commits
afe113c2db
...
b6921f09d1
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b6921f09d1 |
@@ -26,7 +26,7 @@ lazy val `telegram-bot-delivery` = (project in file("."))
|
|||||||
circeAkkaHttp
|
circeAkkaHttp
|
||||||
),
|
),
|
||||||
Docker / defaultLinuxInstallLocation := "/opt/telegram-bot-delivery",
|
Docker / defaultLinuxInstallLocation := "/opt/telegram-bot-delivery",
|
||||||
Docker / dockerExposedPorts := Seq(8443),
|
Docker / dockerExposedPorts := Seq(88, 8443),
|
||||||
Docker / dockerRepository := Some("registry.xeppaka.eu:443")
|
Docker / dockerRepository := Some("registry.xeppaka.eu:443")
|
||||||
)
|
)
|
||||||
.enablePlugins(JavaServerAppPackaging)
|
.enablePlugins(JavaServerAppPackaging)
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
akka {
|
akka {
|
||||||
loglevel = "INFO"
|
loglevel = "DEBUG"
|
||||||
|
|
||||||
extensions = [akka.persistence.Persistence]
|
extensions = [akka.persistence.Persistence]
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
</encoder>
|
</encoder>
|
||||||
</appender>
|
</appender>
|
||||||
|
|
||||||
<root level="INFO">
|
<root level="DEBUG">
|
||||||
<appender-ref ref="STDOUT"/>
|
<appender-ref ref="STDOUT"/>
|
||||||
</root>
|
</root>
|
||||||
</configuration>
|
</configuration>
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
package eu.xeppaka.bot
|
package eu.xeppaka.bot
|
||||||
|
|
||||||
import java.nio.file.Paths
|
|
||||||
|
|
||||||
import akka.actor.Scheduler
|
import akka.actor.Scheduler
|
||||||
import akka.actor.typed.scaladsl.AskPattern._
|
import akka.actor.typed.scaladsl.AskPattern._
|
||||||
import akka.actor.typed.scaladsl.Behaviors
|
import akka.actor.typed.scaladsl.Behaviors
|
||||||
@@ -17,28 +15,30 @@ import scala.io.StdIn
|
|||||||
|
|
||||||
object Main {
|
object Main {
|
||||||
def main(args: Array[String]): Unit = {
|
def main(args: Array[String]): Unit = {
|
||||||
val botId = System.getProperty("botId", "570855144:AAEv7b817cuq2JJI9f2kG5B9G3zW1x-btz4")
|
val isProduction = System.getProperty("isProduction", "false").toBoolean
|
||||||
val localPort = 8443
|
|
||||||
val hookDomain = System.getProperty("hookDomain", "xeppaka.eu")
|
|
||||||
val hookPort = System.getProperty("hookPort", "8443").toInt
|
|
||||||
val useHttpsServer = System.getProperty("useHttpsServer", "true").toBoolean
|
|
||||||
|
|
||||||
val botBehavior = Behaviors.supervise(TelegramBot.behavior(botId, "0.0.0.0", localPort, hookDomain, hookPort, useHttpsServer)).onFailure(SupervisorStrategy.restart)
|
val (botId, localPort, hookPort) = if (isProduction) {
|
||||||
val telegramBot = ActorSystem(botBehavior, "telegram-bot-delivery")
|
("693134480:AAE8JRXA6j1mkOKTaxapP6A-E4LPHRuiIf8", 88, 88) // delivery bot
|
||||||
// implicit val actorSystem: actor.ActorSystem = telegramBot.toUntyped
|
} else {
|
||||||
// implicit val executionContext: ExecutionContextExecutor = telegramBot.dispatchers.lookup(DispatcherSelector.default())
|
("570855144:AAEv7b817cuq2JJI9f2kG5B9G3zW1x-btz4", 8443, 8443) // useless bot
|
||||||
// implicit val scheduler: Scheduler = telegramBot.scheduler
|
}
|
||||||
// implicit val timeout: Timeout = 10.seconds
|
|
||||||
|
|
||||||
// println("Press enter to finish bot...")
|
val botBehavior = Behaviors.supervise(TelegramBot.behavior(botId, "0.0.0.0", localPort, hookPort)).onFailure(SupervisorStrategy.restart)
|
||||||
// StdIn.readLine()
|
val telegramBot = ActorSystem(botBehavior, "telegram-bot")
|
||||||
//
|
implicit val actorSystem: actor.ActorSystem = telegramBot.toUntyped
|
||||||
// val stopFuture: Future[Done] = telegramBot ? (ref => TelegramBot.Stop(ref))
|
implicit val executionContext: ExecutionContextExecutor = telegramBot.dispatchers.lookup(DispatcherSelector.default())
|
||||||
//
|
implicit val scheduler: Scheduler = telegramBot.scheduler
|
||||||
// val terminateFuture = stopFuture
|
implicit val timeout: Timeout = 10.seconds
|
||||||
// .andThen { case _ => Http().shutdownAllConnectionPools() }
|
|
||||||
// .andThen { case _ => telegramBot.terminate() }
|
println("Press enter to finish bot...")
|
||||||
//
|
StdIn.readLine()
|
||||||
// Await.ready(terminateFuture, 20.seconds)
|
|
||||||
|
val stopFuture: Future[Done] = telegramBot ? (ref => TelegramBot.Stop(ref))
|
||||||
|
|
||||||
|
val terminateFuture = stopFuture
|
||||||
|
.andThen { case _ => Http().shutdownAllConnectionPools() }
|
||||||
|
.andThen { case _ => telegramBot.terminate() }
|
||||||
|
|
||||||
|
Await.ready(terminateFuture, 20.seconds)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package eu.xeppaka.bot
|
package eu.xeppaka.bot
|
||||||
|
|
||||||
import java.io.InputStream
|
import java.io.InputStream
|
||||||
import java.nio.file.Path
|
|
||||||
import java.security.{KeyStore, SecureRandom}
|
import java.security.{KeyStore, SecureRandom}
|
||||||
import java.util.UUID
|
import java.util.UUID
|
||||||
|
|
||||||
@@ -21,7 +20,7 @@ import eu.xeppaka.bot.TelegramEntities._
|
|||||||
import javax.net.ssl.{KeyManagerFactory, SSLContext, TrustManagerFactory}
|
import javax.net.ssl.{KeyManagerFactory, SSLContext, TrustManagerFactory}
|
||||||
|
|
||||||
import scala.collection.immutable
|
import scala.collection.immutable
|
||||||
import scala.concurrent.{ExecutionContextExecutor, Future}
|
import scala.concurrent.ExecutionContextExecutor
|
||||||
import scala.concurrent.duration._
|
import scala.concurrent.duration._
|
||||||
import scala.io.Source
|
import scala.io.Source
|
||||||
import scala.util.{Failure, Success}
|
import scala.util.{Failure, Success}
|
||||||
@@ -35,7 +34,7 @@ object TelegramBot {
|
|||||||
case object GetBotInfo
|
case object GetBotInfo
|
||||||
case object GetWebhookInfo
|
case object GetWebhookInfo
|
||||||
|
|
||||||
def behavior(botId: String, interface: String, localPort: Int, hookDomain: String, hookPort: Int, useHttpsServer: Boolean = true): Behavior[Command] = Behaviors.setup[Command] { ctx =>
|
def behavior(botId: String, interface: String, localPort: Int, hookPort: Int): Behavior[Command] = Behaviors.setup[Command] { ctx =>
|
||||||
ctx.log.info("action=start_bot")
|
ctx.log.info("action=start_bot")
|
||||||
|
|
||||||
implicit val untypedSystem: ActorSystem = ctx.system.toUntyped
|
implicit val untypedSystem: ActorSystem = ctx.system.toUntyped
|
||||||
@@ -45,8 +44,8 @@ object TelegramBot {
|
|||||||
val botUri = BotUri(botId)
|
val botUri = BotUri(botId)
|
||||||
val http: HttpExt = Http()
|
val http: HttpExt = Http()
|
||||||
val hookId = UUID.randomUUID().toString
|
val hookId = UUID.randomUUID().toString
|
||||||
val webhookUri = Uri(s"https://$hookDomain:$hookPort/$hookId")
|
val webhookUri = Uri(s"https://xeppaka.eu:$hookPort/$hookId")
|
||||||
val httpsContext = if (useHttpsServer) Some(createHttpsConnectionContext) else None
|
val httpsContext = createHttpsConnectionContext
|
||||||
val stashBuffer = StashBuffer[Command](10)
|
val stashBuffer = StashBuffer[Command](10)
|
||||||
val dialogManager = ctx.spawnAnonymous(Behaviors.supervise(DialogManager.behavior(botUri)).onFailure(SupervisorStrategy.restart))
|
val dialogManager = ctx.spawnAnonymous(Behaviors.supervise(DialogManager.behavior(botUri)).onFailure(SupervisorStrategy.restart))
|
||||||
val routes = botRoutes(hookId, dialogManager)(untypedSystem.scheduler)
|
val routes = botRoutes(hookId, dialogManager)(untypedSystem.scheduler)
|
||||||
@@ -58,7 +57,7 @@ object TelegramBot {
|
|||||||
ctx.log.info("action=bind_server interface={} port={}", interface, localPort)
|
ctx.log.info("action=bind_server interface={} port={}", interface, localPort)
|
||||||
|
|
||||||
http
|
http
|
||||||
.bindAndHandle(routes, interface, localPort, httpsContext.getOrElse(http.defaultServerHttpContext))
|
.bindAndHandle(routes, interface, localPort, httpsContext)
|
||||||
.onComplete {
|
.onComplete {
|
||||||
case Success(binding) => ctx.self ! BindingSuccess(binding)
|
case Success(binding) => ctx.self ! BindingSuccess(binding)
|
||||||
case Failure(exception) => ctx.self ! BindingFailure(exception)
|
case Failure(exception) => ctx.self ! BindingFailure(exception)
|
||||||
@@ -113,21 +112,15 @@ object TelegramBot {
|
|||||||
implicit val executionContextExecutor: ExecutionContextExecutor = ctx.system.dispatchers.lookup(DispatcherSelector.default())
|
implicit val executionContextExecutor: ExecutionContextExecutor = ctx.system.dispatchers.lookup(DispatcherSelector.default())
|
||||||
|
|
||||||
val urlEntity = HttpEntity.Strict(ContentTypes.`text/plain(UTF-8)`, ByteString(webhookUri.toString()))
|
val urlEntity = HttpEntity.Strict(ContentTypes.`text/plain(UTF-8)`, ByteString(webhookUri.toString()))
|
||||||
val urlPart = Some(Multipart.FormData.BodyPart.Strict("url", urlEntity))
|
val urlPart = Multipart.FormData.BodyPart.Strict("url", urlEntity)
|
||||||
|
|
||||||
val certificatePart = if (useHttpsServer) {
|
|
||||||
val certificate = ByteString(Source.fromResource("telegram-bot.pem").mkString)
|
val certificate = ByteString(Source.fromResource("telegram-bot.pem").mkString)
|
||||||
val certificateEntity = HttpEntity.Strict(ContentTypes.`application/octet-stream`, certificate)
|
val certificateEntity = HttpEntity.Strict(ContentTypes.`application/octet-stream`, certificate)
|
||||||
|
val certificatePart = Multipart.FormData.BodyPart.Strict("certificate", certificateEntity, Map("filename" -> "telegram-bot.pem"))
|
||||||
|
|
||||||
Some(Multipart.FormData.BodyPart.Strict("certificate", certificateEntity, Map("filename" -> "cert.pem")))
|
val setWebhookFormData = Multipart.FormData.Strict(immutable.Seq(urlPart, certificatePart))
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
|
|
||||||
val formParts = immutable.Seq(urlPart, certificatePart).flatten
|
Marshal(setWebhookFormData)
|
||||||
val formData = Multipart.FormData.Strict(formParts)
|
|
||||||
|
|
||||||
Marshal(formData)
|
|
||||||
.to[RequestEntity]
|
.to[RequestEntity]
|
||||||
.flatMap(requestEntity => http.singleRequest(HttpRequest(uri = botUri.setWebhook, method = HttpMethods.POST, entity = requestEntity)))
|
.flatMap(requestEntity => http.singleRequest(HttpRequest(uri = botUri.setWebhook, method = HttpMethods.POST, entity = requestEntity)))
|
||||||
.onComplete {
|
.onComplete {
|
||||||
|
|||||||
Reference in New Issue
Block a user