Skip to content

Commit

Permalink
Merge #304: v1.27.2
Browse files Browse the repository at this point in the history
Fix: Request URI construction & local bot issue
  • Loading branch information
HeySreelal authored Nov 15, 2024
2 parents 91b8dac + 0d5efa7 commit 7bd6846
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 68 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# 1.27.2

- Fix: Base URL wasn't even being considered.
- Thanks to [@itsmhmd for reporting](https://t.me/televersedart/1668)
- Updated Request URI construction logic
- ⚠️ Breaking! Removed `APIScheme`. Now the scheme must be added to the `baseURL` itself.

# 1.27.1

- Fix `RawAPI.sendPaidMedia` crashes while sending local file using `InputFile.fromFile`
Expand Down
40 changes: 9 additions & 31 deletions lib/src/televerse/api/raw_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,8 @@ class RawAPI {
/// When the timeout is reached, the API request will be cancelled and the client will throw an exception.
final Duration? timeout;

/// API Scheme
final APIScheme _scheme;

/// Default base URL for the Telegram API.
static const String defaultBase = "api.telegram.org";

/// Status of the RawAPI, true if it is local, false otherwise.
final bool _isLocal;
static const String defaultBase = "https://api.telegram.org";

/// The Bot Token.
final String token;
Expand Down Expand Up @@ -51,12 +45,9 @@ class RawAPI {
RawAPI._(
this.token, {
String? baseUrl,
APIScheme? scheme,
LoggerOptions? loggerOptions,
this.timeout,
}) : _baseUrl = baseUrl ?? defaultBase,
_isLocal = baseUrl != defaultBase,
_scheme = scheme ?? APIScheme.https,
_httpClient = _initializeHttpClient(
loggerOptions: loggerOptions,
timeout: timeout,
Expand Down Expand Up @@ -89,14 +80,12 @@ class RawAPI {
factory RawAPI.local(
String token, {
String baseUrl = "localhost:8081",
APIScheme scheme = APIScheme.http,
LoggerOptions? loggerOptions,
Duration? timeout,
}) {
return RawAPI._(
token,
baseUrl: baseUrl,
scheme: scheme,
loggerOptions: loggerOptions,
timeout: timeout,
);
Expand All @@ -113,32 +102,21 @@ class RawAPI {

/// Build the URI for the Telegram API.
Uri _buildUri(APIMethod method) {
// If Base URI is already set, just tweak the method part and return
if (_baseUri != null) {
return _baseUri!.replace(path: "${_baseUri?.path}/$method");
return _baseUri!.replace(path: "${_baseUri!.path}/$method");
}

// Create the base URI and set the _baseUri property
Uri uri;
if (_isLocal) {
RegExp https = RegExp(r'^(https?://)');
if (https.hasMatch(_baseUrl)) {
final authority = _baseUrl.replaceFirst(https, "");
uri = Uri.http(authority, "/bot$token");
} else {
uri = Uri.http(_baseUrl, "/bot$token");
}
if (_scheme == APIScheme.https) {
uri = uri.replace(scheme: "https");
}
} else {
uri = Uri.https(_baseUrl, "/bot$token");
// Ensure the base URL includes "https" if no scheme is provided
String baseUrl = _baseUrl;
if (!baseUrl.startsWith('http')) {
baseUrl = 'http://$baseUrl';
}

// Set the _baseUri property for future calls
// Create the base URI with the token
Uri uri = Uri.parse("$baseUrl/bot$token");
_baseUri = uri;

// Return the full URI with the method
// Return the full URI with the method appended
return uri.replace(path: "${uri.path}/$method");
}

Expand Down
13 changes: 2 additions & 11 deletions lib/src/televerse/bot/bot.dart
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,14 @@ class Bot<CTX extends Context> {
static RawAPI _constructRawAPI(
String token, {
String baseURL = RawAPI.defaultBase,
APIScheme scheme = APIScheme.https,
LoggerOptions? loggerOptions,
Duration? timeout,
}) {
final isLocal = baseURL == RawAPI.defaultBase;
final isLocal = baseURL != RawAPI.defaultBase;
if (isLocal) {
return RawAPI.local(
token,
baseUrl: baseURL,
scheme: scheme,
loggerOptions: loggerOptions,
timeout: timeout,
);
Expand Down Expand Up @@ -145,20 +143,17 @@ class Bot<CTX extends Context> {
/// You can use the [Bot] class to create a bot instance that listens to a local Bot API server. Use the [Bot.local] constructor to create a bot instance that listens to a local Bot API server.
///
/// You can pass the [baseURL] parameter to the constructor to specify the base URL of the Telegram Bot API. By default, the base URL is `api.telegram.org`. This parameter will be used to create the [RawAPI] instance that points to your local Bot API server.
/// If you're running the Bot API server locally, you should pass the [baseURL] and the [scheme] parameters to the constructor.
Bot(
this.token, {
Fetcher<CTX>? fetcher,
String baseURL = RawAPI.defaultBase,
APIScheme scheme = APIScheme.https,
LoggerOptions? loggerOptions,
this.timeout,
}) : isLocal = baseURL != RawAPI.defaultBase,
fetcher = fetcher ?? LongPolling<CTX>(),
_api = _constructRawAPI(
token,
baseURL: _cookBaseUrlString(baseURL),
scheme: scheme,
loggerOptions: loggerOptions,
timeout: timeout,
) {
Expand Down Expand Up @@ -187,8 +182,6 @@ class Bot<CTX extends Context> {
///
/// [fetcher] - The fetcher - used to fetch updates from the Telegram servers. By default, the bot uses long polling to fetch updates. You can also use webhooks to fetch updates.
///
/// [scheme] - The scheme of the Telegram Bot API. By default, the scheme is `APIScheme.http`.
///
/// ## Note
/// The Bot API server source code is available at [telegram-bot-api](https://github.com/tdlib/telegram-bot-api). You can run it locally and send the requests to your own server instead of ```https://api.telegram.org```.
///
Expand All @@ -206,8 +199,7 @@ class Bot<CTX extends Context> {
factory Bot.local(
String token, {
Fetcher<CTX>? fetcher,
String baseURL = "localhost:8081",
APIScheme scheme = APIScheme.http,
String baseURL = "http://localhost:8081",
LoggerOptions? loggerOptions,
Duration? timeout,
}) {
Expand All @@ -216,7 +208,6 @@ class Bot<CTX extends Context> {
token,
fetcher: fetcher,
baseURL: baseURL,
scheme: scheme,
loggerOptions: loggerOptions,
timeout: timeout,
);
Expand Down
24 changes: 0 additions & 24 deletions lib/src/types/scheme.dart

This file was deleted.

1 change: 0 additions & 1 deletion lib/src/types/types.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ part 'message_entity_type.dart';
part 'parse_mode.dart';
part 'chat_action.dart';
part 'sticker_format.dart';
part 'scheme.dart';
part 'methods.dart';
part 'passport_type.dart';
part 'reaction_type_emoji_type.dart';
Expand Down
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.11!
version: 1.27.1
version: 1.27.2
homepage: https://televerse.xooniverse.com
repository: https://github.com/xooniverse/televerse
topics:
Expand Down

0 comments on commit 7bd6846

Please sign in to comment.