From f2cdb7b3465748be301c819bfbe5f6876bacef45 Mon Sep 17 00:00:00 2001 From: Eugene Zadyra Date: Wed, 25 Jul 2018 17:30:45 +0200 Subject: [PATCH] Added ability to send files. Useful for future send calendar event implementation --- bot/src/main/scala/com/lbs/bot/Bot.scala | 3 ++ bot/src/main/scala/com/lbs/bot/PollBot.scala | 46 ++++++++--------- .../com/lbs/bot/telegram/TelegramBot.scala | 47 +++++++++--------- .../com/lbs/bot/telegram/TelegramClient.scala | 49 ++++++++++--------- 4 files changed, 78 insertions(+), 67 deletions(-) diff --git a/bot/src/main/scala/com/lbs/bot/Bot.scala b/bot/src/main/scala/com/lbs/bot/Bot.scala index c362a32..0cb5600 100644 --- a/bot/src/main/scala/com/lbs/bot/Bot.scala +++ b/bot/src/main/scala/com/lbs/bot/Bot.scala @@ -40,6 +40,9 @@ class Bot(telegram: TelegramBot /* other bots */) extends Logger { def sendEditMessage(source: MessageSource, messageId: String, text: String, inlineKeyboard: Option[InlineKeyboard] = None): Unit = resolveAdapter(source).sendEditMessage(source.chatId, messageId, text, inlineKeyboard) + def sendFile(source: MessageSource, filename: String, contents: Array[Byte], caption: Option[String] = None): Unit = + resolveAdapter(source).sendFile(source.chatId, filename, contents, caption) + private def resolveAdapter(source: MessageSource): PollBot[_] = source.sourceSystem match { case TelegramMessageSourceSystem => telegram diff --git a/bot/src/main/scala/com/lbs/bot/PollBot.scala b/bot/src/main/scala/com/lbs/bot/PollBot.scala index 749e1ff..3edf9f2 100644 --- a/bot/src/main/scala/com/lbs/bot/PollBot.scala +++ b/bot/src/main/scala/com/lbs/bot/PollBot.scala @@ -1,26 +1,26 @@ /** - * MIT License - * - * Copyright (c) 2018 Yevhen Zadyra - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ + * MIT License + * + * Copyright (c) 2018 Yevhen Zadyra + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ package com.lbs.bot import com.lbs.bot.model.{Event, InlineKeyboard} @@ -34,5 +34,7 @@ trait PollBot[In <: Event] { def sendEditMessage(chatId: String, messageId: String, text: String, buttons: Option[InlineKeyboard] = None): Unit + def sendFile(chatId: String, filename: String, contents: Array[Byte], caption: Option[String] = None): Unit + protected def onReceive(command: In): Unit } diff --git a/bot/src/main/scala/com/lbs/bot/telegram/TelegramBot.scala b/bot/src/main/scala/com/lbs/bot/telegram/TelegramBot.scala index 9fdb73a..b271194 100644 --- a/bot/src/main/scala/com/lbs/bot/telegram/TelegramBot.scala +++ b/bot/src/main/scala/com/lbs/bot/telegram/TelegramBot.scala @@ -1,26 +1,26 @@ /** - * MIT License - * - * Copyright (c) 2018 Yevhen Zadyra - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ + * MIT License + * + * Copyright (c) 2018 Yevhen Zadyra + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ package com.lbs.bot.telegram import com.lbs.bot.PollBot @@ -45,6 +45,9 @@ class TelegramBot(onCommand: Command => Unit, botToken: String) extends PollBot[ def sendEditMessage(chatId: String, messageId: String, text: String, buttons: Option[InlineKeyboard] = None): Unit = telegramBot.sendEditMessage(chatId.toLong, messageId.toInt, text, replyMarkup = buttons.map(_.mapTo[InlineKeyboardMarkup])) + def sendFile(chatId: String, filename: String, contents: Array[Byte], caption: Option[String] = None): Unit = + telegramBot.sendFile(chatId.toLong, filename, contents, caption) + override protected def onReceive(command: TelegramEvent): Unit = { onCommand(command.mapTo[Command]) } 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 5bdb3d5..883d5bf 100644 --- a/bot/src/main/scala/com/lbs/bot/telegram/TelegramClient.scala +++ b/bot/src/main/scala/com/lbs/bot/telegram/TelegramClient.scala @@ -1,32 +1,32 @@ /** - * MIT License - * - * Copyright (c) 2018 Yevhen Zadyra - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ + * MIT License + * + * Copyright (c) 2018 Yevhen Zadyra + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ package com.lbs.bot.telegram import com.lbs.common.Logger import info.mukel.telegrambot4s.api.declarative.{Callbacks, Commands} import info.mukel.telegrambot4s.api.{Polling, TelegramBot => TelegramBotBase} -import info.mukel.telegrambot4s.methods.{EditMessageReplyMarkup, EditMessageText, ParseMode, SendMessage} +import info.mukel.telegrambot4s.methods._ import info.mukel.telegrambot4s.models._ import scala.concurrent.Future @@ -47,6 +47,9 @@ class TelegramClient(onReceive: TelegramEvent => Unit, botToken: String) extends def sendEditMessage(chatId: Long, messageId: Int, text: String, replyMarkup: Option[InlineKeyboardMarkup] = None): Future[Either[Boolean, Message]] = request(EditMessageText(Some(chatId), Some(messageId), text = text, parseMode = Some(ParseMode.HTML), replyMarkup = replyMarkup)) + def sendFile(chatId: Long, filename: String, contents: Array[Byte], caption: Option[String] = None): Future[Message] = + request(SendDocument(chatId, InputFile(filename, contents), caption)) + override def receiveMessage(msg: Message): Unit = { debug(s"Received telegram message: $msg")