From a91a352302ca327d5f108ec63c92dc42326beb08 Mon Sep 17 00:00:00 2001 From: Pavel Kachalouski Date: Thu, 10 May 2018 20:00:56 +0200 Subject: [PATCH] More telegram entities. --- .idea/hydra.xml | 9 ++ .idea/modules/root-build.iml | 104 ++++++++++++++++++ .idea/modules/root.iml | 68 ++++++++++++ ...egramBot.scala => TelegramBotServer.scala} | 26 ++--- .../eu/xeppaka/bot1/TelegramEntities.scala | 104 ++++++++++++++---- .../scala/eu/xeppaka/bot1/VkApiTests.scala | 2 +- 6 files changed, 275 insertions(+), 38 deletions(-) create mode 100644 .idea/hydra.xml create mode 100644 .idea/modules/root-build.iml create mode 100644 .idea/modules/root.iml rename src/main/scala/eu/xeppaka/bot1/{TelegramBot.scala => TelegramBotServer.scala} (77%) diff --git a/.idea/hydra.xml b/.idea/hydra.xml new file mode 100644 index 0000000..66eeb9a --- /dev/null +++ b/.idea/hydra.xml @@ -0,0 +1,9 @@ + + + + + \ No newline at end of file diff --git a/.idea/modules/root-build.iml b/.idea/modules/root-build.iml new file mode 100644 index 0000000..ee53ba2 --- /dev/null +++ b/.idea/modules/root-build.iml @@ -0,0 +1,104 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules/root.iml b/.idea/modules/root.iml new file mode 100644 index 0000000..46ec688 --- /dev/null +++ b/.idea/modules/root.iml @@ -0,0 +1,68 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/scala/eu/xeppaka/bot1/TelegramBot.scala b/src/main/scala/eu/xeppaka/bot1/TelegramBotServer.scala similarity index 77% rename from src/main/scala/eu/xeppaka/bot1/TelegramBot.scala rename to src/main/scala/eu/xeppaka/bot1/TelegramBotServer.scala index 3bccab2..493b90e 100644 --- a/src/main/scala/eu/xeppaka/bot1/TelegramBot.scala +++ b/src/main/scala/eu/xeppaka/bot1/TelegramBotServer.scala @@ -18,19 +18,11 @@ import spray.json._ import scala.util.{Failure, Success} trait JsonSupport extends SprayJsonSupport with DefaultJsonProtocol { - implicit object getMeFormat extends RootJsonFormat[GetMe] { - override def write(obj: GetMe): JsValue = ??? - - override def read(json: JsValue): GetMe = - json.asJsObject.getFields("id", "is_bot", "first_name", "username") match { - case Seq(JsNumber(id), JsBoolean(isBot), JsString(firstName), JsString(userName)) => GetMe(id.toInt, isBot, firstName, userName) - case _ => throw new DeserializationException("GetMe deserialize error") - } - } - implicit val responseFormat = jsonFormat2(Response[GetMe]) + implicit val getMeFormat: RootJsonFormat[GetMe] = jsonFormat4(GetMe) + implicit val responseFormat: RootJsonFormat[Response[GetMe]] = jsonFormat4(Response[GetMe]) } -class TelegramBot extends JsonSupport { +class TelegramBotServer extends JsonSupport { def run(): Unit = { implicit val actorSystem: ActorSystem = ActorSystem("telegram-bot") @@ -67,10 +59,14 @@ class TelegramBot extends JsonSupport { def getBotInfo()(implicit actorSystem: ActorSystem, materializer: ActorMaterializer, executionContext: ExecutionContextExecutor): Future[Response[GetMe]] = { Http().singleRequest(HttpRequest(uri = "https://api.telegram.org/bot570855144:AAEv7b817cuq2JJI9f2kG5B9G3zW1x-btz4/getMe")).flatMap(Unmarshal(_).to[Response[GetMe]]) } -} -object TelegramBot { - def main(args: Array[String]): Unit = { - new TelegramBot().run() + def setWebhook(): Unit = { + + } +} + +object TelegramBotServer { + def main(args: Array[String]): Unit = { + new TelegramBotServer().run() } } diff --git a/src/main/scala/eu/xeppaka/bot1/TelegramEntities.scala b/src/main/scala/eu/xeppaka/bot1/TelegramEntities.scala index 2f9a544..30c4c11 100644 --- a/src/main/scala/eu/xeppaka/bot1/TelegramEntities.scala +++ b/src/main/scala/eu/xeppaka/bot1/TelegramEntities.scala @@ -3,41 +3,101 @@ package eu.xeppaka.bot1 object TelegramEntities { case class Response[T](ok: Boolean, -// description: Option[String], -// errorCode: Option[Int], + description: Option[String], + error_code: Option[Int], result: T) - case class GetMe(id: Int, isBot: Boolean, firstName: String, userName: String) + case class GetMe(id: Int, is_bot: Boolean, first_name: String, username: String) - case class Update(updateId: Int, + case class InlineQuery(id: String, + from: User, + location: Location, + query: String, + offset: String) + + case class Location(longitude: Float, + latitude: Float) + + case class Update(update_id: Int, message: Option[Message], - editedMessage: Option[Message], - channelPost: Option[Message], - editedChannelPost: Option[Message]) + edited_message: Option[Message], + channel_post: Option[Message], + edited_channel_post: Option[Message], + inline_query: Option[InlineQuery], + chosen_inline_result: Option[ChosenInlineResult], + callback_query: Option[CallbackQuery], + shipping_query: Option[ShippingQuery], + pre_checkout_query: Option[PreCheckoutQuery]) + + case class ChosenInlineResult(result_id: String, + from: User, + location: Option[Location], + inline_message_id: Option[String], + query: String) + + case class CallbackQuery(id: String, + from: User, + message: Option[Message], + inline_message_id: Option[String], + chat_instance: String, + data: Option[String], + game_short_name: Option[String]) + + case class ShippingQuery(id: String, + from: User, + invoice_payload: String, + shipping_address: ShippingAddress) + + case class ShippingAddress(country_code: String, + state: String, + city: String, + street_line1: String, + street_line2: String, + post_code: String) + + case class PreCheckoutQuery(id: String, + from: User, + currency: String, + total_amount: Int, + invoice_payload: String, + shipping_option_id: Option[String], + order_info: Option[OrderInfo]) + + case class OrderInfo(name: Option[String], + phone_number: Option[String], + email: Option[String], + shipping_address: Option[ShippingAddress]) case class User(id: Int, - isBot: Boolean, - firstName: String, - lastName: Option[String], - userName: Option[String], - languageCode: Option[String]) + is_bot: Boolean, + first_name: String, + last_name: Option[String], + username: Option[String], + language_code: Option[String]) case class Message() - case class ChatPhoto(smallFileId: String, bigFileId: String) + case class ChatPhoto(small_file_id: String, big_file_id: String) case class Chat(id: Int, - chatType: String, + `type`: String, title: Option[String], - userName: Option[String], - firstName: Option[String], - lastName: Option[String], - allMembersAreAdministrators: Option[Boolean], + username: Option[String], + first_name: Option[String], + last_name: Option[String], + all_members_are_administrators: Option[Boolean], photo: Option[ChatPhoto], description: Option[String], - inviteLink: Option[String], - pinnedMessage: Option[Message], - stickerSetName: Option[String], - canSetStickerSet: Option[Boolean] + invite_link: Option[String], + pinned_message: Option[Message], + sticker_set_name: Option[String], + can_set_sticker_set: Option[Boolean] ) + + case class InputFile() + + case class Webhook(url: String, + certificate: Option[InputFile], + max_connections: Option[Int], + allowed_updates: Option[Seq[String]]) } diff --git a/src/main/scala/eu/xeppaka/bot1/VkApiTests.scala b/src/main/scala/eu/xeppaka/bot1/VkApiTests.scala index 3b635d5..70e6c3a 100644 --- a/src/main/scala/eu/xeppaka/bot1/VkApiTests.scala +++ b/src/main/scala/eu/xeppaka/bot1/VkApiTests.scala @@ -23,7 +23,7 @@ object VkApiTests { // .userAuthorizationCodeFlow(APP_ID, APP_SECRET, "https://oauth.vk.com/blank.html", USER_SECRET_CODE) // .execute() - val userActor = new UserActor(6242549, "2ab6508fbc2ff7d2a23fd07cf6b6cb79354abc6c1276447bf2bfb6bfe4bca5cc8d5ecc83dbead6efa04d4") + val userActor = new UserActor(6242549, "e7acb4be42aa11ab692b567ae272f16ec3c49d20f51851bb507126ce9ffc0f4aea013d34c0a26e285202d") val resp = vkApiClient.messages().get(userActor).execute() println(resp)