summaryrefslogtreecommitdiff
path: root/src/NeonXP/BotScript.php
blob: ac0168109a02b026dd9669a82a93ac87c018d47e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?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', '1', '. -'],
                '2' => ['Б', 'Б', 'Ю', 'Ю', 'B', 'Y', '10', '- . . .'],
                '3' => ['В', 'В', 'Э', 'Э', 'C', 'X', '11', '. - -'],
                '4' => ['Г', 'Г', 'Ь', 'Ь', 'D', 'W', '100', '- - .'],
                '5' => ['Д', 'Д', 'Ы', 'Ы', 'E', 'V', '101', '- . .'],
                '6' => ['Е', 'Е', 'Ъ', 'Ъ', 'F', 'U', '110', '.'],
                '7' => ['Ё', 'Ж', 'Щ', 'Щ', 'G', 'T', '111', '.'],
                '8' => ['Ж', 'З', 'Ш', 'Ш', 'H', 'S', '1000', '. . . -'],
                '9' => ['З', 'И', 'Ч', 'Ч', 'I', 'R', '1001', '- - . .'],
                '10' => ['И', 'Й', 'Ц', 'Ц', 'J', 'Q', '1010', '. .'],
                '11' => ['Й', 'К', 'Х', 'Х', 'K', 'P', '1011', '. - - -'],
                '12' => ['К', 'Л', 'Ф', 'Ф', 'L', 'O', '1100', '- . -'],
                '13' => ['Л', 'М', 'У', 'У', 'M', 'N', '1101', '. - . .'],
                '14' => ['М', 'Н', 'Т', 'Т', 'N', 'M', '1110', '- -'],
                '15' => ['Н', 'О', 'С', 'С', 'O', 'L', '1111', '- .'],
                '16' => ['О', 'П', 'Р', 'Р', 'P', 'K', '10000', '- - -'],
                '17' => ['П', 'Р', 'П', 'П', 'Q', 'J', '10001', '. - - .'],
                '18' => ['Р', 'С', 'О', 'О', 'R', 'I', '10010', '. - .'],
                '19' => ['С', 'Т', 'Н', 'Н', 'S', 'H', '10011', '. . .'],
                '20' => ['Т', 'У', 'М', 'М', 'T', 'G', '10100', '-'],
                '21' => ['У', 'Ф', 'Л', 'Л', 'U', 'F', '10101', '. . -'],
                '22' => ['Ф', 'Х', 'К', 'К', 'V', 'E', '10110', '. . - .'],
                '23' => ['Х', 'Ц', 'Й', 'Й', 'W', 'D', '10111', '. . . .'],
                '24' => ['Ц', 'Ч', 'И', 'И', 'X', 'C', '11000', '- . - .'],
                '25' => ['Ч', 'Ш', 'З', 'З', 'Y', 'B', '11001', '- - - .'],
                '26' => ['Ш', 'Щ', 'Ж', 'Ж', 'Z', 'A', '11010', '- - - -'],
                '27' => ['Щ', 'Ъ', 'Ё', 'Е', ' ', ' ', '11011', '- - . -'],
                '28' => ['Ъ', 'Ы', 'Е', 'Д', ' ', ' ', '11100', '- - . - -'],
                '29' => ['Ы', 'Ь', 'Д', 'Г', ' ', ' ', '11101', '- . - -'],
                '30' => ['Ь', 'Э', 'Г', 'В', ' ', ' ', '11110 ', '- . . -'],
                '31' => ['Э', 'Ю', 'В', 'Б', ' ', ' ', '11111 ', '. . - . .'],
                '32' => ['Ю', 'Я', 'Б', 'А', ' ', ' ', '100000', '. . - -'],
                '33' => ['Я', 'А', ' ', ' ', ' ', ' ', '100001', '. - . -'],
            ];
            $result = 'Непонятно :(';
            if ($text == '$') {
                $result = '';
                foreach ($db as $key => $value) {
                    $result .= sprintf('*%d* АлфЁ *%s* Алф *%s* ОбрЁ *%s* Обр *%s* Alph *%s* RvrsAlph *%s* Двоич *%s* Морз *%s*',
                        $key, $value[0],$value[1],$value[2],$value[3], $value[4],$value[5], $value[6],$value[7]
                    ) . PHP_EOL;
                }
            } elseif (is_numeric($text) && (isset($db[$text]))) {
                $result = sprintf('*%d* АлфЁ *%s* Алф *%s* ОбрЁ *%s* Обр *%s* Alph *%s* RvrsAlph *%s* Двоич *%s* Морз *%s*',
                    $text, $db[$text][0],$db[$text][1],$db[$text][2],$db[$text][3], $db[$text][4],$db[$text][5], $db[$text][6],$db[$text][7]
                );
            } else {
                foreach ($db as $key => $value) {
                    if (mb_strtolower($value[0], 'utf8') == mb_strtolower($text)) {
                        $result = sprintf('*%d* АлфЁ *%s* Алф *%s* ОбрЁ *%s* Обр *%s* Alph *%s* RvrsAlph *%s* Двоич *%s* Морз *%s*',
                            $key, $value[0],$value[1],$value[2],$value[3], $value[4],$value[5], $value[6],$value[7]
                        );
                    }

                    if (strtolower($value[4]) == strtolower($text)) {
                        $result = sprintf('*%d* АлфЁ *%s* Алф *%s* ОбрЁ *%s* Обр *%s* Alph *%s* RvrsAlph *%s* Двоич *%s* Морз *%s*',
                            $key, $value[0],$value[1],$value[2],$value[3], $value[4],$value[5], $value[6],$value[7]
                        );
                    }
                }
            }
            if ($result) {
                $telegram->getMethods()->sendMessage($message->getChat(), '```' . PHP_EOL . $result . PHP_EOL . '```', 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();
    }
}