From 8b705c8f77b903c9fc8455cd60ed0c45ff16c4e4 Mon Sep 17 00:00:00 2001 From: Pavel Kachalouski Date: Tue, 8 May 2018 22:49:55 +0200 Subject: [PATCH] Initial commit. Playing with VK api for the bot. --- .gitignore | 69 +++++++++++++++++++ build.sbt | 18 +++++ project/Dependencies.scala | 9 +++ project/build.properties | 1 + .../eu/xeppaka/bot1/AccessTokenActor.scala | 40 +++++++++++ .../scala/eu/xeppaka/bot1/DialogActor.scala | 17 +++++ .../scala/eu/xeppaka/bot1/VkApiTests.scala | 31 +++++++++ src/main/scala/example/Hello.scala | 9 +++ src/test/scala/example/HelloSpec.scala | 9 +++ urls.txt | 2 + 10 files changed, 205 insertions(+) create mode 100644 .gitignore create mode 100644 build.sbt create mode 100644 project/Dependencies.scala create mode 100644 project/build.properties create mode 100644 src/main/scala/eu/xeppaka/bot1/AccessTokenActor.scala create mode 100644 src/main/scala/eu/xeppaka/bot1/DialogActor.scala create mode 100644 src/main/scala/eu/xeppaka/bot1/VkApiTests.scala create mode 100644 src/main/scala/example/Hello.scala create mode 100644 src/test/scala/example/HelloSpec.scala create mode 100644 urls.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a375f85 --- /dev/null +++ b/.gitignore @@ -0,0 +1,69 @@ +# Created by .ignore support plugin (hsz.mobi) +### SBT template +# Simple Build Tool +# http://www.scala-sbt.org/release/docs/Getting-Started/Directories.html#configuring-version-control + +dist/* +target/ +lib_managed/ +src_managed/ +project/boot/ +project/plugins/project/ +.history +.cache +.lib/ +### Scala template +*.class +*.log +### JetBrains template +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/**/dictionaries +.idea/**/shelf + +# Sensitive or high-churn files +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml + +# Gradle +.idea/**/gradle.xml +.idea/**/libraries + +# CMake +cmake-build-debug/ +cmake-build-release/ + +# Mongo Explorer plugin +.idea/**/mongoSettings.xml + +# File-based project format +*.iws + +# IntelliJ +out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Cursive Clojure plugin +.idea/replstate.xml + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +# Editor-based Rest Client +.idea/httpRequests diff --git a/build.sbt b/build.sbt new file mode 100644 index 0000000..b2edd24 --- /dev/null +++ b/build.sbt @@ -0,0 +1,18 @@ +import Dependencies._ + +lazy val root = (project in file(".")). + settings( + inThisBuild(List( + organization := "com.example", + scalaVersion := "2.12.6", + version := "0.1.0-SNAPSHOT" + )), + name := "telegram-bot1", + libraryDependencies ++= Seq( + scalaTest % Test, + akka, + akkaHttp, + akkaStream, + vkapi + ) + ) diff --git a/project/Dependencies.scala b/project/Dependencies.scala new file mode 100644 index 0000000..5ae9dd1 --- /dev/null +++ b/project/Dependencies.scala @@ -0,0 +1,9 @@ +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 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/project/build.properties b/project/build.properties new file mode 100644 index 0000000..64cf32f --- /dev/null +++ b/project/build.properties @@ -0,0 +1 @@ +sbt.version=1.1.4 diff --git a/src/main/scala/eu/xeppaka/bot1/AccessTokenActor.scala b/src/main/scala/eu/xeppaka/bot1/AccessTokenActor.scala new file mode 100644 index 0000000..9881d2c --- /dev/null +++ b/src/main/scala/eu/xeppaka/bot1/AccessTokenActor.scala @@ -0,0 +1,40 @@ +package eu.xeppaka.bot1 + +import akka.actor.{Actor, Props} +import akka.http.scaladsl.Http +import akka.http.scaladsl.model.{HttpEntity, HttpRequest, HttpResponse, StatusCodes} +import akka.stream.{ActorMaterializer, ActorMaterializerSettings} +import eu.xeppaka.bot1.AccessTokenActor.GetToken + +import scala.concurrent.duration._ + +class AccessTokenActor extends Actor { + import akka.pattern.pipe + import context.dispatcher + + private val OAUTH_URL_TEMPLATE = "https://oauth.vk.com/authorize?client_id=%d&scope=friends&redirect_uri=https://oauth.vk.com/blank.html&display=page&v=5.74&response_type=token" + private implicit val materializer: ActorMaterializer = ActorMaterializer(ActorMaterializerSettings(context.system)) + private val http = Http(context.system) + + override def receive: Receive = { + case GetToken(clientId) => + val uri = OAUTH_URL_TEMPLATE.format(clientId) + val request = HttpRequest(uri = uri) + + http.singleRequest(request).pipeTo(self) + case HttpResponse(StatusCodes.OK, headers, entity, _) => + println("OK") + entity.withoutSizeLimit().toStrict(1 second).pipeTo(self) + case resp @ HttpResponse(code, _, _, _) => + println(s"Failed with HTTP code $code") + case HttpEntity.Strict(contentType, data) => + println(data.utf8String) + } +} + +object AccessTokenActor { + + case class GetToken(clientId: Int) + + def props: Props = Props[AccessTokenActor] +} diff --git a/src/main/scala/eu/xeppaka/bot1/DialogActor.scala b/src/main/scala/eu/xeppaka/bot1/DialogActor.scala new file mode 100644 index 0000000..ec3f0df --- /dev/null +++ b/src/main/scala/eu/xeppaka/bot1/DialogActor.scala @@ -0,0 +1,17 @@ +package eu.xeppaka.bot1 + +import java.util.UUID + +import akka.actor.Actor + +class DialogActor extends Actor { + private val dialogId = UUID.randomUUID() + + override def receive: Receive = { + case 1 => + } +} + +object DialogActor { + +} \ No newline at end of file diff --git a/src/main/scala/eu/xeppaka/bot1/VkApiTests.scala b/src/main/scala/eu/xeppaka/bot1/VkApiTests.scala new file mode 100644 index 0000000..3b635d5 --- /dev/null +++ b/src/main/scala/eu/xeppaka/bot1/VkApiTests.scala @@ -0,0 +1,31 @@ +package eu.xeppaka.bot1 + +import akka.actor.ActorSystem +import com.vk.api.sdk.client.VkApiClient +import com.vk.api.sdk.client.actors.UserActor +import com.vk.api.sdk.httpclient.HttpTransportClient + +object VkApiTests { + private val APP_ID = 6472591 + private val APP_SECRET = "iGClqTncCZpzEqD8m5Wt" + private val USER_SECRET_CODE = "4180052ce419f0470d" + + private val vkTransportClient = HttpTransportClient.getInstance() + private val vkApiClient = new VkApiClient(vkTransportClient) + + // private val actorSystem = ActorSystem("vk") + + def main(args: Array[String]): Unit = { + // val accessTokenActor = actorSystem.actorOf(AccessTokenActor.props) + // accessTokenActor ! GetToken(APP_ID) + +// val authResponse = vkApiClient.oauth() +// .userAuthorizationCodeFlow(APP_ID, APP_SECRET, "https://oauth.vk.com/blank.html", USER_SECRET_CODE) +// .execute() + + val userActor = new UserActor(6242549, "2ab6508fbc2ff7d2a23fd07cf6b6cb79354abc6c1276447bf2bfb6bfe4bca5cc8d5ecc83dbead6efa04d4") + + val resp = vkApiClient.messages().get(userActor).execute() + println(resp) + } +} diff --git a/src/main/scala/example/Hello.scala b/src/main/scala/example/Hello.scala new file mode 100644 index 0000000..80ea40a --- /dev/null +++ b/src/main/scala/example/Hello.scala @@ -0,0 +1,9 @@ +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 new file mode 100644 index 0000000..56f5e66 --- /dev/null +++ b/src/test/scala/example/HelloSpec.scala @@ -0,0 +1,9 @@ +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/urls.txt b/urls.txt new file mode 100644 index 0000000..8c1307e --- /dev/null +++ b/urls.txt @@ -0,0 +1,2 @@ +https://oauth.vk.com/authorize?client_id=6472591&scope=friends&redirect_uri=https://oauth.vk.com/blank.html&display=mobile&v=5.74&response_type=token +https://oauth.vk.com/authorize?client_id=6472591&scope=friends&redirect_uri=https://oauth.vk.com/blank.html&display=mobile&v=5.74&response_type=code \ No newline at end of file