├── .gitignore ├── CHANGELOG.md ├── LICENSE ├── README.md ├── composer.json └── src └── Dialogs └── Webhook ├── Request ├── DTO │ ├── Meta.php │ ├── Phrase.php │ ├── Request.php │ └── Session.php ├── Fabric.php └── Formatters │ └── Formatter.php └── Response ├── DTO ├── Buttons │ ├── Button.php │ └── Collection.php ├── Response.php └── Session.php ├── Fabric.php └── Formatters └── Formatter.php /.gitignore: -------------------------------------------------------------------------------- 1 | /.idea* 2 | vendor 3 | composer.phar 4 | composer.lock -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # yandex-dialogs-php-sdk change log 2 | ## 0.1.0 (14.03.2018) 3 | * Initial release -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Maxim Mesilov 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # yandex-dialogs-php-sdk 2 | Репозиторий PHP-библиотеки для облегчения работы с диалогами 3 | 4 | ## Пример использования 5 | ```php 6 | // получаем входящий API-запрос 7 | $apiRequestArray = json_decode(trim(file_get_contents('php://input')), true); 8 | 9 | 10 | $dialogRequest = Yandex\Dialogs\Webhook\Request\Fabric::initFromArray($apiRequestArray); 11 | $responseFabric = new Yandex\Dialogs\Webhook\Response\Fabric($dialogRequest); 12 | 13 | $button1 = new Yandex\Dialogs\Webhook\Response\DTO\Buttons\Button(); 14 | $button1 15 | ->setTitle('Кнопка1') 16 | ->setUrl('https://ya.ru'); 17 | 18 | $button2 = new Yandex\Dialogs\Webhook\Response\DTO\Buttons\Button(); 19 | $button2 20 | ->setTitle('Кнопка2'); 21 | 22 | $response = $responseFabric 23 | ->setText('Привет') 24 | ->setTts('Привет') 25 | ->addButton($button1) 26 | ->addButton($button2) 27 | ->buildResponse(); 28 | 29 | 30 | header('Content-Type: application/json'); 31 | print(json_encode(Yandex\Dialogs\Webhook\Response\Formatters\Formatter::toArray($response), JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT)); 32 | ``` -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "mesilov/yandex-dialogs-php-sdk", 3 | "description": "Yandex dialogs API PHP-wrapper", 4 | "keywords": [ 5 | "Yandex", 6 | "PHP", 7 | "Dialogs", 8 | "API" 9 | ], 10 | "type": "library", 11 | "homepage": "https://github.com/mesilov/yandex-dialogs-php-sdk", 12 | "license": "MIT", 13 | "authors": [ 14 | { 15 | "name": "Maxim Mesilov", 16 | "homepage": "https://github.com/mesilov/" 17 | } 18 | ], 19 | "require": { 20 | "php": ">=7.1", 21 | "ext-json": "*" 22 | }, 23 | "require-dev": { 24 | "jakub-onderka/php-parallel-lint": "0.9", 25 | "jakub-onderka/php-console-highlighter": "~0.3", 26 | "phpunit/phpunit": "~4.8", 27 | "phpunit/phpunit-mock-objects": "2.3.0" 28 | }, 29 | "autoload": { 30 | "psr-4": { 31 | "Yandex\\Dialogs\\": "src/Dialogs" 32 | } 33 | }, 34 | "scripts": { 35 | "test": [ 36 | "parallel-lint . --exclude vendor --no-colors", 37 | "phpunit --colors=always --verbose" 38 | ] 39 | } 40 | } -------------------------------------------------------------------------------- /src/Dialogs/Webhook/Request/DTO/Meta.php: -------------------------------------------------------------------------------- 1 | locale = $locale; 34 | 35 | return $this; 36 | } 37 | 38 | /** 39 | * @param \DateTimeZone $timezone 40 | * 41 | * @return Meta 42 | */ 43 | public function setTimezone(\DateTimeZone $timezone): Meta 44 | { 45 | $this->timezone = $timezone; 46 | 47 | return $this; 48 | } 49 | 50 | /** 51 | * @param string $clientId 52 | * 53 | * @return Meta 54 | */ 55 | public function setClientId(string $clientId): Meta 56 | { 57 | $this->clientId = $clientId; 58 | 59 | return $this; 60 | } 61 | 62 | /** 63 | * @return string 64 | */ 65 | public function getLocale(): string 66 | { 67 | return $this->locale; 68 | } 69 | 70 | /** 71 | * @return \DateTimeZone 72 | */ 73 | public function getTimezone(): \DateTimeZone 74 | { 75 | return $this->timezone; 76 | } 77 | 78 | /** 79 | * @return string 80 | */ 81 | public function getClientId(): string 82 | { 83 | return $this->clientId; 84 | } 85 | } -------------------------------------------------------------------------------- /src/Dialogs/Webhook/Request/DTO/Phrase.php: -------------------------------------------------------------------------------- 1 | type; 43 | } 44 | 45 | /** 46 | * @param string $type 47 | * 48 | * @return Phrase 49 | */ 50 | public function setType(string $type): Phrase 51 | { 52 | $this->type = $type; 53 | 54 | return $this; 55 | } 56 | 57 | /** 58 | * @return string 59 | */ 60 | public function getCommand(): string 61 | { 62 | return $this->command; 63 | } 64 | 65 | /** 66 | * @param string $command 67 | * 68 | * @return Phrase 69 | */ 70 | public function setCommand(string $command): Phrase 71 | { 72 | $this->command = $command; 73 | 74 | return $this; 75 | } 76 | 77 | /** 78 | * @return string 79 | */ 80 | public function getOriginalUtterance(): string 81 | { 82 | return $this->originalUtterance; 83 | } 84 | 85 | /** 86 | * @param string $originalUtterance 87 | * 88 | * @return Phrase 89 | */ 90 | public function setOriginalUtterance(string $originalUtterance): Phrase 91 | { 92 | $this->originalUtterance = $originalUtterance; 93 | 94 | return $this; 95 | } 96 | 97 | /** 98 | * @return string 99 | */ 100 | public function getPayload(): string 101 | { 102 | return $this->payload; 103 | } 104 | 105 | /** 106 | * @param string $payload 107 | * 108 | * @return Phrase 109 | */ 110 | public function setPayload(string $payload): Phrase 111 | { 112 | $this->payload = $payload; 113 | 114 | return $this; 115 | } 116 | 117 | /** 118 | * @return array 119 | */ 120 | public function getMarkup(): array 121 | { 122 | return $this->markup; 123 | } 124 | 125 | /** 126 | * @param array $markup 127 | * 128 | * @return Phrase 129 | */ 130 | public function setMarkup(array $markup): Phrase 131 | { 132 | $this->markup = $markup; 133 | 134 | return $this; 135 | } 136 | } -------------------------------------------------------------------------------- /src/Dialogs/Webhook/Request/DTO/Request.php: -------------------------------------------------------------------------------- 1 | version; 36 | } 37 | 38 | /** 39 | * @param string $version 40 | * 41 | * @return Request 42 | */ 43 | public function setVersion(string $version): Request 44 | { 45 | $this->version = $version; 46 | 47 | return $this; 48 | } 49 | 50 | /** 51 | * @return Meta 52 | */ 53 | public function getMeta(): Meta 54 | { 55 | return $this->meta; 56 | } 57 | 58 | /** 59 | * @param Meta $meta 60 | * 61 | * @return Request 62 | */ 63 | public function setMeta(Meta $meta): Request 64 | { 65 | $this->meta = $meta; 66 | 67 | return $this; 68 | } 69 | 70 | /** 71 | * @return Phrase 72 | */ 73 | public function getPhrase(): Phrase 74 | { 75 | return $this->phrase; 76 | } 77 | 78 | /** 79 | * @param Phrase $phrase 80 | * 81 | * @return Request 82 | */ 83 | public function setPhrase(Phrase $phrase): Request 84 | { 85 | $this->phrase = $phrase; 86 | 87 | return $this; 88 | } 89 | 90 | /** 91 | * @return Session 92 | */ 93 | public function getSession(): Session 94 | { 95 | return $this->session; 96 | } 97 | 98 | /** 99 | * @param Session $session 100 | * 101 | * @return Request 102 | */ 103 | public function setSession(Session $session): Request 104 | { 105 | $this->session = $session; 106 | 107 | return $this; 108 | } 109 | } -------------------------------------------------------------------------------- /src/Dialogs/Webhook/Request/DTO/Session.php: -------------------------------------------------------------------------------- 1 | isNew = $isNew; 44 | 45 | return $this; 46 | } 47 | 48 | /** 49 | * @param string $sessionId 50 | * 51 | * @return Session 52 | */ 53 | public function setSessionId(string $sessionId): Session 54 | { 55 | $this->sessionId = $sessionId; 56 | 57 | return $this; 58 | } 59 | 60 | /** 61 | * @param int $messageId 62 | * 63 | * @return Session 64 | */ 65 | public function setMessageId(int $messageId): Session 66 | { 67 | $this->messageId = $messageId; 68 | 69 | return $this; 70 | } 71 | 72 | /** 73 | * @param string $skillId 74 | * 75 | * @return Session 76 | */ 77 | public function setSkillId(string $skillId): Session 78 | { 79 | $this->skillId = $skillId; 80 | 81 | return $this; 82 | } 83 | 84 | /** 85 | * @param string $userId 86 | * 87 | * @return Session 88 | */ 89 | public function setUserId(string $userId): Session 90 | { 91 | $this->userId = $userId; 92 | 93 | return $this; 94 | } 95 | 96 | /** 97 | * @return bool 98 | */ 99 | public function isNew(): bool 100 | { 101 | return $this->isNew; 102 | } 103 | 104 | /** 105 | * @return string 106 | */ 107 | public function getSessionId(): string 108 | { 109 | return $this->sessionId; 110 | } 111 | 112 | /** 113 | * @return int 114 | */ 115 | public function getMessageId(): int 116 | { 117 | return $this->messageId; 118 | } 119 | 120 | /** 121 | * @return string 122 | */ 123 | public function getSkillId(): string 124 | { 125 | return $this->skillId; 126 | } 127 | 128 | /** 129 | * @return string 130 | */ 131 | public function getUserId(): string 132 | { 133 | return $this->userId; 134 | } 135 | } -------------------------------------------------------------------------------- /src/Dialogs/Webhook/Request/Fabric.php: -------------------------------------------------------------------------------- 1 | setClientId($arRequest['meta']['client_id']) 26 | ->setLocale($arRequest['meta']['locale']) 27 | ->setTimezone(new \DateTimeZone($arRequest['meta']['timezone'])); 28 | $phrase = new Dialogs\Webhook\Request\DTO\Phrase(); 29 | $phrase 30 | ->setCommand($arRequest['request']['command']) 31 | ->setOriginalUtterance($arRequest['request']['original_utterance']) 32 | ->setType($arRequest['request']['type']); 33 | 34 | $session = new Dialogs\Webhook\Request\DTO\Session(); 35 | $session 36 | ->setMessageId($arRequest['session']['message_id']) 37 | ->setIsNew($arRequest['session']['new']) 38 | ->setSessionId($arRequest['session']['session_id']) 39 | ->setSkillId($arRequest['session']['skill_id']) 40 | ->setUserId($arRequest['session']['user_id']); 41 | 42 | $request 43 | ->setVersion($arRequest['version']) 44 | ->setMeta($meta) 45 | ->setPhrase($phrase) 46 | ->setSession($session); 47 | 48 | return $request; 49 | } 50 | } -------------------------------------------------------------------------------- /src/Dialogs/Webhook/Request/Formatters/Formatter.php: -------------------------------------------------------------------------------- 1 | [ 24 | 'client_id' => $request->getMeta()->getClientId(), 25 | 'locale' => $request->getMeta()->getLocale(), 26 | 'timezone' => $request->getMeta()->getTimezone()->getName(), 27 | ], 28 | 'request' => [ 29 | 'command' => $request->getPhrase()->getCommand(), 30 | 'original_utterance' => $request->getPhrase()->getOriginalUtterance(), 31 | 'type' => $request->getPhrase()->getType(), 32 | ], 33 | 'session' => [ 34 | 'message_id' => $request->getSession()->getMessageId(), 35 | 'new' => $request->getSession()->isNew(), 36 | 'session_id' => $request->getSession()->getSessionId(), 37 | 'skill_id' => $request->getSession()->getSkillId(), 38 | 'user_id' => $request->getSession()->getUserId(), 39 | ], 40 | ]; 41 | } 42 | } -------------------------------------------------------------------------------- /src/Dialogs/Webhook/Response/DTO/Buttons/Button.php: -------------------------------------------------------------------------------- 1 | isHide = false; 42 | } 43 | 44 | /** 45 | * @return string 46 | */ 47 | public function getTitle(): string 48 | { 49 | return $this->title; 50 | } 51 | 52 | /** 53 | * @param string $title 54 | * 55 | * @return Button 56 | */ 57 | public function setTitle(string $title): Button 58 | { 59 | $this->title = $title; 60 | 61 | return $this; 62 | } 63 | 64 | /** 65 | * @return string|null 66 | */ 67 | public function getPayload(): ?string 68 | { 69 | return $this->payload; 70 | } 71 | 72 | /** 73 | * @param string $payload 74 | * 75 | * @return Button 76 | */ 77 | public function setPayload(string $payload): Button 78 | { 79 | $this->payload = $payload; 80 | 81 | return $this; 82 | } 83 | 84 | /** 85 | * @return string|null 86 | */ 87 | public function getUrl(): ?string 88 | { 89 | return $this->url; 90 | } 91 | 92 | /** 93 | * @param string $url 94 | * 95 | * @return Button 96 | */ 97 | public function setUrl(string $url): Button 98 | { 99 | $this->url = $url; 100 | 101 | return $this; 102 | } 103 | 104 | /** 105 | * @return bool 106 | */ 107 | public function isHide(): bool 108 | { 109 | return $this->isHide; 110 | } 111 | 112 | /** 113 | * @param bool $isHide 114 | * 115 | * @return Button 116 | */ 117 | public function setIsHide(bool $isHide): Button 118 | { 119 | $this->isHide = $isHide; 120 | 121 | return $this; 122 | } 123 | } -------------------------------------------------------------------------------- /src/Dialogs/Webhook/Response/DTO/Buttons/Collection.php: -------------------------------------------------------------------------------- 1 | buttons = new Collection(); 55 | } 56 | 57 | /** 58 | * @return string 59 | */ 60 | public function getText(): string 61 | { 62 | return $this->text; 63 | } 64 | 65 | /** 66 | * @param string $text 67 | * 68 | * @return Response 69 | */ 70 | public function setText(string $text): Response 71 | { 72 | $this->text = $text; 73 | 74 | return $this; 75 | } 76 | 77 | /** 78 | * @return string 79 | */ 80 | public function getTts(): string 81 | { 82 | return $this->tts; 83 | } 84 | 85 | /** 86 | * @param string $tts 87 | * 88 | * @return Response 89 | */ 90 | public function setTts(string $tts): Response 91 | { 92 | $this->tts = $tts; 93 | 94 | return $this; 95 | } 96 | 97 | /** 98 | * @return bool 99 | */ 100 | public function isEndSession(): bool 101 | { 102 | return $this->isEndSession; 103 | } 104 | 105 | /** 106 | * @param bool $isEndSession 107 | * 108 | * @return Response 109 | */ 110 | public function setIsEndSession(bool $isEndSession): Response 111 | { 112 | $this->isEndSession = $isEndSession; 113 | 114 | return $this; 115 | } 116 | 117 | /** 118 | * @return Session 119 | */ 120 | public function getSession(): Session 121 | { 122 | return $this->session; 123 | } 124 | 125 | /** 126 | * @param Session $session 127 | * 128 | * @return Response 129 | */ 130 | public function setSession(Session $session): Response 131 | { 132 | $this->session = $session; 133 | 134 | return $this; 135 | } 136 | 137 | /** 138 | * @return string 139 | */ 140 | public function getVersion(): string 141 | { 142 | return $this->version; 143 | } 144 | 145 | /** 146 | * @param string $version 147 | * 148 | * @return Response 149 | */ 150 | public function setVersion(string $version): Response 151 | { 152 | $this->version = $version; 153 | 154 | return $this; 155 | } 156 | 157 | /** 158 | * @return Collection 159 | */ 160 | public function getButtons(): Collection 161 | { 162 | return $this->buttons; 163 | } 164 | 165 | /** 166 | * @param Collection $buttons 167 | * 168 | * @return Response 169 | */ 170 | public function setButtons(Collection $buttons): Response 171 | { 172 | $this->buttons = $buttons; 173 | 174 | return $this; 175 | } 176 | } -------------------------------------------------------------------------------- /src/Dialogs/Webhook/Response/DTO/Session.php: -------------------------------------------------------------------------------- 1 | sessionId; 32 | } 33 | 34 | /** 35 | * @param string $sessionId 36 | * 37 | * @return Session 38 | */ 39 | public function setSessionId(string $sessionId): Session 40 | { 41 | $this->sessionId = $sessionId; 42 | 43 | return $this; 44 | } 45 | 46 | /** 47 | * @return int 48 | */ 49 | public function getMessageId(): int 50 | { 51 | return $this->messageId; 52 | } 53 | 54 | /** 55 | * @param int $messageId 56 | * 57 | * @return Session 58 | */ 59 | public function setMessageId(int $messageId): Session 60 | { 61 | $this->messageId = $messageId; 62 | 63 | return $this; 64 | } 65 | 66 | /** 67 | * @return string 68 | */ 69 | public function getUserId(): string 70 | { 71 | return $this->userId; 72 | } 73 | 74 | /** 75 | * @param string $userId 76 | * 77 | * @return Session 78 | */ 79 | public function setUserId(string $userId): Session 80 | { 81 | $this->userId = $userId; 82 | 83 | return $this; 84 | } 85 | } -------------------------------------------------------------------------------- /src/Dialogs/Webhook/Response/Fabric.php: -------------------------------------------------------------------------------- 1 | request = $request; 56 | $this->isEndSession = false; 57 | $this->tts = ''; 58 | $this->buttons = new Collection(); 59 | } 60 | 61 | /** 62 | * @param string $text Текст, который следует показать пользователю, максимум 1024 символа. Обязательное свойство. 63 | * 64 | * @return Fabric 65 | */ 66 | public function setText(string $text): self 67 | { 68 | $this->text = $text; 69 | 70 | return $this; 71 | } 72 | 73 | /** 74 | * @param string $tts Ответ в формате TTS (text-to-speech), максимум 1024 символа. 75 | * 76 | * @return Fabric 77 | */ 78 | public function setTts(string $tts): self 79 | { 80 | $this->tts = $tts; 81 | 82 | return $this; 83 | } 84 | 85 | /** 86 | * Признак конца разговора. Обязательное свойство. 87 | * 88 | * @return Fabric 89 | */ 90 | public function endSession(): self 91 | { 92 | $this->isEndSession = true; 93 | 94 | return $this; 95 | } 96 | 97 | /** 98 | * добавить кнопку 99 | * 100 | * @param Button $button 101 | * 102 | * @return Fabric 103 | */ 104 | public function addButton(Button $button): self 105 | { 106 | $this->buttons->attach($button); 107 | 108 | return $this; 109 | } 110 | 111 | /** 112 | * @return Response 113 | */ 114 | public function buildResponse(): Response 115 | { 116 | $session = new Session(); 117 | $session 118 | ->setSessionId($this->request->getSession()->getSessionId()) 119 | ->setUserId($this->request->getSession()->getUserId()) 120 | ->setMessageId($this->request->getSession()->getMessageId()); 121 | 122 | $response = new Response(); 123 | $response 124 | ->setVersion($this->request->getVersion()) 125 | ->setText($this->text) 126 | ->setTts($this->tts) 127 | ->setIsEndSession($this->isEndSession) 128 | ->setButtons($this->buttons) 129 | ->setSession($session); 130 | 131 | return $response; 132 | } 133 | } -------------------------------------------------------------------------------- /src/Dialogs/Webhook/Response/Formatters/Formatter.php: -------------------------------------------------------------------------------- 1 | getButtons() as $btn) { 24 | $buttons[] = [ 25 | 'title' => $btn->getTitle(), 26 | 'payload' => $btn->getPayload() ?? '', 27 | 'url' => $btn->getUrl() ?? '', 28 | 'hide' => $btn->isHide(), 29 | ]; 30 | } 31 | 32 | return [ 33 | 'response' => [ 34 | 'text' => $response->getText(), 35 | 'tts' => $response->getTts(), 36 | 'buttons' => $buttons, 37 | ], 38 | 'end_session' => $response->isEndSession(), 39 | 'session' => [ 40 | 'session_id' => $response->getSession()->getSessionId(), 41 | 'message_id' => $response->getSession()->getMessageId(), 42 | 'user_id' => $response->getSession()->getUserId(), 43 | ], 44 | 'version' => $response->getVersion(), 45 | ]; 46 | } 47 | } --------------------------------------------------------------------------------