From 6cf836708d11ce4dea638d198ee096ee6666a285 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 6 Feb 2020 14:25:17 +0600 Subject: [PATCH] complete filling of until SendPhoto for usefull extensions --- .../TelegramBotAPI/requests/ForwardMessage.kt | 46 +++++++++- .../TelegramBotAPI/requests/GetMe.kt | 5 +- .../requests/send/SendMessage.kt | 47 ++++++++++ .../requests/send/media/SendPhoto.kt | 89 +++++++++++++++++++ .../types/message/ChatEvents/NewChatPhoto.kt | 3 +- .../message/content/media/PhotoContent.kt | 5 +- 6 files changed, 189 insertions(+), 6 deletions(-) diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/ForwardMessage.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/ForwardMessage.kt index 21c92f71cd..a5e5caee51 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/ForwardMessage.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/ForwardMessage.kt @@ -1,9 +1,11 @@ package com.github.insanusmokrassar.TelegramBotAPI.requests import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.types.MessageAction +import com.github.insanusmokrassar.TelegramBotAPI.bot.RequestsExecutor import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.SimpleRequest import com.github.insanusmokrassar.TelegramBotAPI.types.* -import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.PossiblyForwardedMessage +import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.Chat +import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.* import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.TelegramBotAPIMessageDeserializationStrategyClass import kotlinx.serialization.* @@ -30,3 +32,45 @@ data class ForwardMessage( override val requestSerializer: SerializationStrategy<*> get() = serializer() } + +suspend fun RequestsExecutor.forwardMessage( + fromChatId: ChatIdentifier, + toChatId: ChatIdentifier, + messageId: MessageIdentifier, + disableNotification: Boolean = false +) = execute( + ForwardMessage(fromChatId, toChatId, messageId, disableNotification) +) + +suspend fun RequestsExecutor.forwardMessage( + fromChat: Chat, + toChatId: ChatIdentifier, + messageId: MessageIdentifier, + disableNotification: Boolean = false +) = forwardMessage(fromChat.id, toChatId, messageId, disableNotification) + +suspend fun RequestsExecutor.forwardMessage( + fromChatId: ChatIdentifier, + toChat: Chat, + messageId: MessageIdentifier, + disableNotification: Boolean = false +) = forwardMessage(fromChatId, toChat.id, messageId, disableNotification) + +suspend fun RequestsExecutor.forwardMessage( + fromChat: Chat, + toChat: Chat, + messageId: MessageIdentifier, + disableNotification: Boolean = false +) = forwardMessage(fromChat.id, toChat.id, messageId, disableNotification) + +suspend fun RequestsExecutor.forwardMessage( + toChatId: ChatIdentifier, + message: Message, + disableNotification: Boolean = false +) = forwardMessage(message.chat, toChatId, message.messageId, disableNotification) + +suspend fun RequestsExecutor.forwardMessage( + toChat: Chat, + message: Message, + disableNotification: Boolean = false +) = forwardMessage(message.chat, toChat, message.messageId, disableNotification) diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/GetMe.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/GetMe.kt index 2f19df4909..8dd4b06eac 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/GetMe.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/GetMe.kt @@ -1,5 +1,6 @@ package com.github.insanusmokrassar.TelegramBotAPI.requests +import com.github.insanusmokrassar.TelegramBotAPI.bot.RequestsExecutor import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.SimpleRequest import com.github.insanusmokrassar.TelegramBotAPI.types.* import kotlinx.serialization.* @@ -11,4 +12,6 @@ class GetMe : SimpleRequest { get() = ExtendedBot.serializer() override val requestSerializer: SerializationStrategy<*> get() = serializer() -} \ No newline at end of file +} + +suspend fun RequestsExecutor.getMe() = execute(GetMe()) \ No newline at end of file diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/SendMessage.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/SendMessage.kt index 91ebbad8a6..8594d7400d 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/SendMessage.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/SendMessage.kt @@ -1,11 +1,13 @@ package com.github.insanusmokrassar.TelegramBotAPI.requests.send import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.types.DisableWebPagePreview +import com.github.insanusmokrassar.TelegramBotAPI.bot.RequestsExecutor import com.github.insanusmokrassar.TelegramBotAPI.requests.send.abstracts.* import com.github.insanusmokrassar.TelegramBotAPI.types.* import com.github.insanusmokrassar.TelegramBotAPI.types.ParseMode.ParseMode import com.github.insanusmokrassar.TelegramBotAPI.types.ParseMode.parseModeField import com.github.insanusmokrassar.TelegramBotAPI.types.buttons.KeyboardMarkup +import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.Chat import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.ContentMessage import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.TelegramBotAPIMessageDeserializationStrategyClass import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.TextContent @@ -55,3 +57,48 @@ data class SendTextMessage( ) ) typealias SendMessage = SendTextMessage + +suspend fun RequestsExecutor.sendMessage( + chatId: ChatIdentifier, + text: String, + parseMode: ParseMode? = null, + disableWebPagePreview: Boolean? = null, + disableNotification: Boolean = false, + replyToMessageId: MessageIdentifier? = null, + replyMarkup: KeyboardMarkup? = null +) = execute( + SendTextMessage(chatId, text, parseMode, disableWebPagePreview, disableNotification, replyToMessageId, replyMarkup) +) + +suspend fun RequestsExecutor.sendTextMessage( + chatId: ChatIdentifier, + text: String, + parseMode: ParseMode? = null, + disableWebPagePreview: Boolean? = null, + disableNotification: Boolean = false, + replyToMessageId: MessageIdentifier? = null, + replyMarkup: KeyboardMarkup? = null +) = sendMessage( + chatId, text, parseMode, disableWebPagePreview, disableNotification, replyToMessageId, replyMarkup +) + +suspend fun RequestsExecutor.sendMessage( + chat: Chat, + text: String, + parseMode: ParseMode? = null, + disableWebPagePreview: Boolean? = null, + disableNotification: Boolean = false, + replyToMessageId: MessageIdentifier? = null, + replyMarkup: KeyboardMarkup? = null +) = sendMessage(chat.id, text, parseMode, disableWebPagePreview, disableNotification, replyToMessageId, replyMarkup) + + +suspend fun RequestsExecutor.sendTextMessage( + chat: Chat, + text: String, + parseMode: ParseMode? = null, + disableWebPagePreview: Boolean? = null, + disableNotification: Boolean = false, + replyToMessageId: MessageIdentifier? = null, + replyMarkup: KeyboardMarkup? = null +) = sendTextMessage(chat.id, text, parseMode, disableWebPagePreview, disableNotification, replyToMessageId, replyMarkup) diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/SendPhoto.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/SendPhoto.kt index b9b005d8b4..8edecd4053 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/SendPhoto.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/SendPhoto.kt @@ -1,5 +1,6 @@ package com.github.insanusmokrassar.TelegramBotAPI.requests.send.media +import com.github.insanusmokrassar.TelegramBotAPI.bot.RequestsExecutor import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.* import com.github.insanusmokrassar.TelegramBotAPI.requests.send.abstracts.* import com.github.insanusmokrassar.TelegramBotAPI.requests.send.media.base.* @@ -7,6 +8,8 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.* import com.github.insanusmokrassar.TelegramBotAPI.types.ParseMode.ParseMode import com.github.insanusmokrassar.TelegramBotAPI.types.ParseMode.parseModeField import com.github.insanusmokrassar.TelegramBotAPI.types.buttons.KeyboardMarkup +import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.Chat +import com.github.insanusmokrassar.TelegramBotAPI.types.files.* import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.ContentMessage import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.TelegramBotAPIMessageDeserializationStrategyClass import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.media.PhotoContent @@ -82,3 +85,89 @@ data class SendPhotoFiles internal constructor( ) : Files by mapOf( photoField to photo ) + +suspend fun RequestsExecutor.sendPhoto( + chatId: ChatIdentifier, + fileId: FileId, + caption: String? = null, + parseMode: ParseMode? = null, + disableNotification: Boolean = false, + replyToMessageId: MessageIdentifier? = null, + replyMarkup: KeyboardMarkup? = null +) = execute( + SendPhotoData( + chatId, + fileId.fileId, + caption, + parseMode, + disableNotification, + replyToMessageId, + replyMarkup + ) +) + +suspend fun RequestsExecutor.sendPhoto( + chatId: ChatIdentifier, + file: PhotoSize, + caption: String? = null, + parseMode: ParseMode? = null, + disableNotification: Boolean = false, + replyToMessageId: MessageIdentifier? = null, + replyMarkup: KeyboardMarkup? = null +) = sendPhoto( + chatId, file.fileId, caption, parseMode, disableNotification, replyToMessageId, replyMarkup +) + +suspend fun RequestsExecutor.sendPhoto( + chatId: ChatIdentifier, + photo: Photo, + caption: String? = null, + parseMode: ParseMode? = null, + disableNotification: Boolean = false, + replyToMessageId: MessageIdentifier? = null, + replyMarkup: KeyboardMarkup? = null +) = sendPhoto( + chatId, photo.biggest() ?: throw IllegalArgumentException("Photo $photo is empty"), caption, parseMode, disableNotification, replyToMessageId, replyMarkup +) + +suspend fun RequestsExecutor.sendPhoto( + chat: Chat, + fileId: FileId, + caption: String? = null, + parseMode: ParseMode? = null, + disableNotification: Boolean = false, + replyToMessageId: MessageIdentifier? = null, + replyMarkup: KeyboardMarkup? = null +) = sendPhoto( + chat.id, + fileId, + caption, + parseMode, + disableNotification, + replyToMessageId, + replyMarkup +) + +suspend fun RequestsExecutor.sendPhoto( + chat: Chat, + file: PhotoSize, + caption: String? = null, + parseMode: ParseMode? = null, + disableNotification: Boolean = false, + replyToMessageId: MessageIdentifier? = null, + replyMarkup: KeyboardMarkup? = null +) = sendPhoto( + chat.id, file.fileId, caption, parseMode, disableNotification, replyToMessageId, replyMarkup +) + +suspend fun RequestsExecutor.sendPhoto( + chat: Chat, + photo: Photo, + caption: String? = null, + parseMode: ParseMode? = null, + disableNotification: Boolean = false, + replyToMessageId: MessageIdentifier? = null, + replyMarkup: KeyboardMarkup? = null +) = sendPhoto( + chat.id, photo.biggest() ?: throw IllegalArgumentException("Photo $photo is empty"), caption, parseMode, disableNotification, replyToMessageId, replyMarkup +) diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/ChatEvents/NewChatPhoto.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/ChatEvents/NewChatPhoto.kt index 60c3e5d56c..5396d4e69b 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/ChatEvents/NewChatPhoto.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/ChatEvents/NewChatPhoto.kt @@ -1,8 +1,9 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.message.ChatEvents +import com.github.insanusmokrassar.TelegramBotAPI.types.files.Photo import com.github.insanusmokrassar.TelegramBotAPI.types.files.PhotoSize import com.github.insanusmokrassar.TelegramBotAPI.types.message.ChatEvents.abstracts.CommonEvent data class NewChatPhoto( - val photo: List + val photo: Photo ): CommonEvent diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/content/media/PhotoContent.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/content/media/PhotoContent.kt index ade1597e0d..9363a9f8f0 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/content/media/PhotoContent.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/content/media/PhotoContent.kt @@ -10,8 +10,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.MessageIdentifier import com.github.insanusmokrassar.TelegramBotAPI.types.ParseMode.HTMLParseMode import com.github.insanusmokrassar.TelegramBotAPI.types.ParseMode.MarkdownV2 import com.github.insanusmokrassar.TelegramBotAPI.types.buttons.KeyboardMarkup -import com.github.insanusmokrassar.TelegramBotAPI.types.files.PhotoSize -import com.github.insanusmokrassar.TelegramBotAPI.types.files.biggest +import com.github.insanusmokrassar.TelegramBotAPI.types.files.* import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.ContentMessage import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.abstracts.MediaCollectionContent import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.abstracts.MediaGroupContent @@ -19,7 +18,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.toHtmlCaptions import com.github.insanusmokrassar.TelegramBotAPI.utils.toMarkdownV2Captions data class PhotoContent( - override val mediaCollection: List, + override val mediaCollection: Photo, override val caption: String? = null, override val captionEntities: List = emptyList() ) : MediaCollectionContent, MediaGroupContent {