diff --git a/bot/src/main/scala/com/lbs/bot/telegram/TelegramClient.scala b/bot/src/main/scala/com/lbs/bot/telegram/TelegramClient.scala index a313677..8bf7449 100644 --- a/bot/src/main/scala/com/lbs/bot/telegram/TelegramClient.scala +++ b/bot/src/main/scala/com/lbs/bot/telegram/TelegramClient.scala @@ -1,18 +1,59 @@ package com.lbs.bot.telegram +import akka.http.scaladsl.Http +import akka.http.scaladsl.marshalling.Marshal +import akka.http.scaladsl.model._ +import akka.http.scaladsl.unmarshalling.Unmarshal import com.bot4s.telegram.api.declarative.{Callbacks, Commands} -import com.bot4s.telegram.api.{AkkaTelegramBot, Polling} +import com.bot4s.telegram.api.{AkkaTelegramBot, Polling, RequestHandler} import com.bot4s.telegram.clients.AkkaHttpClient +import com.bot4s.telegram.marshalling._ import com.bot4s.telegram.methods._ import com.bot4s.telegram.models.{InlineKeyboardMarkup, InputFile, Message} import com.lbs.common.Logger +import io.circe.{Decoder, Encoder, Json} import scala.concurrent.Future class TelegramClient(onReceive: TelegramEvent => Unit, botToken: String) extends AkkaTelegramBot with Polling with Commands with Callbacks with Logger { - val client = new AkkaHttpClient(botToken) + val client: RequestHandler = new AkkaHttpClient(botToken) { + + import AkkaHttpMarshalling._ + + private val http = Http() + private val apiBaseUrl = s"https://api.telegram.org/bot$botToken/" + + override def sendRequest[R, T <: Request[_]](request: T)(implicit encT: Encoder[T], decR: Decoder[R]): Future[R] = { + Marshal(request).to[RequestEntity] + .map(re => HttpRequest(HttpMethods.POST, Uri(apiBaseUrl + request.methodName), entity = re)) + .flatMap(http.singleRequest(_)) + .flatMap(r => { + request match { + case _: GetUpdates => + Unmarshal(r.entity).to[Json].flatMap { json => + val patchedJson = json.mapObject { jsonObject => + jsonObject.mapValues { value => + if (value.isArray) { + value.mapArray { update => + update.filterNot(_.findAllByKey("myChatMember").nonEmpty) + } + } else { + value + } + } + } + + Unmarshal(HttpEntity(ContentTypes.`application/json`, patchedJson.noSpaces)).to[Response[R]] + } + case _ => + Unmarshal(r.entity).to[Response[R]] + } + }) + .map(processApiResponse[R]) + } + } def sendMessage(chatId: Long, text: String): Future[Message] = loggingRequest(SendMessage(chatId, text, parseMode = Some(ParseMode.HTML)))