Playing with certificates to run HTTPS server. Not working yet.
This commit is contained in:
2
.idea/modules/root-build.iml
generated
2
.idea/modules/root-build.iml
generated
@@ -1,5 +1,5 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<module external.linked.project.id="root-build" external.linked.project.path="$MODULE_DIR$/../../project" external.root.project.path="$MODULE_DIR$/../.." external.system.id="SBT" sbt.imports="SUB:DOLLAR484980a9ef0855c97e1c.`root`, _root_.sbt.Keys._, _root_.sbt._, _root_.sbt.plugins.IvyPlugin, _root_.sbt.plugins.JvmPlugin, _root_.sbt.plugins.CorePlugin, _root_.sbt.plugins.JUnitXmlReportPlugin, _root_.sbt.plugins.Giter8TemplatePlugin, _root_.scala.xml.{TopScope=>SUB:DOLLARscope}" sbt.resolvers="https://repo1.maven.org/maven2/|maven|public, file:/Users/pavelkachalouski/.sbt/preloaded/|maven|local-preloaded, /Users/pavelkachalouski/.ivy2/cache|ivy|Local cache" type="SBT_MODULE" version="4">
|
<module external.linked.project.id="root-build" external.linked.project.path="$MODULE_DIR$/../../project" external.root.project.path="$MODULE_DIR$/../.." external.system.id="SBT" sbt.imports="SUB:DOLLAR666c86f869e8681275c2.`root`, _root_.sbt.Keys._, _root_.sbt._, _root_.sbt.plugins.IvyPlugin, _root_.sbt.plugins.JvmPlugin, _root_.sbt.plugins.CorePlugin, _root_.sbt.plugins.JUnitXmlReportPlugin, _root_.sbt.plugins.Giter8TemplatePlugin, _root_.scala.xml.{TopScope=>SUB:DOLLARscope}" sbt.resolvers="https://repo1.maven.org/maven2/|maven|public, file:/home/nnm/.sbt/preloaded/|maven|local-preloaded, /home/nnm/.ivy2/cache|ivy|Local cache" type="SBT_MODULE" version="4">
|
||||||
<component name="NewModuleRootManager">
|
<component name="NewModuleRootManager">
|
||||||
<output url="file://$MODULE_DIR$/../../project/target/idea-classes" />
|
<output url="file://$MODULE_DIR$/../../project/target/idea-classes" />
|
||||||
<output-test url="file://$MODULE_DIR$/../../project/target/idea-test-classes" />
|
<output-test url="file://$MODULE_DIR$/../../project/target/idea-test-classes" />
|
||||||
|
|||||||
83
.idea/modules/root.iml
generated
83
.idea/modules/root.iml
generated
@@ -9,13 +9,51 @@
|
|||||||
<sourceFolder url="file://$MODULE_DIR$/../../src/test/scala" isTestSource="true" />
|
<sourceFolder url="file://$MODULE_DIR$/../../src/test/scala" isTestSource="true" />
|
||||||
<sourceFolder url="file://$MODULE_DIR$/../../target/scala-2.12/src_managed/main" isTestSource="false" generated="true" />
|
<sourceFolder url="file://$MODULE_DIR$/../../target/scala-2.12/src_managed/main" isTestSource="false" generated="true" />
|
||||||
<sourceFolder url="file://$MODULE_DIR$/../../target/scala-2.12/src_managed/test" isTestSource="true" generated="true" />
|
<sourceFolder url="file://$MODULE_DIR$/../../target/scala-2.12/src_managed/test" isTestSource="true" generated="true" />
|
||||||
|
<sourceFolder url="file://$MODULE_DIR$/../../src/main/resources" type="java-resource" />
|
||||||
<excludeFolder url="file://$MODULE_DIR$/../../target" />
|
<excludeFolder url="file://$MODULE_DIR$/../../target" />
|
||||||
</content>
|
</content>
|
||||||
<orderEntry type="inheritedJdk" />
|
<orderEntry type="inheritedJdk" />
|
||||||
<orderEntry type="sourceFolder" forTests="false" />
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
<orderEntry type="library" name="sbt: com.google.code.gson:gson:2.8.1:jar" level="project" />
|
<orderEntry type="library" name="sbt: com.google.code.gson:gson:2.8.1:jar" level="project" />
|
||||||
<orderEntry type="library" name="sbt: io.spray:spray-json_2.12:1.3.4:jar" level="project" />
|
<orderEntry type="library" name="sbt: com.typesafe:config:1.3.2:jar" level="project" />
|
||||||
<orderEntry type="library" name="sbt: com.typesafe.akka:akka-http-spray-json_2.12:10.1.1:jar" level="project" />
|
<orderEntry type="library" name="sbt: com.typesafe:ssl-config-core_2.12:0.2.3:jar" level="project" />
|
||||||
|
<orderEntry type="library" name="sbt: com.typesafe.akka:akka-actor_2.12:2.5.12:jar" level="project" />
|
||||||
|
<orderEntry type="library" name="sbt: com.typesafe.akka:akka-http-core_2.12:10.1.1:jar" level="project" />
|
||||||
|
<orderEntry type="library" name="sbt: com.typesafe.akka:akka-http_2.12:10.1.1:jar" level="project" />
|
||||||
|
<orderEntry type="library" name="sbt: com.typesafe.akka:akka-parsing_2.12:10.1.1:jar" level="project" />
|
||||||
|
<orderEntry type="library" name="sbt: com.typesafe.akka:akka-protobuf_2.12:2.5.12:jar" level="project" />
|
||||||
|
<orderEntry type="library" name="sbt: com.typesafe.akka:akka-stream_2.12:2.5.12:jar" level="project" />
|
||||||
|
<orderEntry type="library" name="sbt: com.typesafe.netty:netty-reactive-streams:1.0.8:jar" level="project" />
|
||||||
|
<orderEntry type="library" name="sbt: com.vk.api:sdk:0.5.12:jar" level="project" />
|
||||||
|
<orderEntry type="library" name="sbt: commons-codec:commons-codec:1.9:jar" level="project" />
|
||||||
|
<orderEntry type="library" name="sbt: commons-io:commons-io:2.5:jar" level="project" />
|
||||||
|
<orderEntry type="library" name="sbt: commons-logging:commons-logging:1.2:jar" level="project" />
|
||||||
|
<orderEntry type="library" name="sbt: io.netty:netty-buffer:4.0.48.Final:jar" level="project" />
|
||||||
|
<orderEntry type="library" name="sbt: io.netty:netty-codec:4.0.48.Final:jar" level="project" />
|
||||||
|
<orderEntry type="library" name="sbt: io.netty:netty-codec-http:4.0.48.Final:jar" level="project" />
|
||||||
|
<orderEntry type="library" name="sbt: io.netty:netty-common:4.0.48.Final:jar" level="project" />
|
||||||
|
<orderEntry type="library" name="sbt: io.netty:netty-handler:4.0.48.Final:jar" level="project" />
|
||||||
|
<orderEntry type="library" name="sbt: io.netty:netty-transport:4.0.48.Final:jar" level="project" />
|
||||||
|
<orderEntry type="library" name="sbt: io.netty:netty-transport-native-epoll:4.0.48.Final:linux-x86_64:jar" level="project" />
|
||||||
|
<orderEntry type="library" name="sbt: org.apache.commons:commons-collections4:4.1:jar" level="project" />
|
||||||
|
<orderEntry type="library" name="sbt: org.apache.commons:commons-lang3:3.6:jar" level="project" />
|
||||||
|
<orderEntry type="library" name="sbt: org.apache.httpcomponents:httpclient:4.5.3:jar" level="project" />
|
||||||
|
<orderEntry type="library" name="sbt: org.apache.httpcomponents:httpcore:4.4.6:jar" level="project" />
|
||||||
|
<orderEntry type="library" name="sbt: org.apache.httpcomponents:httpmime:4.5.3:jar" level="project" />
|
||||||
|
<orderEntry type="library" name="sbt: org.asynchttpclient:async-http-client:2.0.33:jar" level="project" />
|
||||||
|
<orderEntry type="library" name="sbt: org.asynchttpclient:async-http-client-netty-utils:2.0.33:jar" level="project" />
|
||||||
|
<orderEntry type="library" name="sbt: org.asynchttpclient:netty-codec-dns:2.0.33:jar" level="project" />
|
||||||
|
<orderEntry type="library" name="sbt: org.asynchttpclient:netty-resolver:2.0.33:jar" level="project" />
|
||||||
|
<orderEntry type="library" name="sbt: org.asynchttpclient:netty-resolver-dns:2.0.33:jar" level="project" />
|
||||||
|
<orderEntry type="library" name="sbt: org.reactivestreams:reactive-streams:1.0.2:jar" level="project" />
|
||||||
|
<orderEntry type="library" name="sbt: org.scala-lang:scala-library:2.12.6:jar" level="project" />
|
||||||
|
<orderEntry type="library" scope="TEST" name="sbt: org.scala-lang:scala-reflect:2.12.6:jar" level="project" />
|
||||||
|
<orderEntry type="library" name="sbt: org.scala-lang.modules:scala-java8-compat_2.12:0.8.0:jar" level="project" />
|
||||||
|
<orderEntry type="library" name="sbt: org.scala-lang.modules:scala-parser-combinators_2.12:1.1.0:jar" level="project" />
|
||||||
|
<orderEntry type="library" scope="TEST" name="sbt: org.scala-lang.modules:scala-xml_2.12:1.0.6:jar" level="project" />
|
||||||
|
<orderEntry type="library" scope="TEST" name="sbt: org.scalactic:scalactic_2.12:3.0.5:jar" level="project" />
|
||||||
|
<orderEntry type="library" scope="TEST" name="sbt: org.scalatest:scalatest_2.12:3.0.5:jar" level="project" />
|
||||||
|
<orderEntry type="library" name="sbt: org.slf4j:slf4j-api:1.7.25:jar" level="project" />
|
||||||
<orderEntry type="module-library">
|
<orderEntry type="module-library">
|
||||||
<library name="sbt: unmanaged-sources-and-docs">
|
<library name="sbt: unmanaged-sources-and-docs">
|
||||||
<CLASSES />
|
<CLASSES />
|
||||||
@@ -25,44 +63,7 @@
|
|||||||
</SOURCES>
|
</SOURCES>
|
||||||
</library>
|
</library>
|
||||||
</orderEntry>
|
</orderEntry>
|
||||||
<orderEntry type="library" name="sbt: org.slf4j:slf4j-api:1.7.25:jar" level="project" />
|
<orderEntry type="library" name="sbt: com.typesafe.akka:akka-http-spray-json_2.12:10.1.1:jar" level="project" />
|
||||||
<orderEntry type="library" scope="TEST" name="sbt: org.scalatest:scalatest_2.12:3.0.5:jar" level="project" />
|
<orderEntry type="library" name="sbt: io.spray:spray-json_2.12:1.3.4:jar" level="project" />
|
||||||
<orderEntry type="library" scope="TEST" name="sbt: org.scalactic:scalactic_2.12:3.0.5:jar" level="project" />
|
|
||||||
<orderEntry type="library" scope="TEST" name="sbt: org.scala-lang.modules:scala-xml_2.12:1.0.6:jar" level="project" />
|
|
||||||
<orderEntry type="library" name="sbt: org.scala-lang.modules:scala-parser-combinators_2.12:1.1.0:jar" level="project" />
|
|
||||||
<orderEntry type="library" name="sbt: org.scala-lang.modules:scala-java8-compat_2.12:0.8.0:jar" level="project" />
|
|
||||||
<orderEntry type="library" scope="TEST" name="sbt: org.scala-lang:scala-reflect:2.12.6:jar" level="project" />
|
|
||||||
<orderEntry type="library" name="sbt: org.scala-lang:scala-library:2.12.6:jar" level="project" />
|
|
||||||
<orderEntry type="library" name="sbt: org.reactivestreams:reactive-streams:1.0.2:jar" level="project" />
|
|
||||||
<orderEntry type="library" name="sbt: org.asynchttpclient:netty-resolver-dns:2.0.33:jar" level="project" />
|
|
||||||
<orderEntry type="library" name="sbt: org.asynchttpclient:netty-resolver:2.0.33:jar" level="project" />
|
|
||||||
<orderEntry type="library" name="sbt: org.asynchttpclient:netty-codec-dns:2.0.33:jar" level="project" />
|
|
||||||
<orderEntry type="library" name="sbt: org.asynchttpclient:async-http-client-netty-utils:2.0.33:jar" level="project" />
|
|
||||||
<orderEntry type="library" name="sbt: org.asynchttpclient:async-http-client:2.0.33:jar" level="project" />
|
|
||||||
<orderEntry type="library" name="sbt: org.apache.httpcomponents:httpmime:4.5.3:jar" level="project" />
|
|
||||||
<orderEntry type="library" name="sbt: org.apache.httpcomponents:httpcore:4.4.6:jar" level="project" />
|
|
||||||
<orderEntry type="library" name="sbt: org.apache.httpcomponents:httpclient:4.5.3:jar" level="project" />
|
|
||||||
<orderEntry type="library" name="sbt: org.apache.commons:commons-lang3:3.6:jar" level="project" />
|
|
||||||
<orderEntry type="library" name="sbt: org.apache.commons:commons-collections4:4.1:jar" level="project" />
|
|
||||||
<orderEntry type="library" name="sbt: io.netty:netty-transport-native-epoll:4.0.48.Final:linux-x86_64:jar" level="project" />
|
|
||||||
<orderEntry type="library" name="sbt: io.netty:netty-transport:4.0.48.Final:jar" level="project" />
|
|
||||||
<orderEntry type="library" name="sbt: io.netty:netty-handler:4.0.48.Final:jar" level="project" />
|
|
||||||
<orderEntry type="library" name="sbt: io.netty:netty-common:4.0.48.Final:jar" level="project" />
|
|
||||||
<orderEntry type="library" name="sbt: io.netty:netty-codec-http:4.0.48.Final:jar" level="project" />
|
|
||||||
<orderEntry type="library" name="sbt: io.netty:netty-codec:4.0.48.Final:jar" level="project" />
|
|
||||||
<orderEntry type="library" name="sbt: io.netty:netty-buffer:4.0.48.Final:jar" level="project" />
|
|
||||||
<orderEntry type="library" name="sbt: commons-logging:commons-logging:1.2:jar" level="project" />
|
|
||||||
<orderEntry type="library" name="sbt: commons-io:commons-io:2.5:jar" level="project" />
|
|
||||||
<orderEntry type="library" name="sbt: commons-codec:commons-codec:1.9:jar" level="project" />
|
|
||||||
<orderEntry type="library" name="sbt: com.vk.api:sdk:0.5.12:jar" level="project" />
|
|
||||||
<orderEntry type="library" name="sbt: com.typesafe.netty:netty-reactive-streams:1.0.8:jar" level="project" />
|
|
||||||
<orderEntry type="library" name="sbt: com.typesafe.akka:akka-stream_2.12:2.5.12:jar" level="project" />
|
|
||||||
<orderEntry type="library" name="sbt: com.typesafe.akka:akka-protobuf_2.12:2.5.12:jar" level="project" />
|
|
||||||
<orderEntry type="library" name="sbt: com.typesafe.akka:akka-parsing_2.12:10.1.1:jar" level="project" />
|
|
||||||
<orderEntry type="library" name="sbt: com.typesafe.akka:akka-http_2.12:10.1.1:jar" level="project" />
|
|
||||||
<orderEntry type="library" name="sbt: com.typesafe.akka:akka-http-core_2.12:10.1.1:jar" level="project" />
|
|
||||||
<orderEntry type="library" name="sbt: com.typesafe.akka:akka-actor_2.12:2.5.12:jar" level="project" />
|
|
||||||
<orderEntry type="library" name="sbt: com.typesafe:ssl-config-core_2.12:0.2.3:jar" level="project" />
|
|
||||||
<orderEntry type="library" name="sbt: com.typesafe:config:1.3.2:jar" level="project" />
|
|
||||||
</component>
|
</component>
|
||||||
</module>
|
</module>
|
||||||
3
src/main/resources/application.conf
Normal file
3
src/main/resources/application.conf
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
akka {
|
||||||
|
loglevel = "DEBUG"
|
||||||
|
}
|
||||||
15
src/main/scala/eu/xeppaka/bot1/BotUri.scala
Normal file
15
src/main/scala/eu/xeppaka/bot1/BotUri.scala
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
package eu.xeppaka.bot1
|
||||||
|
|
||||||
|
import akka.http.scaladsl.model.Uri
|
||||||
|
|
||||||
|
case class BotUri(botId: String) {
|
||||||
|
private val baseUri = Uri(s"https://api.telegram.org/bot$botId")
|
||||||
|
|
||||||
|
val getMe: Uri = baseUri.withPath(baseUri.path / "getMe")
|
||||||
|
|
||||||
|
val setWebhook: Uri = baseUri.withPath(baseUri.path / "setWebhook")
|
||||||
|
|
||||||
|
val deleteWebhook: Uri = baseUri.withPath(baseUri.path / "deleteWebhook")
|
||||||
|
|
||||||
|
val getWebhookInfo: Uri = baseUri.withPath(baseUri.path / "getWebhookInfo")
|
||||||
|
}
|
||||||
@@ -1,39 +1,46 @@
|
|||||||
package eu.xeppaka.bot1
|
package eu.xeppaka.bot1
|
||||||
|
|
||||||
|
import java.io.InputStream
|
||||||
|
import java.security.{KeyStore, SecureRandom}
|
||||||
|
import java.util.UUID
|
||||||
|
|
||||||
import akka.actor.ActorSystem
|
import akka.actor.ActorSystem
|
||||||
import akka.http.scaladsl.Http
|
import akka.http.scaladsl.marshalling.Marshal
|
||||||
|
import akka.http.scaladsl.model._
|
||||||
import akka.http.scaladsl.server.Directives._
|
import akka.http.scaladsl.server.Directives._
|
||||||
|
import akka.http.scaladsl.server.directives.LoggingMagnet
|
||||||
import akka.http.scaladsl.server.{Route, RouteResult}
|
import akka.http.scaladsl.server.{Route, RouteResult}
|
||||||
|
import akka.http.scaladsl.unmarshalling.Unmarshal
|
||||||
|
import akka.http.scaladsl.{ConnectionContext, Http, HttpExt, HttpsConnectionContext}
|
||||||
import akka.stream.ActorMaterializer
|
import akka.stream.ActorMaterializer
|
||||||
|
import com.typesafe.sslconfig.akka.AkkaSSLConfig
|
||||||
|
import javax.net.ssl.{KeyManagerFactory, SSLContext, TrustManagerFactory}
|
||||||
|
|
||||||
import scala.concurrent.{ExecutionContextExecutor, Future}
|
import scala.concurrent.{ExecutionContextExecutor, Future}
|
||||||
import scala.io.StdIn
|
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}
|
import scala.util.{Failure, Success}
|
||||||
|
|
||||||
trait JsonSupport extends SprayJsonSupport with DefaultJsonProtocol {
|
class TelegramBotServer(botId: String, port: Int, httpsContext: Option[HttpsConnectionContext])(implicit val actorSystem: ActorSystem) {
|
||||||
implicit val getMeFormat: RootJsonFormat[GetMe] = jsonFormat4(GetMe)
|
|
||||||
implicit val responseFormat: RootJsonFormat[Response[GetMe]] = jsonFormat4(Response[GetMe])
|
|
||||||
}
|
|
||||||
|
|
||||||
class TelegramBotServer extends JsonSupport {
|
import eu.xeppaka.bot1.TelegramEntities._
|
||||||
|
|
||||||
def run(): Unit = {
|
private val botUri = BotUri(botId)
|
||||||
implicit val actorSystem: ActorSystem = ActorSystem("telegram-bot")
|
private implicit val materializer: ActorMaterializer = ActorMaterializer()
|
||||||
implicit val materializer: ActorMaterializer = ActorMaterializer()
|
private implicit val executionContext: ExecutionContextExecutor = actorSystem.dispatcher
|
||||||
implicit val executionContext: ExecutionContextExecutor = actorSystem.dispatcher
|
|
||||||
|
|
||||||
val bindingFuture = Http().bindAndHandle(botRoutes(), "localhost", 8080)
|
private val http: HttpExt = Http()
|
||||||
|
private val hookId = UUID.randomUUID().toString
|
||||||
|
private val webhookUri = Uri(s"https://xeppaka.eu:8443/$hookId")
|
||||||
|
private val bindingFuture = http.bindAndHandle(botRoutes(hookId),
|
||||||
|
"127.0.0.1",
|
||||||
|
port,
|
||||||
|
connectionContext = httpsContext.getOrElse(http.defaultClientHttpsContext))
|
||||||
|
|
||||||
StdIn.readLine()
|
println(s"webhook path: $webhookUri")
|
||||||
|
|
||||||
|
def stop(): Unit = {
|
||||||
bindingFuture
|
bindingFuture
|
||||||
|
.andThen { case _ => http.shutdownAllConnectionPools() }
|
||||||
.flatMap(_.unbind())
|
.flatMap(_.unbind())
|
||||||
.onComplete(_ => actorSystem.terminate())
|
.onComplete(_ => actorSystem.terminate())
|
||||||
}
|
}
|
||||||
@@ -43,11 +50,11 @@ class TelegramBotServer extends JsonSupport {
|
|||||||
println(res)
|
println(res)
|
||||||
}
|
}
|
||||||
|
|
||||||
def botRoutes()(implicit actorSystem: ActorSystem, materializer: ActorMaterializer, executionContext: ExecutionContextExecutor): Route = {
|
def botRoutes(hookId: String): Route = {
|
||||||
path("test") {
|
path(hookId) {
|
||||||
get {
|
post {
|
||||||
logRequestResult(LoggingMagnet(_ => printRequestMethodAndResponseStatus)) {
|
logRequestResult(LoggingMagnet(_ => printRequestMethodAndResponseStatus)) {
|
||||||
onComplete(getBotInfo()) {
|
onComplete(getBotInfo) {
|
||||||
case Success(res) => complete(res.ok.toString)
|
case Success(res) => complete(res.ok.toString)
|
||||||
case Failure(ex) => complete(StatusCodes.InternalServerError, "Boooom!")
|
case Failure(ex) => complete(StatusCodes.InternalServerError, "Boooom!")
|
||||||
}
|
}
|
||||||
@@ -56,17 +63,73 @@ class TelegramBotServer extends JsonSupport {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
def getBotInfo()(implicit actorSystem: ActorSystem, materializer: ActorMaterializer, executionContext: ExecutionContextExecutor): Future[Response[GetMe]] = {
|
def getBotInfo: Future[Response[GetMe]] = {
|
||||||
Http().singleRequest(HttpRequest(uri = "https://api.telegram.org/bot570855144:AAEv7b817cuq2JJI9f2kG5B9G3zW1x-btz4/getMe")).flatMap(Unmarshal(_).to[Response[GetMe]])
|
http.singleRequest(HttpRequest(uri = botUri.getMe)).flatMap(Unmarshal(_).to[Response[GetMe]])
|
||||||
}
|
}
|
||||||
|
|
||||||
def setWebhook(): Unit = {
|
def setWebhook(): Future[HttpResponse] = {
|
||||||
|
val hook = Webhook(webhookUri.toString())
|
||||||
|
Marshal(hook)
|
||||||
|
.to[MessageEntity]
|
||||||
|
.flatMap(entity => http.singleRequest(HttpRequest(uri = botUri.setWebhook, method = HttpMethods.POST, entity = entity)))
|
||||||
|
// .flatMap(Unmarshal(_).to[Response[String]])
|
||||||
|
}
|
||||||
|
|
||||||
|
def deleteWebhook(): Future[Response[String]] = {
|
||||||
|
http
|
||||||
|
.singleRequest(HttpRequest(uri = botUri.deleteWebhook, method = HttpMethods.POST))
|
||||||
|
.flatMap(Unmarshal(_).to[Response[String]])
|
||||||
|
}
|
||||||
|
|
||||||
|
def getWebhookInfo(): Future[Response[WebhookInfo]] = {
|
||||||
|
http
|
||||||
|
.singleRequest(HttpRequest(uri = botUri.getWebhookInfo, method = HttpMethods.GET))
|
||||||
|
.flatMap(Unmarshal(_).to[Response[WebhookInfo]])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
object TelegramBotServer {
|
object TelegramBotServer {
|
||||||
|
private val botId = "570855144:AAEv7b817cuq2JJI9f2kG5B9G3zW1x-btz4"
|
||||||
|
|
||||||
|
def apply(port: Int, httpsContext: Option[HttpsConnectionContext])(implicit actorSystem: ActorSystem): TelegramBotServer = new TelegramBotServer(botId, port, httpsContext)(actorSystem)
|
||||||
|
|
||||||
def main(args: Array[String]): Unit = {
|
def main(args: Array[String]): Unit = {
|
||||||
new TelegramBotServer().run()
|
val httpsContext = createHttpsConnectionContext
|
||||||
|
|
||||||
|
implicit val actorSystem: ActorSystem = ActorSystem("telegram-bot")
|
||||||
|
implicit val executionContext: ExecutionContextExecutor = actorSystem.dispatcher
|
||||||
|
|
||||||
|
val tbs = TelegramBotServer(8443, Some(createHttpsConnectionContext))
|
||||||
|
//tbs.setWebhook()
|
||||||
|
|
||||||
|
tbs
|
||||||
|
.getWebhookInfo()
|
||||||
|
.onComplete(println(_))
|
||||||
|
|
||||||
|
StdIn.readLine()
|
||||||
|
|
||||||
|
tbs.deleteWebhook()
|
||||||
|
.onComplete(r => tbs.stop())
|
||||||
|
}
|
||||||
|
|
||||||
|
def createHttpsConnectionContext: HttpsConnectionContext = {
|
||||||
|
val password: Array[Char] = "changeit".toCharArray // do not store passwords in code, read them from somewhere safe!
|
||||||
|
|
||||||
|
val ks: KeyStore = KeyStore.getInstance("PKCS12")
|
||||||
|
val keystore: InputStream = getClass.getClassLoader.getResourceAsStream("server.p12")
|
||||||
|
|
||||||
|
require(keystore != null, "Keystore required!")
|
||||||
|
ks.load(keystore, password)
|
||||||
|
|
||||||
|
val keyManagerFactory: KeyManagerFactory = KeyManagerFactory.getInstance("SunX509")
|
||||||
|
keyManagerFactory.init(ks, password)
|
||||||
|
|
||||||
|
val tmf: TrustManagerFactory = TrustManagerFactory.getInstance("SunX509")
|
||||||
|
tmf.init(ks)
|
||||||
|
|
||||||
|
val sslContext: SSLContext = SSLContext.getInstance("TLS")
|
||||||
|
sslContext.init(keyManagerFactory.getKeyManagers, tmf.getTrustManagers, new SecureRandom)
|
||||||
|
|
||||||
|
ConnectionContext.https(sslContext)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,13 @@
|
|||||||
package eu.xeppaka.bot1
|
package eu.xeppaka.bot1
|
||||||
|
|
||||||
object TelegramEntities {
|
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport
|
||||||
|
import spray.json._
|
||||||
|
|
||||||
|
object TelegramEntities extends SprayJsonSupport with DefaultJsonProtocol {
|
||||||
|
|
||||||
case class Response[T](ok: Boolean,
|
case class Response[T](ok: Boolean,
|
||||||
description: Option[String],
|
description: Option[String] = None,
|
||||||
error_code: Option[Int],
|
error_code: Option[Int] = None,
|
||||||
result: T)
|
result: T)
|
||||||
|
|
||||||
case class GetMe(id: Int, is_bot: Boolean, first_name: String, username: String)
|
case class GetMe(id: Int, is_bot: Boolean, first_name: String, username: String)
|
||||||
@@ -19,29 +22,29 @@ object TelegramEntities {
|
|||||||
latitude: Float)
|
latitude: Float)
|
||||||
|
|
||||||
case class Update(update_id: Int,
|
case class Update(update_id: Int,
|
||||||
message: Option[Message],
|
message: Option[Message] = None,
|
||||||
edited_message: Option[Message],
|
edited_message: Option[Message] = None,
|
||||||
channel_post: Option[Message],
|
channel_post: Option[Message] = None,
|
||||||
edited_channel_post: Option[Message],
|
edited_channel_post: Option[Message] = None,
|
||||||
inline_query: Option[InlineQuery],
|
inline_query: Option[InlineQuery] = None,
|
||||||
chosen_inline_result: Option[ChosenInlineResult],
|
chosen_inline_result: Option[ChosenInlineResult] = None,
|
||||||
callback_query: Option[CallbackQuery],
|
callback_query: Option[CallbackQuery] = None,
|
||||||
shipping_query: Option[ShippingQuery],
|
shipping_query: Option[ShippingQuery] = None,
|
||||||
pre_checkout_query: Option[PreCheckoutQuery])
|
pre_checkout_query: Option[PreCheckoutQuery] = None)
|
||||||
|
|
||||||
case class ChosenInlineResult(result_id: String,
|
case class ChosenInlineResult(result_id: String,
|
||||||
from: User,
|
from: User,
|
||||||
location: Option[Location],
|
location: Option[Location] = None,
|
||||||
inline_message_id: Option[String],
|
inline_message_id: Option[String] = None,
|
||||||
query: String)
|
query: String)
|
||||||
|
|
||||||
case class CallbackQuery(id: String,
|
case class CallbackQuery(id: String,
|
||||||
from: User,
|
from: User,
|
||||||
message: Option[Message],
|
message: Option[Message] = None,
|
||||||
inline_message_id: Option[String],
|
inline_message_id: Option[String] = None,
|
||||||
chat_instance: String,
|
chat_instance: String,
|
||||||
data: Option[String],
|
data: Option[String] = None,
|
||||||
game_short_name: Option[String])
|
game_short_name: Option[String] = None)
|
||||||
|
|
||||||
case class ShippingQuery(id: String,
|
case class ShippingQuery(id: String,
|
||||||
from: User,
|
from: User,
|
||||||
@@ -60,20 +63,20 @@ object TelegramEntities {
|
|||||||
currency: String,
|
currency: String,
|
||||||
total_amount: Int,
|
total_amount: Int,
|
||||||
invoice_payload: String,
|
invoice_payload: String,
|
||||||
shipping_option_id: Option[String],
|
shipping_option_id: Option[String] = None,
|
||||||
order_info: Option[OrderInfo])
|
order_info: Option[OrderInfo] = None)
|
||||||
|
|
||||||
case class OrderInfo(name: Option[String],
|
case class OrderInfo(name: Option[String] = None,
|
||||||
phone_number: Option[String],
|
phone_number: Option[String] = None,
|
||||||
email: Option[String],
|
email: Option[String] = None,
|
||||||
shipping_address: Option[ShippingAddress])
|
shipping_address: Option[ShippingAddress] = None)
|
||||||
|
|
||||||
case class User(id: Int,
|
case class User(id: Int,
|
||||||
is_bot: Boolean,
|
is_bot: Boolean,
|
||||||
first_name: String,
|
first_name: String,
|
||||||
last_name: Option[String],
|
last_name: Option[String] = None,
|
||||||
username: Option[String],
|
username: Option[String] = None,
|
||||||
language_code: Option[String])
|
language_code: Option[String] = None)
|
||||||
|
|
||||||
case class Message()
|
case class Message()
|
||||||
|
|
||||||
@@ -81,23 +84,39 @@ object TelegramEntities {
|
|||||||
|
|
||||||
case class Chat(id: Int,
|
case class Chat(id: Int,
|
||||||
`type`: String,
|
`type`: String,
|
||||||
title: Option[String],
|
title: Option[String] = None,
|
||||||
username: Option[String],
|
username: Option[String] = None,
|
||||||
first_name: Option[String],
|
first_name: Option[String] = None,
|
||||||
last_name: Option[String],
|
last_name: Option[String] = None,
|
||||||
all_members_are_administrators: Option[Boolean],
|
all_members_are_administrators: Option[Boolean] = None,
|
||||||
photo: Option[ChatPhoto],
|
photo: Option[ChatPhoto] = None,
|
||||||
description: Option[String],
|
description: Option[String] = None,
|
||||||
invite_link: Option[String],
|
invite_link: Option[String] = None,
|
||||||
pinned_message: Option[Message],
|
pinned_message: Option[Message] = None,
|
||||||
sticker_set_name: Option[String],
|
sticker_set_name: Option[String] = None,
|
||||||
can_set_sticker_set: Option[Boolean]
|
can_set_sticker_set: Option[Boolean] = None)
|
||||||
)
|
|
||||||
|
|
||||||
case class InputFile()
|
case class InputFile()
|
||||||
|
|
||||||
case class Webhook(url: String,
|
case class Webhook(url: String,
|
||||||
certificate: Option[InputFile],
|
certificate: Option[InputFile] = None,
|
||||||
max_connections: Option[Int],
|
max_connections: Option[Int] = None,
|
||||||
allowed_updates: Option[Seq[String]])
|
allowed_updates: Option[Seq[String]] = None)
|
||||||
|
|
||||||
|
case class WebhookInfo(url: String,
|
||||||
|
has_custom_certificate: Boolean,
|
||||||
|
pending_update_count: Int,
|
||||||
|
last_error_date: Option[Int] = None,
|
||||||
|
last_error_message: Option[String] = None,
|
||||||
|
max_connections: Option[Int] = None,
|
||||||
|
allowed_updates: Option[Seq[String]] = None)
|
||||||
|
|
||||||
|
implicit val inputFileFormat: RootJsonFormat[InputFile] = jsonFormat0(InputFile)
|
||||||
|
implicit val webHookFormat: RootJsonFormat[Webhook] = jsonFormat4(Webhook)
|
||||||
|
implicit val webHookInfoFormat: RootJsonFormat[WebhookInfo] = jsonFormat7(WebhookInfo)
|
||||||
|
implicit val getMeFormat: RootJsonFormat[GetMe] = jsonFormat4(GetMe)
|
||||||
|
// responses
|
||||||
|
implicit val responseGetMeFormat: RootJsonFormat[Response[GetMe]] = jsonFormat4(Response[GetMe])
|
||||||
|
implicit val responseWebhookInfoFormat: RootJsonFormat[Response[WebhookInfo]] = jsonFormat4(Response[WebhookInfo])
|
||||||
|
implicit val responseStringFormat: RootJsonFormat[Response[String]] = jsonFormat4(Response[String])
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user