phptdgram/td-client

PHP TD Gram TdClient 组件

1.0.0 2020-04-13 10:54 UTC

This package is auto-updated.

Last update: 2024-09-13 20:57:28 UTC


README

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

Email

PHP TD Gram TdLib 客户端。管理 TdLib 和 PHP 之间的通信。接受基于 phptdgram/adapter 的适配器。与 phptdgram/schema 对象协同工作。

安装

通过 Composer

$ composer require phptdgram/td-client

基本用法

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

参考

<?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;
}

测试

运行测试用例

$ composer test

运行带覆盖率的测试用例(HTML 格式)

$ composer test-coverage

运行 PHP 风格检查器

$ composer cs-check

运行 PHP 风格修复器

$ composer cs-fix

运行所有持续集成测试

$ composer ci-run

贡献

请参阅 CONTRIBUTINGCONDUCT 以获取详细信息。

许可证

请参阅 许可证文件 以获取更多信息。