Skip to content

Commit

Permalink
Structure refactoring (#132)
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik authored Jan 10, 2025
1 parent 5976a1e commit 1d3e639
Show file tree
Hide file tree
Showing 319 changed files with 1,362 additions and 1,404 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# Telegram Bot API for PHP Change Log

## 0.6.0 under development

- Chg #132: Rename definitions "telegram client" to "transport", "telegram response" to "api response" and
"telegram request" to "method". Classes, namespaces, and variables rename accordingly.
- Chg #132: Remove `TelegramRequest`.
- Chg #132: Rename `TelegramClientInterface` to `TransportInterface` and change `send()` method signature.
- Chg #132: Rename `TelegramRequestWithResultPreparingInterface` to `MethodInterface`.
- Chg #132: Rename `RequestFileCollector` to `FileCollector` and move to root namespace.
- Chg #132: Rename `TelegramBotApi::send()` method to `call()`.

## 0.5.0 January 02, 2025

- New #129: Add `verifyUser`, `verifyChat`, `removeUserVerification` and `removeChatVerification` methods.
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ composer require vjik/telegram-bot-api
## General usage

To make requests to the Telegram Bot API, you need to create an instance of the `TelegramBotApi` class
that requires an instance of the `TelegramClientInterface` implementation. Out of the box, the package provides `PsrTelegramClient` based on 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.
that requires an instance of the `TransportInterface` implementation. Out of the box, the package provides
`PsrTransport` based on 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):

Expand All @@ -42,7 +42,7 @@ use Http\Client\Curl\Client;
use HttpSoft\Message\RequestFactory;
use HttpSoft\Message\ResponseFactory;
use HttpSoft\Message\StreamFactory;
use Vjik\TelegramBot\Api\Client\PsrTelegramClient;
use Vjik\TelegramBot\Api\Transport\PsrTransport;
use Vjik\TelegramBot\Api\TelegramBotApi;

// Telegram bot authentication token
Expand All @@ -56,7 +56,7 @@ $client = new Client($responseFactory, $streamFactory);

// API
$api = new TelegramBotApi(
new PsrTelegramClient(
new PsrTransport(
$token,
$client,
$requestFactory,
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@
"psr/http-message": "^1.1|^2.0"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.65",
"friendsofphp/php-cs-fixer": "^3.67",
"httpsoft/http-message": "^1.1.6",
"maglnet/composer-require-checker": "^4.14",
"php-http/curl-client": "^2.3.3",
"phpunit/phpunit": "^10.5.38",
"phpunit/phpunit": "^10.5.40",
"psr/log": "^3.0.2",
"roave/infection-static-analysis-plugin": "^1.35",
"vimeo/psalm": "^5.26.1",
Expand Down
14 changes: 9 additions & 5 deletions docs/custom-requests.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
# Custom requests

You can make custom requests using the `send()` method and `TelegramRequest` object:
You can make custom requests to API using the `call()` method and `CustomMethod` object:

```php
use Vjik\TelegramBot\Api\Request\TelegramRequest;
use Vjik\TelegramBot\Api\CustomMethod;

$request = new TelegramRequest(
httpMethod: HttpMethod::GET,
/**
* @var \Vjik\TelegramBot\Api\TelegramBotApi $api
*/

$method = new CustomMethod(
apiMethod: 'getChat',
data: ['chat_id' => '@sergei_predvoditelev'],
resultType: ChatFullInfo::class,
httpMethod: HttpMethod::GET,
);

// Result is an object of `Vjik\TelegramBot\Api\Type\ChatFullInfo`
$result = $api->send($request);
$result = $api->call($method);
```
16 changes: 8 additions & 8 deletions docs/logging.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ Pass logger to `TelegramBotApi` constructor:

```php
use Psr\Log\LoggerInterface;
use Vjik\TelegramBot\Api\Client\TelegramClientInterface;
use Vjik\TelegramBot\Api\Transport\TransportInterface;
use Vjik\TelegramBot\Api\TelegramBotApi;

/**
* @var TelegramClientInterface $telegramClient
* @var TransportInterface $transport
* @var LoggerInterface $logger
*/

// API
$api = new TelegramBotApi($telegramClient, $logger);
$api = new TelegramBotApi($transport, $logger);
```

You can use logger on create `Update` object also:
Expand Down Expand Up @@ -49,7 +49,7 @@ To logger passed 4 types of messages.
[
'type' => LogType::SEND_REQUEST,
'payload' => $payload, // Request data as JSON encoded string
'request' => $request, // `TelegramRequestInterface` implementation
'method' => $method, // `MethodInterface` implementation
]
```

Expand All @@ -59,8 +59,8 @@ To logger passed 4 types of messages.
[
'type' => LogType::SUCCESS_RESULT,
'payload' => $payload, // Response body as JSON encoded string
'request' => $request, // `TelegramRequestInterface` implementation
'response' => $response, // `TelegramResponse` object
'method' => $method, // `MethodInterface` implementation
'response' => $response, // `ApiResponse` object
'decodedResponse' => $decodedResponse, // Decoded response body as array
]
```
Expand All @@ -71,8 +71,8 @@ To logger passed 4 types of messages.
[
'type' => LogType::FAIL_RESULT,
'payload' => $payload, // Response body as JSON encoded string
'request' => $request, // `TelegramRequestInterface` implementation
'response' => $response, // `TelegramResponse` object
'method' => $method, // `MethodInterface` implementation
'response' => $response, // `ApiResponse` object
'decodedResponse' => $decodedResponse, // Decoded response body as array
]
```
Expand Down
12 changes: 0 additions & 12 deletions src/Client/TelegramClientInterface.php

This file was deleted.

19 changes: 10 additions & 9 deletions src/Request/TelegramRequest.php → src/CustomMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,28 @@

declare(strict_types=1);

namespace Vjik\TelegramBot\Api\Request;
namespace Vjik\TelegramBot\Api;

use Vjik\TelegramBot\Api\ParseResult\ValueProcessor\ValueProcessorInterface;
use Vjik\TelegramBot\Api\Transport\HttpMethod;

/**
* @template T as class-string|ValueProcessorInterface|null
* @template-implements TelegramRequestWithResultPreparingInterface<T>
* @template-implements MethodInterface<T>
*/
final readonly class TelegramRequest implements TelegramRequestWithResultPreparingInterface
final readonly class CustomMethod implements MethodInterface
{
/**
* @psalm-param array<string,mixed> $data
* @psalm-param T $resultType
*/
public function __construct(
private HttpMethod $httpMethod,
private string $apiMethod,
private array $data = [],
private string|ValueProcessorInterface|null $resultType = null,
private HttpMethod $httpMethod = HttpMethod::POST,
) {}

public function getHttpMethod(): HttpMethod
{
return $this->httpMethod;
}

public function getApiMethod(): string
{
return $this->apiMethod;
Expand All @@ -42,4 +38,9 @@ public function getResultType(): string|ValueProcessorInterface|null
{
return $this->resultType;
}

public function getHttpMethod(): HttpMethod
{
return $this->httpMethod;
}
}
7 changes: 3 additions & 4 deletions src/FailResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@

namespace Vjik\TelegramBot\Api;

use Vjik\TelegramBot\Api\Client\TelegramResponse;
use Vjik\TelegramBot\Api\Request\TelegramRequestInterface;
use Vjik\TelegramBot\Api\Transport\ApiResponse;
use Vjik\TelegramBot\Api\Type\ResponseParameters;

final readonly class FailResult
{
public function __construct(
public TelegramRequestInterface $request,
public TelegramResponse $response,
public MethodInterface $method,
public ApiResponse $response,
public ?string $description = null,
public ?ResponseParameters $parameters = null,
public mixed $errorCode = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

declare(strict_types=1);

namespace Vjik\TelegramBot\Api\Request;
namespace Vjik\TelegramBot\Api;

use Vjik\TelegramBot\Api\Type\InputFile;

final class RequestFileCollector
final class FileCollector
{
private int $counter;

Expand All @@ -16,7 +16,7 @@ final class RequestFileCollector
private array $files = [];

public function __construct(
private string $prefix = 'file',
private readonly string $prefix = 'file',
int $start = 0,
) {
$this->counter = $start;
Expand Down
31 changes: 15 additions & 16 deletions src/LogType.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,28 @@
namespace Vjik\TelegramBot\Api;

use JsonException;
use Vjik\TelegramBot\Api\Client\TelegramResponse;
use Vjik\TelegramBot\Api\Request\TelegramRequestInterface;
use Vjik\TelegramBot\Api\Transport\ApiResponse;

/**
* @psalm-type SendRequestContext = array{
* type: LogType::SEND_REQUEST,
* payload: string,
* request: TelegramRequestInterface,
* method: MethodInterface,
* }
*
* @psalm-type SuccessResultContext = array{
* type: LogType::SUCCESS_RESULT,
* payload: string,
* request: TelegramRequestInterface,
* response: TelegramResponse,
* method: MethodInterface,
* response: ApiResponse,
* decodedResponse: mixed
* }
*
* @psalm-type FailResultContext = array{
* type: LogType::FAIL_RESULT,
* payload: string,
* request: TelegramRequestInterface,
* response: TelegramResponse,
* method: MethodInterface,
* response: ApiResponse,
* decodedResponse: mixed
* }
*
Expand All @@ -46,26 +45,26 @@
/**
* @psalm-return SendRequestContext
*/
public static function createSendRequestContext(TelegramRequestInterface $request): array
public static function createSendRequestContext(MethodInterface $method): array
{
try {
$payload = json_encode($request->getData(), JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE);
$payload = json_encode($method->getData(), JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE);
} catch (JsonException) {
$payload = '%UNABLE_DATA%';
}
return [
'type' => self::SEND_REQUEST,
'payload' => $payload,
'request' => $request,
'method' => $method,
];
}

/**
* @psalm-return SuccessResultContext
*/
public static function createSuccessResultContext(
TelegramRequestInterface $request,
TelegramResponse $response,
MethodInterface $method,
ApiResponse $response,
mixed $decodedResponse,
): array {
try {
Expand All @@ -76,7 +75,7 @@ public static function createSuccessResultContext(
return [
'type' => self::SUCCESS_RESULT,
'payload' => $payload,
'request' => $request,
'method' => $method,
'response' => $response,
'decodedResponse' => $decodedResponse,
];
Expand All @@ -86,14 +85,14 @@ public static function createSuccessResultContext(
* @psalm-return FailResultContext
*/
public static function createFailResultContext(
TelegramRequestInterface $request,
TelegramResponse $response,
MethodInterface $method,
ApiResponse $response,
mixed $decodedResponse,
): array {
return [
'type' => self::FAIL_RESULT,
'payload' => $response->body,
'request' => $request,
'method' => $method,
'response' => $response,
'decodedResponse' => $decodedResponse,
];
Expand Down
8 changes: 4 additions & 4 deletions src/Method/AnswerCallbackQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
namespace Vjik\TelegramBot\Api\Method;

use Vjik\TelegramBot\Api\ParseResult\ValueProcessor\TrueValue;
use Vjik\TelegramBot\Api\Request\HttpMethod;
use Vjik\TelegramBot\Api\Request\TelegramRequestWithResultPreparingInterface;
use Vjik\TelegramBot\Api\Transport\HttpMethod;
use Vjik\TelegramBot\Api\MethodInterface;

/**
* @see https://core.telegram.org/bots/api#answercallbackquery
*
* @template-implements TelegramRequestWithResultPreparingInterface<TrueValue>
* @template-implements MethodInterface<TrueValue>
*/
final readonly class AnswerCallbackQuery implements TelegramRequestWithResultPreparingInterface
final readonly class AnswerCallbackQuery implements MethodInterface
{
public function __construct(
private string $callbackQueryId,
Expand Down
8 changes: 4 additions & 4 deletions src/Method/ApproveChatJoinRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
namespace Vjik\TelegramBot\Api\Method;

use Vjik\TelegramBot\Api\ParseResult\ValueProcessor\TrueValue;
use Vjik\TelegramBot\Api\Request\HttpMethod;
use Vjik\TelegramBot\Api\Request\TelegramRequestWithResultPreparingInterface;
use Vjik\TelegramBot\Api\Transport\HttpMethod;
use Vjik\TelegramBot\Api\MethodInterface;

/**
* @see https://core.telegram.org/bots/api#approvechatjoinrequest
*
* @template-implements TelegramRequestWithResultPreparingInterface<TrueValue>
* @template-implements MethodInterface<TrueValue>
*/
final readonly class ApproveChatJoinRequest implements TelegramRequestWithResultPreparingInterface
final readonly class ApproveChatJoinRequest implements MethodInterface
{
public function __construct(
private int|string $chatId,
Expand Down
8 changes: 4 additions & 4 deletions src/Method/BanChatMember.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@

use DateTimeInterface;
use Vjik\TelegramBot\Api\ParseResult\ValueProcessor\TrueValue;
use Vjik\TelegramBot\Api\Request\HttpMethod;
use Vjik\TelegramBot\Api\Request\TelegramRequestWithResultPreparingInterface;
use Vjik\TelegramBot\Api\Transport\HttpMethod;
use Vjik\TelegramBot\Api\MethodInterface;

/**
* @see https://core.telegram.org/bots/api#banchatmember
*
* @template-implements TelegramRequestWithResultPreparingInterface<TrueValue>
* @template-implements MethodInterface<TrueValue>
*/
final readonly class BanChatMember implements TelegramRequestWithResultPreparingInterface
final readonly class BanChatMember implements MethodInterface
{
public function __construct(
private int|string $chatId,
Expand Down
Loading

0 comments on commit 1d3e639

Please sign in to comment.