PHP code example of isamarin / alisa

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

    

isamarin / alisa example snippets




use isamarin\Alisa\Alisa;
use isamarin\Alisa\Response;
use isamarin\Alisa\Trigger;

 '/sessions');
// $bot->setDictionaryPath(__DIR__ . '/dicts'); // если есть phpmorphy + словари

$hello = (new Trigger('HELLO'))->setAsInit();
$mistake = (new Trigger('MISTAKE'))->setAsMistake();
$default = (new Trigger('DEFAULT'))->setAsDefault();

$bye = (new Trigger('BYE'))->linkTokens(['пока', 'до свидания', 'прощай']);

$bot->addTrigger($hello, $mistake, $default, $bye);

$bot->sendResponse($hello, static function () {
    return (new Response())->addText('Привет! Чем помочь?');
});

$bot->sendResponse($bye, static function () {
    return (new Response())
        ->addText('До встречи!')
        ->endSession(); // protocol: end_session
});

$bot->sendResponse($mistake, static function () {
    return (new Response())->addText('Не поняла. Попробуйте иначе.');
});

$bot->setExitOnSend(false);
$json = $bot->sendResponse($hello, fn () => (new Response())->addText('ok'));
// return $json; // Yandex Cloud Functions raw integration

$bot->sendResponse($tea, static function () use ($bot) {
    $prev = $bot->getRequest()->getSessionState();
    return (new Response())
        ->addText('Запомнила')
        ->setSessionState(['count' => ($prev['count'] ?? 0) + 1]);
});

$blackTea->linkTokens(['дай', 'хочу', 'налей'], ['чай'], ['зеленый']);

use isamarin\Alisa\DistanceRecognition;

$bot->setAlgorithm(new DistanceRecognition());
$coffee->linkTokens(['налей кофе'], ['хочу кофе']);

use isamarin\Alisa\Button;

$btn = new Button('Сайт');
$btn->addLink('https://example.com'); // → protocol field `url`
$btn->linkTrigger($someTrigger);
$answer->addButton($btn);