1. Go to this page and download the library: Download afaztech/neili 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/ */
afaztech / neili example snippets
Neili\Client;
use Neili\Poller;
use Neili\Settings;
$settings = (new Settings())
->setAccessToken('YOUR_BOT_TOKEN');
$client = new Client($settings);
$poller = new Poller($client);
$poller->onMessage(function (array $update) use ($client) {
$chatId = $update['message']['chat']['id'] ?? null;
$text = $update['message']['text'] ?? null;
if ($chatId && $text) {
$client->sendMessage($chatId, "Echo: {$text}");
}
});
$poller->start();
use Neili\Settings;
$settings = (new Settings())
->setAccessToken('123456:ABC-DEF...') // // transfer timeout, connection timeout
->setPollerTimeout(5)
->setPollerBackoffBase(1)
->setPollerMaxBackoff(32)
->setPollerMaxConcurrency(50);
$client = new \Neili\Client($settings);
use Neili\Settings;
use Monolog\Logger as MonologLogger;
use Monolog\Handler\StreamHandler;
$logger = new MonologLogger('bot', [new StreamHandler('/var/log/bot.log')]);
$settings = (new Settings($logger))->setAccessToken('YOUR_BOT_TOKEN');
use Neili\Logger;
$logger = new Logger('/var/log/neili.log', printToConsole: true);
use Amp\Http\Client\HttpClientBuilder;
$http = HttpClientBuilder::buildDefault();
$settings = (new Settings())
->setAccessToken('YOUR_BOT_TOKEN')
->setHttpClient($http);
$client->setHttpClient($newHttpClient);
$client->reconnect(); // rebuilds the default client from Settings
use Neili\Client;
use Neili\Poller;
use Neili\Settings;
$settings = (new Settings())->setAccessToken('YOUR_BOT_TOKEN');
$client = new Client($settings);
$poller = new Poller($client);
// Optional: catch errors raised inside any handler
$poller->onError(function (\Throwable $e, array $update, string $type) {
error_log("[$type] {$e->getMessage()}");
});
// Global handler (fires for every update)
$poller->onUpdate(function (array $update) {
// log all updates
});
// Type-specific handlers
$poller->onMessage(function (array $update) use ($client) {
$chatId = $update['message']['chat']['id'] ?? null;
$text = $update['message']['text'] ?? null;
if ($chatId && $text) {
$client->sendMessage($chatId, "Echo: {$text}");
}
});
$poller->onCallbackQuery(function (array $update) use ($client) {
$client->answerCallbackQuery($update['callback_query']['id'], 'Got it!');
});
// Start polling (default: discard pending updates at startup)
$poller->start(discardOldUpdates: true);
$poller->onMessage(function () use ($poller) {
$poller->stop();
});
use Neili\Client;
use Neili\Settings;
$settings = (new Settings())->setAccessToken('YOUR_BOT_TOKEN');
$client = new Client($settings);
$update = $client->handleUpdate('YOUR_SECRET_TOKEN');
if (isset($update['message']['chat']['id'], $update['message']['text'])) {
$client->sendMessage(
$update['message']['chat']['id'],
'Echo: ' . $update['message']['text']
)->await();
}
use Neili\Client;
use Neili\Settings;
$settings = (new Settings())
->setAccessToken('YOUR_BOT_TOKEN')
->setMultiProcess(true)
->setPhpBinary('/usr/bin/php');
$client = new Client($settings);
// In webhook.php — returns immediately after forking
$client->handleUpdate('YOUR_SECRET_TOKEN');
use Neili\Exceptions\NeiliException;
use Neili\Exceptions\PermanentException;
use Neili\Exceptions\RateLimitException;
use Neili\Exceptions\TransientException;
try {
$client->sendMessage(123, 'hi')->await();
} catch (RateLimitException $e) {
sleep($e->getRetryAfter());
// retry
} catch (TransientException $e) {
// backoff and retry
} catch (PermanentException $e) {
// log; do not retry — bad token, chat not found, etc.
echo $e->getErrorCode(); // e.g. 400
print_r($e->getParameters()); // raw "parameters" block
} catch (NeiliException $e) {
// catch-all
}
// RateLimitException
$e->getRetryAfter(): int // seconds, min 1
$e->getParameters(): array // e.g. ['retry_after' => 30]
// PermanentException
$e->getErrorCode(): int
$e->getParameters(): array
// TransientException / NeiliException
// Standard \RuntimeException API
use Neili\Media;
$photo = new Media('/path/to/photo.jpg');
echo $photo->filePath; // realpath
use Neili\Media;
$client->sendPhoto(123, new Media('/path/to/photo.jpg'), caption: 'Hello!')->await();
$client->sendMediaGroup(123, [
new Media('/path/to/a.jpg'),
new Media('/path/to/b.jpg'),
'https://example.com/remote.jpg',
], caption: 'Album')->await();
$client->sendDocument(123, new Media('/path/to/report.pdf'))->await();
// 1) Get file metadata
$info = $client->getFile('FILE_ID')->await();
$remotePath = $info['result']['file_path'];
// 2) Get a public download URL
$url = $client->getFileUrl($remotePath);
// → https://api.telegram.org/file/bot<TOKEN>/<file_path>
// 3) Stream to disk (64 KiB chunks — safe for large files)
$client->downloadFile('FILE_ID', '/path/to/save.bin')->await();