summaryrefslogtreecommitdiff
path: root/src/NeonXP/BotScript.php
blob: 54a6c90ee1a01ad6454ea8b7e117b20d7fdcbcb1 (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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php

namespace NeonXP;

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;

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());
            $link = $telegram->getCache()->fetch("link_{$message->getChat()->getId()}");
            $telegram->getMethods()->sendMessage($message->getChat(), 'Ок, записал: '.$link);

            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));
            $code = iconv('utf8', 'cp1251', $code);
            $result = false;
            try {
                $result = $dozorApi->sendCode($message->getChat(), $code);
            } catch (\Exception $e) {
                $telegram->getMethods()->sendMessage($message->getChat(), 'Ошибка сети: '.$e->getMessage(), true);
            }
            if ($result) {
                $telegram->getMethods()->sendMessage($message->getChat(), $result, true);
            }

            return State::INITIAL; //Set initial state
        };
        $geoCode = function (Message $message, Telegram $telegram) use ($dozorApi) {
            $coords = explode(',', trim($message->getText()));
            $location = new Location();
            $location->setLatitude($coords[0]);
            $location->setLongitude($coords[1]);
            $telegram->getMethods()->sendLocation($message->getChat(), $location);

            return State::INITIAL; //Set initial state
        };
        $reverseGeoCode = function (Message $message, Telegram $telegram) use ($dozorApi) {

            $telegram->getMethods()->sendMessage($message->getChat(),
                sprintf('%s, %s', $message->getLocation()->getLatitude(), $message->getLocation()->getLongitude())
            );

            return State::INITIAL; //Set initial state
        };

        $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', '- - . .'],
                '10' => ['И', 'Ц', 'J', 'Q', '01000', '. .'],
                '11' => ['Й', 'Х', 'K', 'P', '01001', '. - - -'],
                '12' => ['К', 'Ф', 'L', 'O', '01010', '- . -'],
                '13' => ['Л', 'У', 'M', 'N', '01011', '. - . .'],
                '14' => ['М', 'Т', 'N', 'M', '01100', '- -'],
                '15' => ['Н', 'С', 'O', 'L', '01101', '- .'],
                '16' => ['О', 'Р', 'P', 'K', '01110', '- - -'],
                '17' => ['П', 'П', 'Q', 'J', '01111', '. - - .'],
                '18' => ['Р', 'О', 'R', 'I', '10000', '. - .'],
                '19' => ['С', 'Н', 'S', 'H', '10001', '. . .'],
                '20' => ['Т', 'М', 'T', 'G', '10010', '-'],
                '21' => ['У', 'Л', 'U', 'F', '10011', '. . -'],
                '22' => ['Ф', 'К', 'V', 'E', '10100', '. . - .'],
                '23' => ['Х', 'Й', 'W', 'D', '10101', '. . . .'],
                '24' => ['Ц', 'И', 'X', 'C', '10110', '- . - .'],
                '25' => ['Ч', 'З', 'Y', 'B', '10111', '- - - .'],
                '26' => ['Ш', 'Ж', 'Z', 'A', '11000', '- - - -'],
                '27' => ['Щ', 'Ё', ' ', ' ', '11001', '- - . -'],
                '28' => ['Ъ', 'Е', ' ', ' ', '11010', '- - . - -'],
                '29' => ['Ы', 'Д', ' ', ' ', '11011', '- . - -'],
                '30' => ['Ь', 'Г', ' ', ' ', '11100', '- . . -'],
                '31' => ['Э', 'В', ' ', ' ', '11101', '. . - . .'],
                '32' => ['Ю', 'Б', ' ', ' ', '11110', '. . - -'],
                '33' => ['Я', 'А', ' ', ' ', '11111', '. - . -'],
            ];
            $result = 'Непонятно :(';
            $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;
                    } 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 {
                $result = '';
                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;
                            } else {
                                $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;
                            } 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 isMatch('/^\d\d\.\d+\s?\,\s?\d\d\.\d+\s?$/i'), $geoCode)->
        action(new isLocation(), $reverseGeoCode)->
        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();
    }
}