PHP code example of phptdgram / td-client

1. Go to this page and download the library: Download phptdgram/td-client 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/ */

    

phptdgram / td-client example snippets




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



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