Skip to content

Commit 7981421

Browse files
authored
Merge pull request #1168 from noplanman/1161-custom-bot-api-server
Allow custom Bot API server to be used
2 parents 0197933 + b3ad4e6 commit 7981421

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Exclamation symbols (:exclamation:) note something of importance e.g. breaking c
77
### Notes
88
- [:ledger: View file changes][Unreleased]
99
### Added
10+
- Define a custom Bot API server and file download URI.
1011
### Changed
1112
- Improved error messages for empty input.
1213
- Log update when processing it, not when fetching input.

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ A Telegram Bot based on the official [Telegram Bot API]
2727
- [Create your first bot](#create-your-first-bot)
2828
- [Require this package with Composer](#require-this-package-with-composer)
2929
- [Choose how to retrieve Telegram updates](#choose-how-to-retrieve-telegram-updates)
30+
- [Using a custom Bot API server](#using-a-custom-bot-api-server)
3031
- [Webhook installation](#webhook-installation)
3132
- [Self Signed Certificate](#self-signed-certificate)
3233
- [Unset Webhook](#unset-webhook)
@@ -200,6 +201,23 @@ The bot can handle updates with [**Webhook**](#webhook-installation) or [**getUp
200201
| Host with https | Required | Not required |
201202
| MySQL | Not required | ([Not](#getupdates-without-database)) Required |
202203

204+
## Using a custom Bot API server
205+
206+
**For advanced users only!**
207+
208+
As from Telegram Bot API 5.0, users can [run their own Bot API server] to handle updates.
209+
This means, that the PHP Telegram Bot needs to be configured to serve that custom URI.
210+
Additionally, you can define the URI where uploaded files to the bot can be downloaded (note the `{API_KEY}` placeholder).
211+
212+
```php
213+
Longman\TelegramBot\Request::setCustomBotApiUri(
214+
$api_base_uri = 'https://your-bot-api-server', // Default: https://api.telegram.org
215+
$api_base_download_uri = '/path/to/files/{API_KEY}' // Default: /file/bot{API_KEY}
216+
);
217+
```
218+
219+
**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()`.
220+
203221
## Webhook installation
204222

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

658676
[Telegram Bot API]: https://core.telegram.org/bots/api "Telegram Bot API"
659677
[Composer]: https://getcomposer.org/ "Composer"
678+
[run their own Bot API server]: https://core.telegram.org/bots/api#using-a-local-bot-api-server "Using a Local Bot API Server"
660679
[example-bot repository]: https://github.com/php-telegram-bot/example-bot "Example Bot repository"
661680
[api-setwebhook]: https://core.telegram.org/bots/api#setwebhook "Webhook on Telegram Bot API"
662681
[set.php]: https://github.com/php-telegram-bot/example-bot/blob/master/set.php "example set.php"

src/Request.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,13 @@ class Request
116116
*/
117117
private static $api_base_uri = 'https://api.telegram.org';
118118

119+
/**
120+
* URI of the Telegram API for downloading files (relative to $api_base_url or absolute)
121+
*
122+
* @var string
123+
*/
124+
private static $api_base_download_uri = '/file/bot{API_KEY}';
125+
119126
/**
120127
* Guzzle Client object
121128
*
@@ -292,6 +299,20 @@ public static function setClient(ClientInterface $client): void
292299
self::$client = $client;
293300
}
294301

302+
/**
303+
* Set a custom Bot API URL
304+
*
305+
* @param string $api_base_uri
306+
* @param string $api_base_download_uri
307+
*/
308+
public static function setCustomBotApiUri(string $api_base_uri, string $api_base_download_uri = ''): void
309+
{
310+
self::$api_base_uri = $api_base_uri;
311+
if ($api_base_download_uri !== '') {
312+
self::$api_base_download_uri = $api_base_download_uri;
313+
}
314+
}
315+
295316
/**
296317
* Get input from custom input or stdin and return it
297318
*
@@ -532,8 +553,9 @@ public static function downloadFile(File $file): bool
532553
$debug_handle = TelegramLog::getDebugLogTempStream();
533554

534555
try {
556+
$base_download_uri = str_replace('{API_KEY}', self::$telegram->getApiKey(), self::$api_base_download_uri);
535557
self::$client->get(
536-
'/file/bot' . self::$telegram->getApiKey() . '/' . $tg_file_path,
558+
"{$base_download_uri}/{$tg_file_path}",
537559
['debug' => $debug_handle, 'sink' => $file_path]
538560
);
539561

0 commit comments

Comments
 (0)