Search by

phptg / transport-amphp

vjik

AMPHP Transport for Telegram Bot API

Package info

github.com/phptg/transport-amphp

Chat

pkg:composer/phptg/transport-amphp

Fund package maintenance!

Cloudtips

Boosty

Statistics

Installs: 1

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

0.1 2026-12-03 14:20 UTC

This package is auto-updated.

Last update: 2026-12-03 14:26:21 UTC


README

PHPTG

AMPHP Transport for Telegram Bot API


Latest Stable Version Total Downloads Build status Coverage Status Mutation score Static analysis

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. null means a client built with HttpClientBuilder::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 to ApacheMimeTypeResolver.
  • $tcpConnectTimeout — timeout in seconds for establishing the TCP connection for each request. Defaults to 10.
  • $tlsHandshakeTimeout — timeout in seconds for the TLS handshake for each request. Defaults to 10.
  • $apiTransferTimeout — timeout in seconds for the entire transfer of API requests without file uploads, i.e. get() and post(), not counting the TCP connect and TLS handshake. Defaults to 60.
  • $uploadTransferTimeout — timeout in seconds for the entire transfer of API requests with file uploads, i.e. postWithFiles(). Defaults to 600.
  • $longPollingTimeoutMargin — timeout in seconds added to the long polling timeout, i.e. the maximum number of seconds the server may hold a longPoll() request, to get the transfer timeout for the request. Defaults to 10.
  • $downloadTransferTimeout — timeout in seconds for the entire transfer of file downloads, i.e. downloadFile() and downloadFileStream(). Defaults to 300.
  • $downloadInactivityTimeout — timeout in seconds since the last data was received before a file download fails due to inactivity. Defaults to 10.
  • $downloadBodySizeLimit — maximum size in bytes of the body downloaded by downloadFile() and downloadFileStream(). Defaults to 21 MiB, a margin over the 20 MB served by the default Bot API server; raise it or set to 0 for 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.