PHP code example of chatflowphp / telegram

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

    

chatflowphp / telegram example snippets


use ChatFlow\Core\Context;
use ChatFlow\Scene\BaseScene;
use ChatFlow\Scene\RootScene;
use ChatFlow\Storage\Drivers\FileStorage;
use ChatFlow\Telegram\Bot;
use ChatFlow\View\Action;
use ChatFlow\View\View;

final class PhoneScene extends BaseScene
{
    public function handle(Context $ctx): void
    {
        $ctx->ask('Send your phone in +79991234567 format')
            ->validate('regex:/^\+7\d{10}$/', 'Use +79991234567.')
            ->onText('cancel', 'onCancel')
            ->handle('savePhone');
    }

    public function savePhone(Context $ctx): void
    {
        $ctx->session()->set('phone', $ctx->getText());
        $ctx->reply('Saved');
        $ctx->leave();
    }

    public function onCancel(Context $ctx): void
    {
        $ctx->leave();
    }
}

$bot = new Bot($_ENV['TELEGRAM_BOT_TOKEN'], __DIR__, storage: new FileStorage(__DIR__ . '/storage/bot'));

$bot->registerScene(PhoneScene::class);
$bot->allowTransition(RootScene::ID, PhoneScene::class);

$bot->command('start', static function (Context $ctx): void {
    $ctx->reply(View::text('Hello')->addActionRow(new Action('phone:edit', 'Set phone')));
});

$bot->onAction('phone:edit', static function (Context $ctx): void {
    $ctx->ack();
    $ctx->enter(PhoneScene::class);
});

$bot->runWebhook();

$bot->command('ask', static function (TelegramContext $telegram): void {
    $telegram->forceReply('Send the updated post text');
});

$bot->onTelegramEvent('my_chat_member', static function (TelegramContext $telegram, array $update): void {
    $telegram->reply('Membership changed');
});
sh
php examples/StarterBot/mock.php
php examples/MiniShop/mock.php
composer check