Skip to content
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Telegram Bot API for PHP Change Log

## 0.16 under development

- Chg #186: Remove `PsrTransport`, `PsrWebhookResponseFactory` and `StreamResourceReader` classes.
- Chg #186: Remove `Update::fromServerRequest()` method.
- Chg #186: Remove PSR composer dependencies.

## 0.15 February 28, 2026

- Chg #187: Change `TransportInterface::downloadFile()` signature — remove `$stream` parameter, method now returns
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ The package provides a simple and convenient way to interact with the Telegram B

✔️ Telegram Bot API 9.4 (February 9, 2026) is **fully supported**.

♻️ **Zero dependencies** — no third-party libraries, only native PHP.

> [!IMPORTANT]
> This project is developed and maintained by [Sergei Predvoditelev](https://github.com/vjik).
> Community support helps keep the project actively developed and well maintained.
Expand Down
11 changes: 3 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,13 @@
}
],
"require": {
"php-64bit": "8.2 - 8.5",
"php-http/multipart-stream-builder": "^1.4.2",
"psr/http-client": "^1.0",
"psr/http-factory": "^1.1",
"psr/http-message": "^1.1 || ^2.0"
"php-64bit": "8.2 - 8.5"
},
"require-dev": {
"ext-curl": "*",
"bamarni/composer-bin-plugin": "^1.8.3",
"httpsoft/http-message": "^1.1.6",
"bamarni/composer-bin-plugin": "^1.9.1",
"php-http/curl-client": "^2.4.0",
"phpunit/phpunit": "^11.5.46",
"phpunit/phpunit": "^11.5.51",
"psr/log": "^3.0.2",
"yiisoft/files": "^2.1",
"yiisoft/test-support": "^3.2.0"
Expand Down
3 changes: 0 additions & 3 deletions docs/logging.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,14 @@ $api = new TelegramBotApi($token, logger: $logger);
You can use logger on create `Update` object also:

```php
use Psr\Http\Message\ServerRequestInterface;
use Psr\Log\LoggerInterface;
use Phptg\BotApi\Type\Update\Update;

/**
* @var ServerRequestInterface $request
* @var string $jsonString
* @var LoggerInterface $logger
*/

$update = Update::fromServerRequest($request, $logger);
$update = Update::fromJson($jsonString, $logger);
```

Expand Down
8 changes: 5 additions & 3 deletions docs/resource-readers.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ and uses the first one that supports the resource type.

## Built-in readers

The package provides two built-in resource readers:
The package provides one built-in resource reader:

- `NativeResourceReader` — handles native PHP stream resources created by functions like `fopen()`, `tmpfile()`, etc.
- `StreamResourceReader` — handles PSR-7 `StreamInterface` instances, commonly used in PSR-7 HTTP message
implementations.

## Additional readers

- `StreamResourceReader` — handles PSR-7 `StreamInterface` resources. Provided by the [phptg/transport-psr](https://github.com/phptg/transport-psr) package.

## Custom resource readers

Expand Down
53 changes: 6 additions & 47 deletions docs/transport.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ By default `TelegramBotApi` uses cURL to make requests to the Telegram Bot API a
But you can use any other transport implementation that implements
the `Phptg\BotApi\Transport\TransportInterface` interface.

Out of the box, available three transport implementations: cURL, native and PSR.
Out of the box, available two transport implementations: cURL and native.

Additionally, the [phptg/transport-psr](https://github.com/phptg/transport-psr) package provides `PsrTransport`
based on PSR-18 HTTP client and PSR-17 HTTP factories.

## cURL

Expand All @@ -29,7 +32,7 @@ $api = new TelegramBotApi($token, transport: $transport);
Constructor parameters:

- `$resourceReaders` — list of [resource readers](resource-readers.md) to handle different resource types. By default,
includes `NativeResourceReader` and `StreamResourceReader`.
includes `NativeResourceReader`.

## Native

Expand Down Expand Up @@ -58,7 +61,7 @@ Constructor parameters:

- `$mimeTypeResolver` — MIME type resolver for determining file types. Defaults to `ApacheMimeTypeResolver`.
- `$resourceReaders` — List of [resource readers](resource-readers.md) to handle different resource types. By default,
includes `NativeResourceReader` and `StreamResourceReader`.
includes `NativeResourceReader`.

Available MIME type resolvers:

Expand All @@ -67,47 +70,3 @@ Available MIME type resolvers:
by default);
- `CustomMimeTypeResolver` - based on file extension and custom MIME types map;
- `CompositeMimeTypeResolver` - allows to combine multiple resolvers into one.

## PSR

PSR transport requires the [PSR-18](https://www.php-fig.org/psr/psr-18/) HTTP client and [PSR-17](https://www.php-fig.org/psr/psr-17/) HTTP factories.

For example, you can use the [php-http/curl-client](https://github.com/php-http/curl-client) and [httpsoft/http-message](https://github.com/httpsoft/http-message):

```shell
composer require php-http/curl-client httpsoft/http-message
```

General usage:

```php
use Http\Client\Curl\Client;
use HttpSoft\Message\RequestFactory;
use HttpSoft\Message\ResponseFactory;
use HttpSoft\Message\StreamFactory;
use Phptg\BotApi\TelegramBotApi;
use Phptg\BotApi\Transport\PsrTransport;

$streamFactory = new StreamFactory();
$responseFactory = new ResponseFactory();
$requestFactory = new RequestFactory();
$client = new Client($responseFactory, $streamFactory);

// Telegram bot authentication token
$token = '110201543:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw';

$transport = new PsrTransport(
$client,
$requestFactory,
$streamFactory,
);

$api = new TelegramBotApi($token, transport: $transport);
```

Constructor parameters:

- `$client` — PSR-18 HTTP client;
- `$requestFactory` — PSR-17 HTTP request factory;
- `$streamFactory` — PSR-17 HTTP stream factory;

62 changes: 9 additions & 53 deletions docs/webhook-handling.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ This guide explains how to handle incoming webhook updates from Telegram and how

You can create an `Update` object by several ways:

- [from PSR-7 request](#from-psr-7-request),
- [from JSON string](#from-json-string),
- [via constructor](#via-constructor).

If `Update` created by `fromJson()` or `fromServerRequest()` method, you can get raw data via `getRaw()` method:
Additionally, the [phptg/transport-psr](https://github.com/phptg/transport-psr) package provides `PsrUpdateFactory`
that creates `Update` object from a PSR-7 `ServerRequestInterface`.

If `Update` created by `fromJson()` method, you can get raw data via `getRaw()` method:

```php
/**
Expand Down Expand Up @@ -55,26 +57,6 @@ $raw = $update->getRaw();
$raw = $update->getRaw(decoded: true);
```

### From PSR-7 request

Creating `Update` object from the incoming webhook PSR-7 request:

```php
use Psr\Http\Message\ServerRequestInterface;
use Phptg\BotApi\ParseResult\TelegramParseResultException;
use Phptg\BotApi\Type\Update\Update;

/**
* @var ServerRequestInterface $request
*/

try {
$update = Update::fromServerRequest($request);
} catch (TelegramParseResultException $e) {
// ...
}
```

### From JSON string

Creating `Update` object from JSON string received from POST request body:
Expand Down Expand Up @@ -137,40 +119,14 @@ if ($webhookResponse->isSupported()) {
}
```

### PSR-7 response factory

The `PsrWebhookResponseFactory` creates PSR-7 compliant HTTP responses for webhook handlers:

```php
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Phptg\BotApi\Method\SendMessage;
use Phptg\BotApi\WebhookResponse\PsrWebhookResponseFactory;
use Phptg\BotApi\WebhookResponse\WebhookResponse;

/**
* @var ResponseFactoryInterface $responseFactory
* @var StreamFactoryInterface $streamFactory
*/

$factory = new PsrWebhookResponseFactory($responseFactory, $streamFactory);

// Create response from WebhookResponse object
$webhookResponse = new WebhookResponse(new SendMessage(chatId: 12345, text: 'Hello!'));
$response = $factory->create($webhookResponse);

// Or create response directly from method, if you are sure that InputFile is not used
$method = new SendMessage(chatId: 12345, text: 'Hello!');
$response = $factory->byMethod($method);
```
### Response factories

The factory automatically:
The package provides a built-in `JsonWebhookResponseFactory` that creates JSON strings for webhook responses.

- encodes the data as JSON;
- sets the `Content-Type` header to `application/json; charset=utf-8`;
- sets the `Content-Length` header.
Additionally, the [phptg/transport-psr](https://github.com/phptg/transport-psr) package provides
`PsrWebhookResponseFactory` that creates a PSR-7 `ResponseInterface`.

### JSON response factory
#### JSON response factory

The `JsonWebhookResponseFactory` creates JSON strings for webhook responses:

Expand Down
2 changes: 0 additions & 2 deletions src/Transport/CurlTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use Phptg\BotApi\Curl\CurlInterface;
use Phptg\BotApi\Transport\ResourceReader\NativeResourceReader;
use Phptg\BotApi\Transport\ResourceReader\ResourceReaderInterface;
use Phptg\BotApi\Transport\ResourceReader\StreamResourceReader;

use function is_int;

Expand All @@ -29,7 +28,6 @@
public function __construct(
private array $resourceReaders = [
new NativeResourceReader(),
new StreamResourceReader(),
],
private CurlInterface $curl = new Curl(),
) {
Expand Down
2 changes: 0 additions & 2 deletions src/Transport/NativeTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Phptg\BotApi\Transport\ResourceReader\NativeResourceReader;
use Phptg\BotApi\Transport\ResourceReader\ResourceReaderInterface;
use Phptg\BotApi\Transport\ResourceReader\StreamResourceReader;
use RuntimeException;
use Phptg\BotApi\Transport\MimeTypeResolver\ApacheMimeTypeResolver;
use Phptg\BotApi\Transport\MimeTypeResolver\MimeTypeResolverInterface;
Expand All @@ -32,7 +31,6 @@ public function __construct(
private MimeTypeResolverInterface $mimeTypeResolver = new ApacheMimeTypeResolver(),
private array $resourceReaders = [
new NativeResourceReader(),
new StreamResourceReader(),
],
) {}

Expand Down
Loading