Skip to content

Commit

Permalink
Merge #293: Televerse v1.26.0
Browse files Browse the repository at this point in the history
  • Loading branch information
HeySreelal committed Sep 8, 2024
2 parents 7a8d987 + aa941ca commit b88ce46
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 22 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
57 changes: 42 additions & 15 deletions lib/src/televerse/api/raw_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<Map<String, dynamic>> call(
APIMethod method, [
Payload? payload,
]) async {
final uri = _buildUri(method);
payload?.params.removeWhere(_nullFilter);
final result = await _httpClient._makeApiCall<Map<String, dynamic>>(
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
Expand Down Expand Up @@ -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;
}
Expand Down
6 changes: 3 additions & 3 deletions lib/src/televerse/middlewares/transformer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ abstract interface class Transformer implements MiddlewareBase {
/// actual API method with the modified payload.
Future<Map<String, dynamic>> transform(
APICaller call,
APIMethod method,
Payload payload,
);
APIMethod method, [
Payload? payload,
]);

/// Constructs a `Transformer` instance.
const Transformer();
Expand Down
6 changes: 3 additions & 3 deletions lib/src/televerse/middlewares/types.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ typedef NextFunction = FutureOr<void> Function();
/// - `Future<Map<String, dynamic>>`: A future that completes with the result
/// of the API call.
typedef APICaller = Future<Map<String, dynamic>> Function(
APIMethod method,
Payload payload,
);
APIMethod method, [
Payload? payload,
]);
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down

0 comments on commit b88ce46

Please sign in to comment.