diff options
Diffstat (limited to 'src/NeonXP/BotScript.php')
-rw-r--r-- | src/NeonXP/BotScript.php | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/src/NeonXP/BotScript.php b/src/NeonXP/BotScript.php new file mode 100644 index 0000000..0a447bf --- /dev/null +++ b/src/NeonXP/BotScript.php @@ -0,0 +1,73 @@ +<?php + +namespace NeonXP; + +use Doctrine\Common\Cache\CacheProvider; +use \NeonXP\TelegramApi\Types\Message; +use \NeonXP\TelegramApi\Script; +use \NeonXP\TelegramApi\Script\State; +use \NeonXP\TelegramApi\Script\Conditions\isText; +use \NeonXP\TelegramApi\Script\Conditions\isMatch; +use \NeonXP\TelegramApi\Script\Conditions\isAnyText; +use \NeonXP\TelegramApi\Telegram; + +class BotScript +{ + public static function getScript(Telegram $telegram, DozorApi $dozorApi) + { + $saveLink = function (Message $message, Telegram $telegram) { + $telegram->getCache()->save("link_{$message->getChat()->getId()}", $message->getText()); + $telegram->getMethods()->sendMessage($message->getChat(), 'Ок, записал: ' . $message->getText()); + + return State::INITIAL; + }; + $showLink = function (Message $message, Telegram $telegram) { + $link = $telegram->getCache()->fetch("link_{$message->getChat()->getId()}"); + $telegram->getMethods()->sendMessage($message->getChat(), $link); + + return State::INITIAL; + }; + $sendCode = function (Message $message, Telegram $telegram) use ($dozorApi) { + $code = trim(substr($message->getText(),1)); + try { + $result = $dozorApi->sendCode($message->getChat(), $code); + }catch (\Exception $e) { + $telegram->getMethods()->sendMessage($message->getChat(), 'Ошибка сети', true); + } + + $telegram->getMethods()->sendMessage($message->getChat(), $result, true); + + + return State::INITIAL; //Set initial state + }; + $goBack = function (Message $message, Telegram $telegram) { + return State::INITIAL; + }; + $pause = function (Message $message, Telegram $telegram) { + return 'STATE_PAUSE'; + }; + + return (new Script($telegram))-> + state(State::INITIAL)-> //Start initial state + action(new isText('/link'), $showLink)-> + action(new isText('/setlink'), [ + 'Пришлите ссылку с пином /cancel', + 'STATE_SET_LINK' + ])-> + action(new isText('/pause'), [ + 'Замер. /resume для возврата', + 'STATE_PAUSE' + ])-> + action(new isMatch('/^\!.+?$/i'), $sendCode)-> + action(new isAnyText(), $goBack)-> + end()-> + state('STATE_SET_LINK')-> + action(new isText('/cancel'), $goBack)-> + action(new isAnyText(), $saveLink)-> + end()-> + state('STATE_PAUSE')-> + action(new isText('/resume'), ['Я вернулся!', State::INITIAL])-> + action(new isAnyText(), $pause)-> + end(); + } +}
\ No newline at end of file |