diff options
Diffstat (limited to 'src/NeonXP')
-rw-r--r-- | src/NeonXP/BotScript.php | 67 | ||||
-rw-r--r-- | src/NeonXP/DozorApi.php | 47 |
2 files changed, 60 insertions, 54 deletions
diff --git a/src/NeonXP/BotScript.php b/src/NeonXP/BotScript.php index 9309b5a..12313d2 100644 --- a/src/NeonXP/BotScript.php +++ b/src/NeonXP/BotScript.php @@ -2,16 +2,15 @@ namespace NeonXP; -use Doctrine\Common\Cache\CacheProvider; use NeonXP\TelegramApi\Types\Location; -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\isLocation; -use \NeonXP\TelegramApi\Script\Conditions\isMatch; -use \NeonXP\TelegramApi\Script\Conditions\isAnyText; -use \NeonXP\TelegramApi\Telegram; +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\isLocation; +use NeonXP\TelegramApi\Script\Conditions\isMatch; +use NeonXP\TelegramApi\Script\Conditions\isAnyText; +use NeonXP\TelegramApi\Telegram; class BotScript { @@ -19,7 +18,8 @@ class BotScript { $saveLink = function (Message $message, Telegram $telegram) { $telegram->getCache()->save("link_{$message->getChat()->getId()}", $message->getText()); - $telegram->getMethods()->sendMessage($message->getChat(), 'Ок, записал: ' . $message->getText()); + $link = $telegram->getCache()->fetch("link_{$message->getChat()->getId()}"); + $telegram->getMethods()->sendMessage($message->getChat(), 'Ок, записал: '.$link); return State::INITIAL; }; @@ -38,7 +38,7 @@ class BotScript try { $result = $dozorApi->sendCode($message->getChat(), $code); } catch (\Exception $e) { - $telegram->getMethods()->sendMessage($message->getChat(), 'Ошибка сети', true); + $telegram->getMethods()->sendMessage($message->getChat(), 'Ошибка сети: '.$e->getMessage(), true); } if ($result) { $telegram->getMethods()->sendMessage($message->getChat(), $result, true); @@ -67,15 +67,15 @@ class BotScript $getDb = function (Message $message, Telegram $telegram) { $text = trim(substr($message->getText(), 1)); $db = [ - '1' => ['А', 'Я', 'A', 'Z', '00000', '. -'], - '2' => ['Б', 'Ю', 'B', 'Y', '00001', '- . . .'], - '3' => ['В', 'Э', 'C', 'X', '00010', '. - -'], - '4' => ['Г', 'Ь', 'D', 'W', '00011', '- - .'], - '5' => ['Д', 'Ы', 'E', 'V', '00100', '- . .'], - '6' => ['Е', 'Ъ', 'F', 'U', '00101', '.'], - '7' => ['Ё', 'Щ', 'G', 'T', '00101', '.'], - '8' => ['Ж', 'Ш', 'H', 'S', '00110', '. . . -'], - '9' => ['З', 'Ч', 'I', 'R', '00111', '- - . .'], + '1' => ['А', 'Я', 'A', 'Z', '00000', '. -'], + '2' => ['Б', 'Ю', 'B', 'Y', '00001', '- . . .'], + '3' => ['В', 'Э', 'C', 'X', '00010', '. - -'], + '4' => ['Г', 'Ь', 'D', 'W', '00011', '- - .'], + '5' => ['Д', 'Ы', 'E', 'V', '00100', '- . .'], + '6' => ['Е', 'Ъ', 'F', 'U', '00101', '.'], + '7' => ['Ё', 'Щ', 'G', 'T', '00101', '.'], + '8' => ['Ж', 'Ш', 'H', 'S', '00110', '. . . -'], + '9' => ['З', 'Ч', 'I', 'R', '00111', '- - . .'], '10' => ['И', 'Ц', 'J', 'Q', '01000', '. .'], '11' => ['Й', 'Х', 'K', 'P', '01001', '. - - -'], '12' => ['К', 'Ф', 'L', 'O', '01010', '- . -'], @@ -102,48 +102,49 @@ class BotScript '33' => ['Я', 'А', ' ', ' ', '11111', '. - . -'], ]; $result = 'Непонятно :('; - $header = ' № | А | Я | A | Z | Двоичн | Морзе ' . PHP_EOL . '____________________________________'; + $header = ' № | А | Я | A | Z | Двоич | Морзе '.PHP_EOL. + '___________________________________'; 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; + $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; + $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; + $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; + $result = "$text | $value[0] | $value[1] | $value[2] | $value[3] | $value[4] | $value[5]".PHP_EOL; } } else { $result = ''; - for ($i = 0; $i < mb_strlen($text, 'utf8'); $i++) { + for ($i = 0; $i < mb_strlen($text, 'utf8'); ++$i) { $char = mb_substr($text, $i, 1, 'utf8'); foreach ($db as $key => $value) { if (mb_strtolower($value[0], 'utf8') == mb_strtolower($char)) { if ($key < 10) { - $result .= "0$key | $value[0] | $value[1] | $value[2] | $value[3] | $value[4] | $value[5]" . PHP_EOL; + $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; + $result .= "$key | $value[0] | $value[1] | $value[2] | $value[3] | $value[4] | $value[5]".PHP_EOL; } } if (strtolower($value[2]) == strtolower($char)) { if ($key < 10) { - $result .= "0$key | $value[0] | $value[1] | $value[2] | $value[3] | $value[4] | $value[5]" . PHP_EOL; + $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; + $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); + $telegram->getMethods()->sendMessage($message->getChat(), '```'.$header.PHP_EOL.$result.'```', true); } return State::INITIAL; //Set initial state @@ -161,11 +162,11 @@ class BotScript action(new isText('/link'), $showLink)-> action(new isText('/setlink'), [ 'Пришлите ссылку с пином /cancel', - 'STATE_SET_LINK' + 'STATE_SET_LINK', ])-> action(new isText('/pause'), [ 'Замер. /resume для возврата', - 'STATE_PAUSE' + 'STATE_PAUSE', ])-> action(new isMatch('/^\!.+?$/i'), $sendCode)-> action(new isMatch('/^\?.+?$/i'), $getDb)-> diff --git a/src/NeonXP/DozorApi.php b/src/NeonXP/DozorApi.php index da724a9..4865e96 100644 --- a/src/NeonXP/DozorApi.php +++ b/src/NeonXP/DozorApi.php @@ -2,16 +2,15 @@ /** * @author Alexander "NeonXP" Kiryukhin */ + namespace NeonXP; + use Doctrine\Common\Cache\CacheProvider; use NeonXP\TelegramApi\Types\Chat; -use Symfony\Component\CssSelector\Node\AbstractNode; use Symfony\Component\DomCrawler\Crawler; - /** - * Class DozorApi - * @package NeonXP + * Class DozorApi. */ class DozorApi { @@ -22,6 +21,7 @@ class DozorApi /** * DozorApi constructor. + * * @param CacheProvider $cache */ public function __construct(CacheProvider $cache) @@ -32,31 +32,35 @@ class DozorApi public function sendCode(Chat $chat, $code) { $params = [ - "log" => 'on', - "mes" => '', - "legend" => 'on', - "nostat" => '', - "notext" => '', - "refresh" => '0', - "bonus" => '', - "kladMap" => '', - "notags" => '', - "cod" => $code, - "action" => 'entcod', + 'log' => 'on', + 'mes' => '', + 'legend' => 'on', + 'nostat' => '', + 'notext' => '', + 'refresh' => '0', + 'bonus' => '', + 'kladMap' => '', + 'notags' => '', + 'cod' => $code, + 'action' => 'entcod', ]; $data = http_build_query($params); $contextData = [ 'http' => [ 'method' => 'POST', - 'header' => 'Content-type: application/x-www-form-urlencoded', - 'content' => $data - ] + 'header' => 'Content-type: application/x-www-form-urlencoded', + 'content' => $data, + ], ]; $context = stream_context_create($contextData); + if (!$this->getLink($chat)) { + throw new \Exception('Не задана ссылка'); + } $result = file_get_contents($this->getLink($chat), false, $context); $contextData['result'] = $result; - file_put_contents(getenv('OPENSHIFT_DATA_DIR') . 'log.txt', serialize($contextData)); + file_put_contents(getenv('OPENSHIFT_DATA_DIR').'log.txt', serialize($contextData)); + return $this->parseResult($result); } @@ -66,7 +70,7 @@ class DozorApi $answer = [ $this->parseCodeEnter($html), - $this->parseTask($crawler) + $this->parseTask($crawler), ]; $answer = array_merge($answer, $this->parseCodes($crawler)); @@ -78,9 +82,10 @@ class DozorApi private function parseCodeEnter($html) { $html = iconv('cp1251', 'utf8', $html); - $regex = '/\<\!\-\-errorText\-\-\>\<p\>\<strong\>(.+?)\<\/strong\>\<\/p\>\<\!\-\-errorTextEnd\-\-\>'; + $regex = '/\<\!\-\-errorText\-\-\>\<p\>\<strong\>(.+?)\<\/strong\>\<\/p\>\<\!\-\-errorTextEnd\-\-\>/i'; $result = []; preg_match($regex, $html, $result); + return $result[1]; } |