phptg/transport-psr

PSR Transport for Telegram Bot API

Fund package maintenance!
Cloudtips
Boosty

Installs: 0

Dependents: 0

Suggesters: 0

Security: 0

Stars: 1

Watchers: 1

Forks: 0

Open Issues: 0

pkg:composer/phptg/transport-psr

0.1.1 2026-01-27 14:43 UTC

This package is auto-updated.

Last update: 2026-01-28 14:38:14 UTC


README

PHPTG

PSR 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:

  • PSR-18 and PSR-17 compatible transport implementation;
  • PSR-7 webhook response factory.

It allows you to use any PSR-compliant HTTP client to make requests to the Telegram Bot API.

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.5.

Installation

The package can be installed with Composer:

composer require phptg/transport-psr

General usage

First, install a PSR-18 HTTP client and PSR-17 HTTP factories. For example, you can use php-http/curl-client and httpsoft/http-message:

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

PSR transport

Create an instance of PsrTransport and pass it to TelegramBotApi:

use Http\Client\Curl\Client;
use HttpSoft\Message\RequestFactory;
use HttpSoft\Message\ResponseFactory;
use HttpSoft\Message\StreamFactory;
use Phptg\BotApi\TelegramBotApi;
use Phptg\TransportPsr\PsrTransport;

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

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

$api = new TelegramBotApi(
    token: '110201543:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw',
    transport: $transport,
);

// Now you can use the API as usual
$api->sendMessage(
    chatId: 123456789,
    text: 'Hello from PSR transport!',
);

PsrTransport constructor parameters:

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

PSR webhook response factory

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

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);

The factory automatically:

  • encodes the data as JSON;
  • sets the Content-Type header to application/json; charset=utf-8;
  • sets the Content-Length header.

Documentation

If you have any questions or problems with this package, use author telegram chat for communication.

License

The phptg/transport-psr is free software. It is released under the terms of the BSD License. Please see LICENSE for more information.