phptdgram/td-client

PHP TD Gram TdClient Component

1.0.0 2020-04-13 10:54 UTC

This package is auto-updated.

Last update: 2024-12-13 21:30:30 UTC


README

Latest Version on Packagist Software License Build Status Code Quality Code Coverage Mutation testing badge Total Downloads

Email

The PHP TD Gram TdLib Client. Manages communication between TdLib and PHP. Accepts phptdgram/adapter based adapters. Works with phptdgram/schema objects.

Install

Via Composer

$ composer require phptdgram/td-client

Basic Usage

<?php

use PHPTdGram\Schema\FormattedText;
use PHPTdGram\Schema\InputMessageText;
use PHPTdGram\Schema\SendMessage;
use PHPTdGram\Schema\SendMessageOptions;
use PHPTdGram\Schema\TdObject;
use PHPTdGram\TdClient\TdClient;

$adapter = new FFIAdapter();

$tdClient = new TdClient($adapter);
$tdClient->verifyVersion(); // Make sure that libtdjson version and our Schema version matches

while (true) {
    /** @var TdObject $packet */
    $packet = $tdClient->receive(10);

    // ... Your logic

    $sendMessagePacket = new SendMessage(
        312321312,
        0,
        new SendMessageOptions(
            // ...
        ),
        null,
        new InputMessageText(
            new FormattedText(
                'Hello world',
                []
            ),
            false,
            true,
        )
    );
    
    $tdClient->send($sendMessagePacket);
}

Reference

<?php

declare(strict_types=1);

namespace PHPTdGram\TdClient;

class TdClient
{
    public function __construct(AdapterInterface $adapter, LoggerInterface $logger = null);

    /**
     * @throws AdapterException
     * @throws JsonException
     * @throws TdClientException
     */
    public function verifyVersion(): void;

    /**
     * @param float $timeout        the maximum number of seconds allowed for this function to wait for new data
     * @param bool  $processBacklog should process backlog packets
     *
     * @throws AdapterException
     * @throws ErrorReceivedException
     * @throws JsonException
     */
    public function receive(float $timeout, bool $processBacklog = true): ?TdObject;

    /**
     * @param int $level New value of the verbosity level for logging. Value 0 corresponds to fatal errors, value 1
     *                   corresponds to errors, value 2 corresponds to warnings and debug warnings, value 3 corresponds
     *                   to informational, value 4 corresponds to debug, value 5 corresponds to verbose debug, value
     *                   greater than 5 and up to 1023 can be used to enable even more logging.
     *
     * @return $this
     *
     * @throws AdapterException
     * @throws JsonException
     */
    public function setLogVerbosityLevel(int $level): self;

    /**
     * @param string $file           path to the file to where the internal TDLib log will be written
     * @param int    $maxLogFileSize the maximum size of the file to where the internal TDLib log is written before the
     *                               file will be auto-rotated
     *
     * @return $this
     *
     * @throws AdapterException
     * @throws JsonException
     */
    public function setLogToFile(string $file, int $maxLogFileSize = PHP_INT_MAX): self;

    /**
     * @return $this
     *
     * @throws AdapterException
     * @throws JsonException
     */
    public function setLogToStderr(): self;

    /**
     * @return $this
     *
     * @throws AdapterException
     * @throws JsonException
     */
    public function setLogToNone(): self;
    }

    /**
     * Sends packet to TdLib marked with extra identifier and loops till received marked response back or timeout
     * occurs. Stores all in between packets in backlog
     *
     * @param TdFunction $packet         request packet to send to TdLib
     * @param int        $timeout        the maximum number of seconds allowed for this function to wait for a response
     *                                   packet
     * @param float      $receiveTimeout the maximum number of seconds allowed for this function to wait for new data
     *
     * @throws AdapterException
     * @throws ErrorReceivedException
     * @throws JsonException
     * @throws QueryTimeoutException
     */
    public function query(TdFunction $packet, int $timeout = 10, float $receiveTimeout = 0.1): TdObject;

    /**
     * Sends packet to TdLib
     *
     * @param TdFunction $packet request packet to send to TdLib
     *
     * @throws AdapterException
     * @throws JsonException
     */
    public function send(TdFunction $packet): void;
}

Testing

Run test cases

$ composer test

Run test cases with coverage (HTML format)

$ composer test-coverage

Run PHP style checker

$ composer cs-check

Run PHP style fixer

$ composer cs-fix

Run all continuous integration tests

$ composer ci-run

Contributing

Please see CONTRIBUTING and CONDUCT for details.

License

Please see License File for more information.