<?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()}");
if (!$link) {
$link = 'Ссылка не задана! Используй /setlink';
}
$telegram->getMethods()->sendMessage($message->getChat(), $link);
return State::INITIAL;
};
$sendCode = function (Message $message, Telegram $telegram) use ($dozorApi) {
$code = trim(substr($message->getText(), 1));
$result = false;
try {
$result = $dozorApi->sendCode($message->getChat(), $code);
} catch (\Exception $e) {
$telegram->getMethods()->sendMessage($message->getChat(), 'Ошибка сети', true);
}
if ($result) {
$telegram->getMethods()->sendMessage($message->getChat(), $result, true);
}
return State::INITIAL; //Set initial state
};
$getDb = function (Message $message, Telegram $telegram) {
$text = trim(substr($message->getText(), 1));
$db = [
'1' => ['А', 'А', 'A', 'Z', '000001', '. -'],
'2' => ['Б', 'Б', 'B', 'Y', '000010', '- . . .'],
'3' => ['В', 'В', 'C', 'X', '000011', '. - -'],
'4' => ['Г', 'Г', 'D', 'W', '000100', '- - .'],
'5' => ['Д', 'Д', 'E', 'V', '000101', '- . .'],
'6' => ['Е', 'Е', 'F', 'U', '000110', '.'],
'7' => ['Ё', 'Ж', 'G', 'T', '000111', '.'],
'8' => ['Ж', 'З', 'H', 'S', '001000', '. . . -'],
'9' => ['З', 'И', 'I', 'R', '001001', '- - . .'],
'10' => ['И', 'Й', 'J', 'Q', '001010', '. .'],
'11' => ['Й', 'К', 'K', 'P', '001011', '. - - -'],
'12' => ['К', 'Л', 'L', 'O', '001100', '- . -'],
'13' => ['Л', 'М', 'M', 'N', '001101', '. - . .'],
'14' => ['М', 'Н', 'N', 'M', '001110', '- -'],
'15' => ['Н', 'О', 'O', 'L', '001111', '- .'],
'16' => ['О', 'П', 'P', 'K', '010000', '- - -'],
'17' => ['П', 'Р', 'Q', 'J', '010001', '. - - .'],
'18' => ['Р', 'С', 'R', 'I', '010010', '. - .'],
'19' => ['С', 'Т', 'S', 'H', '010011', '. . .'],
'20' => ['Т', 'У', 'T', 'G', '010100', '-'],
'21' => ['У', 'Ф', 'U', 'F', '010101', '. . -'],
'22' => ['Ф', 'Х', 'V', 'E', '010110', '. . - .'],
'23' => ['Х', 'Ц', 'W', 'D', '010111', '. . . .'],
'24' => ['Ц', 'Ч', 'X', 'C', '011000', '- . - .'],
'25' => ['Ч', 'Ш', 'Y', 'B', '011001', '- - - .'],
'26' => ['Ш', 'Щ', 'Z', 'A', '011010', '- - - -'],
'27' => ['Щ', 'Ъ', ' ', ' ', '011011', '- - . -'],
'28' => ['Ъ', 'Ы', ' ', ' ', '011100', '- - . - -'],
'29' => ['Ы', 'Ь', ' ', ' ', '011101', '- . - -'],
'30' => ['Ь', 'Э', ' ', ' ', '011110 ', '- . . -'],
'31' => ['Э', 'Ю', ' ', ' ', '011111 ', '. . - . .'],
'32' => ['Ю', 'Я', ' ', ' ', '100000', '. . - -'],
'33' => ['Я', 'А', ' ', ' ', '100001', '. - . -'],
];
$result = 'Непонятно :(';
$header = ' № | А | Я | A | Z | Двоичн | Морзе';
if ($text == '$') {
$result = '';
foreach ($db as $key => $value) {
if ($key < 10) {
$result .= "0$key | $value[0] | $value[1] | $value[2] | $value[3] | $value[4] | $value[5]" . PHP_EOL;
} else {
$result .= "$key | $value[0] | $value[1] | $value[2] | $value[3] | $value[4] | $value[5]" . PHP_EOL;
}
}
} elseif (is_numeric($text) && (isset($db[$text]))) {
$value = $db[$text];
if ($text < 10) {
$result = "0$text | $value[0] | $value[1] | $value[2] | $value[3] | $value[4] | $value[5]" . PHP_EOL;
} else {
$result = "$text | $value[0] | $value[1] | $value[2] | $value[3] | $value[4] | $value[5]" . PHP_EOL;
}
} else {
foreach ($db as $key => $value) {
if (mb_strtolower($value[0], 'utf8') == mb_strtolower($text)) {
if ($key < 10) {
$result = "0$key | $value[0] | $value[1] | $value[2] | $value[3] | $value[4] | $value[5]" . PHP_EOL;
} else {
$result = "$key | $value[0] | $value[1] | $value[2] | $value[3] | $value[4] | $value[5]" . PHP_EOL;
}
}
if (strtolower($value[4]) == strtolower($text)) {
if ($key < 10) {
$result = "0$key | $value[0] | $value[1] | $value[2] | $value[3] | $value[4] | $value[5]" . PHP_EOL;
} else {
$result = "$key | $value[0] | $value[1] | $value[2] | $value[3] | $value[4] | $value[5]" . PHP_EOL;
}
}
}
}
if ($result) {
$telegram->getMethods()->sendMessage($message->getChat(), '```' . $header . PHP_EOL . $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 isMatch('/^\$.+?$/i'), $getDb)->
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();
}
}