diff --git a/CHANGELOG.md b/CHANGELOG.md index eab6a364..58c3b161 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +# 1.26.0 + +- [⚠️ Breaking | Plugins] Updated the `Transformer` class +- Made the `payload` parameter for the `Transformer.transform` method optional-positional argument. +- Added `RawAPI.call` method for independently calling the Telegram Bot API methods. + # 1.25.0 - 🤖 Bot API 7.10 diff --git a/lib/src/televerse/api/raw_api.dart b/lib/src/televerse/api/raw_api.dart index 2da45955..39e25b4f 100644 --- a/lib/src/televerse/api/raw_api.dart +++ b/lib/src/televerse/api/raw_api.dart @@ -259,11 +259,49 @@ class RawAPI { /// multiple transformers into a single API caller function, which processes /// the payload through each transformer in sequence before making the API call. APICaller _combineTransformer(APICaller prev, Transformer transformer) { - return (APIMethod method, Payload payload) async { + return (method, [payload]) async { return await transformer.transform(prev, method, payload); }; } + /// Independent API Caller. + /// + /// This method allows you to directly call any Telegram Bot API method without the interference of Transformers. + /// It's ideal for situations where you need full control over the request or want to bypass automatic transformations. + /// + /// The data to be sent must be a JSON-serializable `Map`, wrapped within a `Payload` class. + /// Ensure that all fields of the JSON strictly adhere to the Telegram Bot API documentation for correct execution. + /// + /// ### Example: + /// You can use this method to invoke the `sendMessage` API method like this: + /// + /// ```dart + /// void main() async { + /// final api = RawAPI(Platform.environment["BOT_TOKEN"]!); + /// + /// final data = { + /// "chat_id": 12345, + /// "text": "Hello World!", + /// }; + /// + /// await api(APIMethod.sendMessage, Payload(data)); + /// } + /// ``` + /// + /// This gives you the flexibility to directly interact with Telegram's API while maintaining a simple and clean implementation. + Future> call( + APIMethod method, [ + Payload? payload, + ]) async { + final uri = _buildUri(method); + payload?.params.removeWhere(_nullFilter); + final result = await _httpClient._makeApiCall>( + uri, + payload: payload, + ); + return result; + } + /// Executes an API call, applying any attached transformers. /// /// The `_makeApiCall` method is the central point for executing API calls. It @@ -300,27 +338,16 @@ class RawAPI { APIMethod method, { Payload? payload, }) async { - payload ??= Payload(); - - APICaller call = (APIMethod method, Payload payload) async { - final uri = _buildUri(method); - payload.params.removeWhere(_nullFilter); - final result = await _httpClient._makeApiCall( - uri, - payload: payload, - ); - return result; - }; - + APICaller caller = call; final transformers = [..._transformers, ...(_context?._transformers ?? [])]; // Combine transformers for (final transformer in transformers.reversed) { - call = _combineTransformer(call, transformer); + caller = _combineTransformer(caller, transformer); } // Execute the combined call - final result = await call(method, payload); + final result = await caller(method, payload); return result["result"] as T; } diff --git a/lib/src/televerse/middlewares/transformer.dart b/lib/src/televerse/middlewares/transformer.dart index d66cd07c..e39080b6 100644 --- a/lib/src/televerse/middlewares/transformer.dart +++ b/lib/src/televerse/middlewares/transformer.dart @@ -82,9 +82,9 @@ abstract interface class Transformer implements MiddlewareBase { /// actual API method with the modified payload. Future> transform( APICaller call, - APIMethod method, - Payload payload, - ); + APIMethod method, [ + Payload? payload, + ]); /// Constructs a `Transformer` instance. const Transformer(); diff --git a/lib/src/televerse/middlewares/types.dart b/lib/src/televerse/middlewares/types.dart index e88b8665..d207c64e 100644 --- a/lib/src/televerse/middlewares/types.dart +++ b/lib/src/televerse/middlewares/types.dart @@ -26,6 +26,6 @@ typedef NextFunction = FutureOr Function(); /// - `Future>`: A future that completes with the result /// of the API call. typedef APICaller = Future> Function( - APIMethod method, - Payload payload, -); + APIMethod method, [ + Payload? payload, +]); diff --git a/pubspec.yaml b/pubspec.yaml index 469d4291..ec9423a2 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: televerse description: Televerse lets you create your own efficient Telegram bots with ease in Dart. Supports latest Telegram Bot API - 7.10! -version: 1.25.0 +version: 1.26.0 homepage: https://televerse.xooniverse.com repository: https://github.com/xooniverse/televerse topics: