phptg / transport-amphp
AMPHP Transport for Telegram Bot API
Requires
- php: 8.2 - 8.6
- amphp/amp: ^3
- amphp/byte-stream: ^2
- amphp/http-client: ^5.3.8
- phptg/bot-api: ^0.25
Requires (Dev)
- amphp/file: ^3.0
- amphp/http-tunnel: ^2.0.1
- amphp/socket: ^2.4
- bamarni/composer-bin-plugin: ^1.9.1
- llm/skills: ^1.23
- phpstan/phpstan: ^2.3.7
- phpstan/phpstan-phpunit: ^2.1.2
- phpunit/phpunit: ^11.5.58
- shipmonk/dead-code-detector: ^1.4.2
Suggests
- amphp/file: Required to upload a file passed as a path or a regular file resource.
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-12-03 14:26:21 UTC
README
AMPHP Transport for Telegram Bot API
The package provides for phptg/bot-api:
AmpTransport— transport implementation based on the Amp HTTP client.
It allows you to make requests to the Telegram Bot API and download files with the non-blocking Amp HTTP client, so they do not block the event loop and are performed concurrently with other asynchronous code, e.g. requests of other bots in the same process.
Important
This project is developed and maintained by Sergei Predvoditelev. Community support helps keep the project actively developed and well maintained. You can support the project using the following services:
Thank you for your support ❤️
Requirements
- PHP 8.2 - 8.6.
Installation
The package can be installed with Composer:
composer require phptg/transport-amphp
To upload a local file, i.e. an InputFile with a file path or a regular file resource, also install
the amphp/file package, see file uploading:
composer require amphp/file
General usage
Create an instance of AmpTransport and pass it to TelegramBotApi:
use Phptg\BotApi\TelegramBotApi; use Phptg\TransportAmphp\AmpTransport; $api = new TelegramBotApi( token: '110201543:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw', transport: new AmpTransport(), ); // Now you can use the API as usual $api->sendMessage( chatId: 123456789, text: 'Hello from Amp transport!', );
A request does not block the event loop while waiting for the network, so requests started with Amp\async()
are performed concurrently:
use Amp\async; use function Amp\Future\awaitAll; /** * @var TelegramBotApi $api */ [$results] = awaitAll([ async(fn() => $api->sendMessage(chatId: 123456789, text: 'Hello!')), async(fn() => $api->sendMessage(chatId: 987654321, text: 'Hello!')), ]);
AmpTransport constructor parameters:
$client— Amp HTTP client for making requests.nullmeans a client built withHttpClientBuilder::buildDefault()is used. A custom client may be used, for example, to tune the connection pool, set up a proxy or add interceptors, see custom HTTP client. The default client retries GET requests, i.e.get(),longPoll()and file downloads, up to twice on any network error, so such a request may take up to three times the corresponding timeout; a POST request is retried only if it has not been sent yet.$mimeTypeResolver— MIME type resolver for determining file types. Defaults toApacheMimeTypeResolver.$tcpConnectTimeout— timeout in seconds for establishing the TCP connection for each request. Defaults to10.$tlsHandshakeTimeout— timeout in seconds for the TLS handshake for each request. Defaults to10.$apiTransferTimeout— timeout in seconds for the entire transfer of API requests without file uploads, i.e.get()andpost(), not counting the TCP connect and TLS handshake. Defaults to60.$uploadTransferTimeout— timeout in seconds for the entire transfer of API requests with file uploads, i.e.postWithFiles(). Defaults to600.$longPollingTimeoutMargin— timeout in seconds added to the long polling timeout, i.e. the maximum number of seconds the server may hold alongPoll()request, to get the transfer timeout for the request. Defaults to10.$downloadTransferTimeout— timeout in seconds for the entire transfer of file downloads, i.e.downloadFile()anddownloadFileStream(). Defaults to300.$downloadInactivityTimeout— timeout in seconds since the last data was received before a file download fails due to inactivity. Defaults to10.$downloadBodySizeLimit— maximum size in bytes of the body downloaded bydownloadFile()anddownloadFileStream(). Defaults to 21 MiB, a margin over the 20 MB served by the default Bot API server; raise it or set to0for a local Bot API server, which serves files of any size.
0 disables any timeout; for $downloadBodySizeLimit it means no limit, which slows down HTTP/2 downloads.
Custom HTTP client
A custom client built with HttpClientBuilder may be passed to the transport,
e.g. to disable retries or add interceptors:
use Amp\Http\Client\HttpClientBuilder; use Phptg\TransportAmphp\AmpTransport; $transport = new AmpTransport( (new HttpClientBuilder()) ->retry(0) ->build(), );
To use a proxy, install amphp/http-tunnel and build the client with a connection pool that connects through the tunnel:
composer require amphp/http-tunnel
use Amp\Http\Client\Connection\DefaultConnectionFactory; use Amp\Http\Client\Connection\UnlimitedConnectionPool; use Amp\Http\Client\HttpClientBuilder; use Amp\Http\Tunnel\Http1TunnelConnector; $transport = new AmpTransport( (new HttpClientBuilder()) ->usingPool(new UnlimitedConnectionPool( new DefaultConnectionFactory(new Http1TunnelConnector('127.0.0.1:3128')), )) ->build(), );
File uploading
A file passed as a path or a regular file resource is streamed from disk by the amphp/file package, which is not
installed with this package; without it, uploading such a file throws a LogicException:
use Phptg\BotApi\Type\InputFile; /** * @var TelegramBotApi $api */ $api->sendPhoto( chatId: 123456789, photo: new InputFile('/path/to/photo.jpg'), );
A file passed as any other stream, e.g. a php://temp resource, is uploaded without the package.
File downloading
downloadFile() buffers the whole file into a php://temp stream before returning, as required by
TransportInterface. To process a file chunk by chunk without buffering, use the downloadFileStream() method
specific to this transport:
use Phptg\BotApi\Type\File; /** * @var TelegramBotApi $api * @var AmpTransport $transport * @var File $file */ // Download the file and buffer it $content = $api->downloadFile($file)->getBody(); // Download the file as a stream and process it chunk by chunk $payload = $transport->downloadFileStream($api->makeFileUrl($file)); while (null !== $chunk = $payload->read()) { // Process the chunk, e.g. write it to a stream }
downloadFileStream() returns the response body as an Amp\ByteStream\Payload and accepts an Amp\Cancellation
token to abort the download. Unlike downloadFile(), it throws Amp\Http\Client\HttpException on failure instead
of DownloadFileException, and Amp\CancelledException if the download has been cancelled.
Documentation
If you have any questions or problems with this package, use author telegram chat for communication.
License
The phptg/transport-amphp is free software. It is released under the terms of the BSD License.
Please see LICENSE for more information.