summaryrefslogtreecommitdiff
path: root/src/NeonXP/BotScript.php
blob: 3e94d1b31f15d14e228882b7e02e85808e31dc92 (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
<?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
        };
        $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();
    }
}