diff --git a/LICENSE b/LICENSE index 9b95b43..818b767 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2022 Kiky Tokamuro (Daniil Archangelsky) +Copyright (c) 2022-2024 Kiky Tokamuro (Daniil Archangelsky) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index 07b9f56..19f7896 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,4 @@ Tiny framework for Yandex Alice ### Adding your own сommands 1. Add new command to `src/AliceFramework/Commands/` using examples `Example.php` and `RandomQuote.php` (`start` method must be present) -2. Add path to new command class in `src/AliceFramework/Commands/Common.php` - -### TODO -- Command arguments \ No newline at end of file +2. Add path to new command class in `src/AliceFramework/Commands/CommandList.php` diff --git a/composer.json b/composer.json index c843009..b0aecbf 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,7 @@ } ], "license": "MIT", - "version": "1.0.0", + "version": "2.0.0", "require": { "php": "^8.0.0" } diff --git a/index.php b/index.php index da381b7..9957a91 100644 --- a/index.php +++ b/index.php @@ -2,11 +2,7 @@ require __DIR__ . '/vendor/autoload.php'; -use AliceFramework\Core; - header('Content-Type: application/json'); -// Get request and start -$core = new Core(file_get_contents('php://input'), 'мой информер'); -echo $core->start(); - +// Get request and start app +echo (new AliceFramework\App(file_get_contents('php://input'), 'Моя автоматизация'))->start(); diff --git a/src/AliceFramework/App.php b/src/AliceFramework/App.php new file mode 100644 index 0000000..2627246 --- /dev/null +++ b/src/AliceFramework/App.php @@ -0,0 +1,64 @@ +name = strtolower($name); + $this->request = new Request($data); + } + + /** + * Start processing request data + * + * @return false|string + */ + public function start() : false|string + { + if (!$this->request->isValid()) + return Response::error('Wrong request'); + + $response = new Response( + $this->request->getSessionId(), + $this->request->getMessageId(), + $this->request->getUserId() + ); + + // Command includes skill name + if (str_contains($this->request->getCommand(), $this->name) || empty($this->request->getCommand())) + return $response->success('Hello i am ' . $this->name); + + // Check command + if (!CommandList::exists($this->request->getCommand())) + return $response->success('I do not understand'); + + // Command processing + try { + return $response->success( + (new CommandList::$commands[$this->request->getCommand()])->start() + ); + } + catch (\Exception $e) + { + return Response::error('Command error'); + } + } +} diff --git a/src/AliceFramework/Commands/Command.php b/src/AliceFramework/Commands/Command.php new file mode 100644 index 0000000..1caf0ea --- /dev/null +++ b/src/AliceFramework/Commands/Command.php @@ -0,0 +1,8 @@ + RandomQuote::class, + "пример" => Example::class + ]; + + /** + * Check if command exists + * + * @param string $command Command name + * @return bool + */ + public static function exists(string $command) : bool + { + return array_key_exists($command, CommandList::$commands); + } +} diff --git a/src/AliceFramework/Commands/Common.php b/src/AliceFramework/Commands/Common.php deleted file mode 100644 index ff31989..0000000 --- a/src/AliceFramework/Commands/Common.php +++ /dev/null @@ -1,14 +0,0 @@ - "\\AliceFramework\\Commands\\RandomQuote", - "пример" => "\\AliceFramework\\Commands\\Example" - ]; -} diff --git a/src/AliceFramework/Commands/Example.php b/src/AliceFramework/Commands/Example.php index cc59c16..cee57d9 100644 --- a/src/AliceFramework/Commands/Example.php +++ b/src/AliceFramework/Commands/Example.php @@ -3,17 +3,15 @@ namespace AliceFramework\Commands; /** - * Example class - Example command + * Example command */ -class Example +class Example implements Command { - public function __construct() { } - /** - * start - Start command + * Start command */ - public function start() + public function start() : string { - return "это пример"; + return "This is example"; } } diff --git a/src/AliceFramework/Commands/RandomQuote.php b/src/AliceFramework/Commands/RandomQuote.php index 5825fdc..7392c7b 100644 --- a/src/AliceFramework/Commands/RandomQuote.php +++ b/src/AliceFramework/Commands/RandomQuote.php @@ -3,10 +3,15 @@ namespace AliceFramework\Commands; /** - * RandomQuote class - Command for get random quote + * Command for get random quote */ -class RandomQuote +class RandomQuote implements Command { + /** + * @var string API url + */ + private string $url; + public function __construct() { $rand = random_int(0, 999999); @@ -14,12 +19,17 @@ public function __construct() } /** - * start - Start command + * Start command + * + * @return string */ - public function start() + public function start() : string { - $json = file_get_contents($this->url); - $decoded = json_decode($json, true); - return $decoded['quoteText'] . " - " . $decoded['quoteAuthor']; + $decoded = json_decode(file_get_contents($this->url), true); + + if (isset($decoded['quoteText'], $decoded['quoteAuthor'])) + return $decoded['quoteText'] . " - " . $decoded['quoteAuthor']; + + return "Random quote"; } } diff --git a/src/AliceFramework/Core.php b/src/AliceFramework/Core.php deleted file mode 100644 index 7c96b40..0000000 --- a/src/AliceFramework/Core.php +++ /dev/null @@ -1,111 +0,0 @@ -name = $name; - - // Checking request - if (!empty($data)) { - $this->data = json_decode($data, true); - - if ( - !isset($this->data['request'], - $this->data['request']['command'], - $this->data['session'], - $this->data['session']['session_id'], - $this->data['session']['message_id'], - $this->data['session']['user_id'] - ) - ) { - $this->result = json_encode([]); - } else { - $this->command = strtolower($this->data['request']['command']); - $this->session_id = $this->data['session']['session_id']; - $this->message_id = $this->data['session']['message_id']; - $this->user_id = $this->data['session']['user_id']; - } - } else { - $this->result = $this->returnError('Данные отсутствуют'); - } - } - - /** - * returnError - Generate error response - * - * @param string $text - */ - private function returnError($text) - { - return json_encode([ - 'version' => '1.0', - 'session' => 'Error', - 'response' => [ - 'text' => $text, - 'tts' => $text - ] - ]); - } - - /** - * returnResponse - Generate success response - * - * @param mixed $text - */ - private function returnResponse($text) - { - return json_encode([ - 'version' => '1.0', - 'session' => [ - 'session_id' => $this->session_id, - 'message_id' => $this->message_id, - 'user_id' => $this->user_id - ], - 'response' => [ - 'text' => $text, - 'tts' => $text, - 'buttons' => [], - 'end_session' => false - ] - ]); - } - - /** - * start - Start AliceFramework - */ - public function start() - { - // Return error - if (isset($this->result)) { - return $this->result; - } - - // Command includes skill name - if (strpos($this->command, $this->name) !== false) { - $this->result = $this->returnResponse('Привет я навык ' . $this->name); - return $this->result; - } - - // Command processing - if (array_key_exists($this->command, Common::$commands)) { - $cmd = new Common::$commands[$this->command]; - $this->result = $this->returnResponse($cmd->start()); - } else { - $this->result = $this->returnResponse('Я вас не понимаю, либо вы ничего не сказали'); - } - - return $this->result; - } -} diff --git a/src/AliceFramework/Request.php b/src/AliceFramework/Request.php new file mode 100644 index 0000000..bc57f9c --- /dev/null +++ b/src/AliceFramework/Request.php @@ -0,0 +1,124 @@ +isEmpty = false; + + // Decode request JSON + $requestData = json_decode($data, true); + + // Validate request + if ($this->isValidData($requestData)) + { + $this->isValid = true; + + // Set request params + $this->command = strtolower($requestData['request']['command']); + $this->sessionId = $requestData['session']['session_id']; + $this->messageId = $requestData['session']['message_id']; + $this->userId = $requestData['session']['user_id']; + } + } + + /** + * Validate request data + * + * @param mixed $requestData Request data + * @return bool + */ + private function isValidData(mixed $requestData) : bool + { + return isset( + $requestData['request'], + $requestData['request']['command'], + $requestData['session'], + $requestData['session']['session_id'], + $requestData['session']['message_id'], + $requestData['session']['user_id'] + ); + } + + /** + * Request is valid + * + * @return bool + */ + public function isValid() : bool + { + return $this->isValid && !$this->isEmpty; + } + + /** + * @return string Get request command + */ + public function getCommand(): string + { + return $this->command; + } + + /** + * @return string Get request session_id + */ + public function getSessionId(): string + { + return $this->sessionId; + } + + /** + * @return string Get request message_id + */ + public function getMessageId(): string + { + return $this->messageId; + } + + /** + * @return string Get request user_id + */ + public function getUserId(): string + { + return $this->userId; + } +} \ No newline at end of file diff --git a/src/AliceFramework/Response.php b/src/AliceFramework/Response.php new file mode 100644 index 0000000..54c1157 --- /dev/null +++ b/src/AliceFramework/Response.php @@ -0,0 +1,70 @@ +sessionId = $sessionId; + $this->messageId = $messageId; + $this->userId = $userId; + } + + /** + * Generate error JSON + * + * @param string $text Error message + * @return false|string + */ + public static function error(string $text) : false|string + { + return json_encode([ + 'version' => '1.0', + 'session' => 'Error', + 'response' => [ + 'text' => $text, + 'tts' => $text + ] + ]); + } + + /** + * Generate success JSON + * + * @param string $text Success message + * @return false|string + */ + public function success(string $text) : false|string + { + return json_encode([ + 'version' => '1.0', + 'session' => [ + 'session_id' => $this->sessionId, + 'message_id' => $this->messageId, + 'user_id' => $this->userId + ], + 'response' => [ + 'text' => $text, + 'tts' => $text, + 'buttons' => [], + 'end_session' => false + ] + ]); + } +} \ No newline at end of file