- Install the package via composer
- Install
symfony/cache
via composer - Install a PSR-16 storage (to store dialog states between requests), for example
composer require symfony/cache
:- a. If you have Redis installed, you can use File driver (see example below)
- b. You have Redis installed: See
RedisAdapter
example below
File storage example.
use KootLabs\TelegramBotDialogs\DialogManager;
use KootLabs\TelegramBotDialogs\DialogRepository;
use KootLabs\TelegramBotDialogs\Dialogs\HelloExampleDialog;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
use Symfony\Component\Cache\Psr16Cache;
require __DIR__.'/vendor/autoload.php';
/** @todo replace by your token, {@see https://core.telegram.org/bots#6-botfather} */
$token = '110201543:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw';
$bot = new \Telegram\Bot\Api($token);
$store = new Psr16Cache(new FilesystemAdapter('', 0, __DIR__.'/store/'));
$dialogManager = new DialogManager($bot, new DialogRepository($store));
// A simplified example for updates polling. We recommend you using webhooks instead.
$updates = $bot->getUpdates(['offset' => $store->get('latest_update_id') + 1]);
foreach ($updates as $update) {
echo 'Received update: '.$update->update_id.PHP_EOL;
if (! $dialogManager->hasActiveDialog($update)) {
$dialog = new HelloExampleDialog($update->getChat()->id, $bot);
$dialogManager->activate($dialog);
}
$dialogManager->processUpdate($update);
}
if (isset($update)) {
$store->set('latest_update_id', $update->update_id);
} else {
echo 'No updates received'.PHP_EOL;
}
Important
a note for this example: The overhead of filesystem IO (FilesystemAdapter) often makes this adapter one of the slower choices. If throughput is paramount, the in-memory adapters ( Apcu, Memcached, and Redis) or the database adapters (PDO) are recommended.
Init redis store example (if you have Redis installed):
-$store = new Psr16Cache(new FilesystemAdapter('', 0, __DIR__.'/store/'));
+$redis = new \Redis();
+$redis->connect('127.0.0.1', 6379);
+$store = new Psr16Cache(new \Symfony\Component\Cache\Adapter\RedisAdapter($redis));
See Redis Cache Adapter for details.