Skip to content

Allow custom Bot API server to be used #1168

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Exclamation symbols (:exclamation:) note something of importance e.g. breaking c
### Notes
- [:ledger: View file changes][Unreleased]
### Added
- Define a custom Bot API server and file download URI.
### Changed
- Improved error messages for empty input.
- Log update when processing it, not when fetching input.
Expand Down
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ A Telegram Bot based on the official [Telegram Bot API]
- [Create your first bot](#create-your-first-bot)
- [Require this package with Composer](#require-this-package-with-composer)
- [Choose how to retrieve Telegram updates](#choose-how-to-retrieve-telegram-updates)
- [Using a custom Bot API server](#using-a-custom-bot-api-server)
- [Webhook installation](#webhook-installation)
- [Self Signed Certificate](#self-signed-certificate)
- [Unset Webhook](#unset-webhook)
Expand Down Expand Up @@ -200,6 +201,23 @@ The bot can handle updates with [**Webhook**](#webhook-installation) or [**getUp
| Host with https | Required | Not required |
| MySQL | Not required | ([Not](#getupdates-without-database)) Required |

## Using a custom Bot API server

**For advanced users only!**

As from Telegram Bot API 5.0, users can [run their own Bot API server] to handle updates.
This means, that the PHP Telegram Bot needs to be configured to serve that custom URI.
Additionally, you can define the URI where uploaded files to the bot can be downloaded (note the `{API_KEY}` placeholder).

```php
Longman\TelegramBot\Request::setCustomBotApiUri(
$api_base_uri = 'https://your-bot-api-server', // Default: https://api.telegram.org
$api_base_download_uri = '/path/to/files/{API_KEY}' // Default: /file/bot{API_KEY}
);
```

**Note:** If you are running your bot in `--local` mode, you won't need the `Request::downloadFile()` method, since you can then access your files directly from the absolute path returned by `Request::getFile()`.

## Webhook installation

Note: For a more detailed explanation, head over to the [example-bot repository] and follow the instructions there.
Expand Down Expand Up @@ -657,6 +675,7 @@ Credit list in [CREDITS](CREDITS)

[Telegram Bot API]: https://core.telegram.org/bots/api "Telegram Bot API"
[Composer]: https://getcomposer.org/ "Composer"
[run their own Bot API server]: https://core.telegram.org/bots/api#using-a-local-bot-api-server "Using a Local Bot API Server"
[example-bot repository]: https://github.com/php-telegram-bot/example-bot "Example Bot repository"
[api-setwebhook]: https://core.telegram.org/bots/api#setwebhook "Webhook on Telegram Bot API"
[set.php]: https://github.com/php-telegram-bot/example-bot/blob/master/set.php "example set.php"
Expand Down
24 changes: 23 additions & 1 deletion src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,13 @@ class Request
*/
private static $api_base_uri = 'https://api.telegram.org';

/**
* URI of the Telegram API for downloading files (relative to $api_base_url or absolute)
*
* @var string
*/
private static $api_base_download_uri = '/file/bot{API_KEY}';

/**
* Guzzle Client object
*
Expand Down Expand Up @@ -292,6 +299,20 @@ public static function setClient(ClientInterface $client): void
self::$client = $client;
}

/**
* Set a custom Bot API URL
*
* @param string $api_base_uri
* @param string $api_base_download_uri
*/
public static function setCustomBotApiUri(string $api_base_uri, string $api_base_download_uri = ''): void
{
self::$api_base_uri = $api_base_uri;
if ($api_base_download_uri !== '') {
self::$api_base_download_uri = $api_base_download_uri;
}
}

/**
* Get input from custom input or stdin and return it
*
Expand Down Expand Up @@ -532,8 +553,9 @@ public static function downloadFile(File $file): bool
$debug_handle = TelegramLog::getDebugLogTempStream();

try {
$base_download_uri = str_replace('{API_KEY}', self::$telegram->getApiKey(), self::$api_base_download_uri);
self::$client->get(
'/file/bot' . self::$telegram->getApiKey() . '/' . $tg_file_path,
"{$base_download_uri}/{$tg_file_path}",
['debug' => $debug_handle, 'sink' => $file_path]
);

Expand Down