From b44a7c4c98dba0900ab794988d47ae6ac9e9aa9f Mon Sep 17 00:00:00 2001 From: Sreelal TS Date: Wed, 19 Jun 2024 01:16:16 +0530 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=91=F0=9F=8F=BB=E2=80=8D=F0=9F=92=BB?= =?UTF-8?q?=20Reworked=20on=20all=20edit=20methods?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/src/televerse/context/context.dart | 215 ++++++++++--------------- lib/src/televerse/raw_api.dart | 25 +++ lib/src/utils/utils.dart | 8 + 3 files changed, 115 insertions(+), 133 deletions(-) diff --git a/lib/src/televerse/context/context.dart b/lib/src/televerse/context/context.dart index 295d0329..0218b80e 100644 --- a/lib/src/televerse/context/context.dart +++ b/lib/src/televerse/context/context.dart @@ -208,17 +208,6 @@ class Context { } } - /// Internal method to check whether the passed type argument is correct - void _verifyMessageOrBoolean() { - if (MessageOrBool != Message && MessageOrBool != bool) { - throw TeleverseException.typeParameterRequired( - "editMessageMedia", - MessageOrBool, - [Message, bool], - ); - } - } - /// Reply a Text Message to the user. Future reply( String text, { @@ -832,7 +821,7 @@ class Context { /// Edit message live location /// /// This method will edit the message live location in the current context. - Future editMessageLiveLocation({ + Future editMessageLiveLocation({ String? inlineMessageId, double? latitude, double? longitude, @@ -842,32 +831,23 @@ class Context { InlineKeyboardMarkup? replyMarkup, String? businessConnectionId, }) async { - _verifyMessageOrBoolean(); - if (_isInline()) { - return api.editInlineMessageLiveLocation( - _inlineMsgId!, - latitude: latitude, - longitude: longitude, - horizontalAccuracy: horizontalAccuracy, - heading: heading, - proximityAlertRadius: proximityAlertRadius, - replyMarkup: replyMarkup, - businessConnectionId: businessConnectionId, - ) as MessageOrBool; - } else { + if (!_isInline()) { _verifyInfo([_chatId, _msgId], APIMethod.editMessageLiveLocation); - return api.editMessageLiveLocation( - id, - _msgId!, - latitude: latitude, - longitude: longitude, - horizontalAccuracy: horizontalAccuracy, - heading: heading, - proximityAlertRadius: proximityAlertRadius, - replyMarkup: replyMarkup, - businessConnectionId: businessConnectionId, - ) as MessageOrBool; } + + await api._editMessageLiveLocation<_Ignore>( + chatId: id, + messageId: _msgId, + inlineMessageId: _inlineMsgId, + latitude: latitude, + longitude: longitude, + horizontalAccuracy: horizontalAccuracy, + heading: heading, + proximityAlertRadius: proximityAlertRadius, + replyMarkup: replyMarkup, + businessConnectionId: businessConnectionId, + ); + return true; } /// Forward the message. @@ -1241,27 +1221,23 @@ class Context { /// Context aware method for stopping live location of message: [APIMethod.stopMessageLiveLocation]. /// /// Use this method to stop updating a live location message before live_period expires. On success, if the message is not an inline message, the edited Message is returned, otherwise True is returned. - Future stopMessageLiveLocation({ + Future stopMessageLiveLocation({ InlineKeyboardMarkup? replyMarkup, String? businessConnectionId, }) async { - _verifyMessageOrBoolean(); - - if (_isInline()) { - return api.stopInlineMessageLiveLocation( - _inlineMsgId!, - replyMarkup: replyMarkup, - businessConnectionId: businessConnectionId, - ) as MessageOrBool; + if (!_isInline()) { + _verifyInfo([_chatId, _msgId], APIMethod.stopMessageLiveLocation); } - _verifyInfo([_chatId, _msgId], APIMethod.stopMessageLiveLocation); - return api.stopMessageLiveLocation( - id, - _msgId!, + await api._stopMessageLiveLocation<_Ignore>( + chatId: id, + messageId: _msgId, + inlineMessageId: _inlineMsgId, replyMarkup: replyMarkup, businessConnectionId: businessConnectionId, - ) as MessageOrBool; + ); + + return true; } /// Context aware method for send "Typing..." action: [APIMethod.sendChatAction]. @@ -1707,7 +1683,7 @@ class Context { return _inlineMsgId != null; } - /// Edit the message text + /// Edit the message text. Returns True on success. Future editMessageText( String text, { ParseMode? parseMode, @@ -1716,33 +1692,26 @@ class Context { LinkPreviewOptions? linkPreviewOptions, String? businessConnectionId, }) async { - if (_isInline()) { - await api.editInlineMessageText( - _inlineMsgId!, - text, - parseMode: parseMode, - entities: entities, - replyMarkup: replyMarkup, - linkPreviewOptions: linkPreviewOptions, - businessConnectionId: businessConnectionId, - ); - } else { + if (!_isInline()) { _verifyInfo([_chatId, _msgId], APIMethod.editMessageText); - await api.editMessageText( - id, - _msgId!, - text, - parseMode: parseMode, - entities: entities, - replyMarkup: replyMarkup, - linkPreviewOptions: linkPreviewOptions, - businessConnectionId: businessConnectionId, - ); } + + await api._editMessageText<_Ignore>( + text: text, + chatId: id, + inlineMessageId: _inlineMsgId, + messageId: _msgId, + parseMode: parseMode, + entities: entities, + replyMarkup: replyMarkup, + linkPreviewOptions: linkPreviewOptions, + businessConnectionId: businessConnectionId, + ); + return true; } - /// Edit the message caption + /// Edit the message caption. Returns `true` on success. Future editMessageCaption({ String? caption, ParseMode? parseMode, @@ -1751,89 +1720,69 @@ class Context { bool? showCaptionAboveMedia, String? businessConnectionId, }) async { - if (_isInline()) { - await api.editInlineMessageCaption( - _inlineMsgId!, - caption: caption, - parseMode: parseMode, - captionEntities: captionEntities, - replyMarkup: replyMarkup, - showCaptionAboveMedia: showCaptionAboveMedia, - businessConnectionId: businessConnectionId, - ); - } else { + if (!_isInline()) { _verifyInfo([_chatId, _msgId], APIMethod.editMessageCaption); - await api.editMessageCaption( - id, - _msgId!, - caption: caption, - parseMode: parseMode, - captionEntities: captionEntities, - replyMarkup: replyMarkup, - showCaptionAboveMedia: showCaptionAboveMedia, - businessConnectionId: businessConnectionId, - ); } + + await api._editMessageCaption<_Ignore>( + chatId: id, + messageId: _msgId, + inlineMessageId: _inlineMsgId, + caption: caption, + parseMode: parseMode, + captionEntities: captionEntities, + replyMarkup: replyMarkup, + showCaptionAboveMedia: showCaptionAboveMedia, + businessConnectionId: businessConnectionId, + ); + return true; } /// Edit the message media /// Use this method to edit animation, audio, document, photo, or video messages. /// - /// Make sure to pass the type parameter for correct typing. If the message to be edited is an - /// inline message, the type parameter should be `bool`, otherwise `Message` should be passed. - /// - /// Since Televerse don't deal with `dynamic` types on the public interface, if you do not pass - /// type parameter, this method will throw a `TeleverseException`. - Future editMessageMedia( + /// This method ignores the result and always returns True on success. + Future editMessageMedia( InputMedia media, { InlineKeyboardMarkup? replyMarkup, String? businessConnectionId, }) async { - _verifyMessageOrBoolean(); - - if (_isInline()) { - return await api.editInlineMessageMedia( - _inlineMsgId!, - media, - replyMarkup: replyMarkup, - businessConnectionId: businessConnectionId, - ) as MessageOrBool; - } else { + if (!_isInline()) { _verifyInfo([_chatId, _msgId], APIMethod.editMessageMedia); - return await api.editMessageMedia( - id, - _msgId!, - media, - replyMarkup: replyMarkup, - businessConnectionId: businessConnectionId, - ) as MessageOrBool; } + + await api._editMessageMedia<_Ignore>( + inlineMessageId: _inlineMsgId, + chatId: id, + messageId: _msgId, + media: media, + replyMarkup: replyMarkup, + businessConnectionId: businessConnectionId, + ); + + return true; } /// Edit the message reply markup /// Use this method to edit only the reply markup of messages. - Future editMessageReplyMarkup({ + Future editMessageReplyMarkup({ InlineKeyboardMarkup? replyMarkup, String? businessConnectionId, }) async { - _verifyMessageOrBoolean(); - - if (_isInline()) { - return await api.editInlineMessageReplyMarkup( - _inlineMsgId!, - replyMarkup: replyMarkup, - businessConnectionId: businessConnectionId, - ) as MessageOrBool; - } else { + if (!_isInline()) { _verifyInfo([_chatId, _msgId], APIMethod.editMessageReplyMarkup); - return await api.editMessageReplyMarkup( - id, - _msgId!, - replyMarkup: replyMarkup, - businessConnectionId: businessConnectionId, - ) as MessageOrBool; } + + await api._editMessageReplyMarkup<_Ignore>( + chatId: id, + messageId: _msgId, + inlineMessageId: _inlineMsgId, + replyMarkup: replyMarkup, + businessConnectionId: businessConnectionId, + ); + + return true; } /// Answer inline query diff --git a/lib/src/televerse/raw_api.dart b/lib/src/televerse/raw_api.dart index 1ac9953a..fd85d646 100644 --- a/lib/src/televerse/raw_api.dart +++ b/lib/src/televerse/raw_api.dart @@ -933,6 +933,10 @@ class RawAPI { params, ); + if (MessageOrBool == _Ignore) { + return _ignore as MessageOrBool; + } + if (MessageOrBool == Message) { return Message.fromJson(response) as MessageOrBool; } @@ -1022,6 +1026,10 @@ class RawAPI { params, ); + if (MessageOrBool == _Ignore) { + return _ignore as MessageOrBool; + } + if (MessageOrBool == Message) { return Message.fromJson(response) as MessageOrBool; } @@ -2320,6 +2328,10 @@ class RawAPI { params, ); + if (MessageOrBool == _Ignore) { + return _ignore as MessageOrBool; + } + if (MessageOrBool == Message) { return Message.fromJson(response) as MessageOrBool; } @@ -2343,6 +2355,7 @@ class RawAPI { String? businessConnectionId, }) async { return await _editMessageText( + chatId: chatId, text: text, messageId: messageId, parseMode: parseMode, @@ -2409,6 +2422,10 @@ class RawAPI { params, ); + if (MessageOrBool == _Ignore) { + return _ignore as MessageOrBool; + } + if (MessageOrBool == Message) { return Message.fromJson(response) as MessageOrBool; } @@ -2512,6 +2529,10 @@ class RawAPI { ); } + if (MessageOrBool == _Ignore) { + return _ignore as MessageOrBool; + } + if (MessageOrBool == Message) { return Message.fromJson(response) as MessageOrBool; } @@ -2583,6 +2604,10 @@ class RawAPI { params, ); + if (MessageOrBool == _Ignore) { + return _ignore as MessageOrBool; + } + if (MessageOrBool == Message) { return Message.fromJson(response) as MessageOrBool; } diff --git a/lib/src/utils/utils.dart b/lib/src/utils/utils.dart index 8f0966a5..7212ce54 100644 --- a/lib/src/utils/utils.dart +++ b/lib/src/utils/utils.dart @@ -168,3 +168,11 @@ extension GetUserChatID on User { return false; } } + +/// (Internal) Ignore value +class _Ignore { + const _Ignore(); +} + +/// Ignores the value +const _ignore = _Ignore();