PHP code example of chatflowphp / core

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


use ChatFlow\Core\Context;
use ChatFlow\Scene\BaseScene;
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(View::text('Saved')->addActionRow($this->sceneAction('Back', 'onBack')));
    }

    public function onBack(Context $ctx): void
    {
        $ctx->back();
    }

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

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

$application->onCommand('phone', static function (Context $ctx): void {
    $ctx->enter(PhoneScene::class);
});

$application->onCommand('start', static function (Context $ctx): void {
    $ctx->reply('Hello');
});