1. Go to this page and download the library: Download phenogram/bindings library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
declare(strict_types=1);
namespace Phenogram\Bindings\Tests\Readme;
use Phenogram\Bindings\ClientInterface;
use Phenogram\Bindings\Types;
final readonly class ReadmeClient implements ClientInterface
{
public function __construct(
private string $token,
private string $apiUrl = 'https://api.telegram.org',
) {
}
public function sendRequest(string $method, array $data): Types\Interfaces\ResponseInterface
{
$ch = curl_init("{$this->apiUrl}/bot{$this->token}/{$method}");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
foreach ($data as $key => $value) {
if ($value instanceof Types\Interfaces\InputFileInterface) {
if (file_exists($value->filePath)) {
$data[$key] = new \CURLFile($value->filePath);
} else {
throw new \RuntimeException("File not found: {$value->filePath}");
}
}
}
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($ch);
if (curl_errno($ch)) {
throw new \RuntimeException('Request Error: ' . curl_error($ch));
}
curl_close($ch);
$responseData = json_decode($response, true);
if (!isset($responseData['ok']) || !isset($responseData['result'])) {
return new Types\Response(
ok: false,
errorCode: $responseData['error_code'] ?? null,
description: $responseData['description'] ?? null,
parameters: isset($responseData['parameters']) ? new Types\ResponseParameters(
migrateToChatId: $responseData['parameters']['migrate_to_chat_id'] ?? null,
retryAfter: $responseData['parameters']['retry_after'] ?? null,
) : null,
);
}
return new Types\Response(
ok: $responseData['ok'],
result: $responseData['result'],
errorCode: $responseData['error_code'] ?? null,
description: $responseData['description'] ?? null,
parameters: isset($responseData['parameters']) ? new Types\ResponseParameters(
migrateToChatId: $responseData['parameters']['migrate_to_chat_id'] ?? null,
retryAfter: $responseData['parameters']['retry_after'] ?? null,
) : null,
);
}
}
$api = new Api(
client: new TelegramBotApiClient($token),
serializer: new Serializer(),
);
$me = $api->getMe();
assert($me instanceof User::class);
class MyChatLocation extends \Phenogram\Bindings\Types\ChatLocation
{
public string $address {
get => $this->normalizeAddress($this->address);
}
private function normalizeAddress(string $address): string
{
return mb_strtoupper($address);
}
}
use Phenogram\Bindings\Factory;
use Phenogram\Bindings\Types\Interfaces\ChatLocationInterface;
use Phenogram\Bindings\Types\Interfaces\LocationInterface;
class MyFactory extends Factory
{
public function makeChatLocation(
LocationInterface $location,
string $address
): ChatLocationInterface
{
return new MyChatLocation(
location: $location,
address: $address,
);
}
}
$api = new Api(
client: new TelegramBotApiClient($token),
serializer: new Serializer(
factory: new MyFactory()
),
);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.