Added ability to send files. Useful for future send calendar event implementation

This commit is contained in:
Eugene Zadyra
2018-07-25 17:30:45 +02:00
parent fbc26e4c7c
commit f2cdb7b346
4 changed files with 78 additions and 67 deletions

View File

@@ -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

View File

@@ -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
}

View File

@@ -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])
}

View File

@@ -26,7 +26,7 @@ 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")