├── .editorconfig ├── CHANGELOG.md ├── CONDUCT.md ├── CONTRIBUTING.md ├── LICENSE.md ├── README.md ├── composer.json └── src ├── Laravel ├── Facades │ └── Wit.php ├── WitServiceProvider.php └── config │ └── wit.php └── Wit.php /.editorconfig: -------------------------------------------------------------------------------- 1 | ; This file is for unifying the coding style for different editors and IDEs. 2 | ; More information at http://editorconfig.org 3 | 4 | root = true 5 | 6 | [*] 7 | charset = utf-8 8 | indent_size = 4 9 | indent_style = space 10 | end_of_line = lf 11 | insert_final_newline = true 12 | trim_trailing_whitespace = true 13 | 14 | [*.md] 15 | trim_trailing_whitespace = false 16 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All Notable changes to `php-witai` will be documented in this file. 4 | 5 | Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) principles. 6 | -------------------------------------------------------------------------------- /CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Code of Conduct 2 | 3 | As contributors and maintainers of this project, and in the interest of fostering an open and welcoming community, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities. 4 | 5 | We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, religion, or nationality. 6 | 7 | Examples of unacceptable behavior by participants include: 8 | 9 | * The use of sexualized language or imagery 10 | * Personal attacks 11 | * Trolling or insulting/derogatory comments 12 | * Public or private harassment 13 | * Publishing other's private information, such as physical or electronic addresses, without explicit permission 14 | * Other unethical or unprofessional conduct. 15 | 16 | Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. By adopting this Code of Conduct, project maintainers commit themselves to fairly and consistently applying these principles to every aspect of managing this project. Project maintainers who do not follow or enforce the Code of Conduct may be permanently removed from the project team. 17 | 18 | This code of conduct applies both within project spaces and in public spaces when an individual is representing the project or its community in a direct capacity. Personal views, beliefs and values of individuals do not necessarily reflect those of the organisation or affiliated individuals and organisations. 19 | 20 | Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers. 21 | 22 | This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.2.0, available at [http://contributor-covenant.org/version/1/2/0/](http://contributor-covenant.org/version/1/2/0/) 23 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | Contributions are **welcome** and will be fully **credited**. 4 | 5 | We accept contributions via Pull Requests on [Github](https://github.com/irazasyed/php-witai). 6 | 7 | 8 | ## Pull Requests 9 | 10 | - **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](http://pear.php.net/package/PHP_CodeSniffer). 11 | 12 | - **Add tests!** - Your patch won't be accepted if it doesn't have tests. 13 | 14 | - **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date. 15 | 16 | - **Consider our release cycle** - We try to follow [SemVer v2.0.0](http://semver.org/). Randomly breaking public APIs is not an option. 17 | 18 | - **Create feature branches** - Don't ask us to pull from your master branch. 19 | 20 | - **One pull request per feature** - If you want to do more than one thing, send multiple pull requests. 21 | 22 | - **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](http://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting. 23 | 24 | 25 | ## Running Tests 26 | 27 | ``` bash 28 | $ composer test 29 | ``` 30 | 31 | 32 | **Happy coding**! 33 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Syed Irfaq R. 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 13 | > all 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 21 | > THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PHP-Witai Library 2 | 3 | [![Latest Version on Packagist][ico-version]][link-packagist] 4 | [![Software License][ico-license]](LICENSE.md) 5 | [![Build Status][ico-travis]][link-travis] 6 | [![Coverage Status][ico-scrutinizer]][link-scrutinizer] 7 | [![Quality Score][ico-code-quality]][link-code-quality] 8 | [![Total Downloads][ico-downloads]][link-downloads] 9 | 10 | > PHP Library for Wit.ai API with Laravel & Lumen Support out of the box! 11 | 12 | ## Install 13 | 14 | Via Composer 15 | 16 | ``` bash 17 | $ composer require irazasyed/php-witai 18 | ``` 19 | 20 | ## Usage 21 | 22 | ``` php 23 | $wit = new Irazasyed\Wit($access_token, $isAsyncRequests = false); 24 | $response = $wit->getIntentByText('Whats the weather like in San Francisco?', $optional_params); 25 | ``` 26 | 27 | ## Change log 28 | 29 | Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently. 30 | 31 | ## Testing 32 | 33 | ``` bash 34 | $ composer test 35 | ``` 36 | 37 | ## Contributing 38 | 39 | Please see [CONTRIBUTING](CONTRIBUTING.md) and [CONDUCT](CONDUCT.md) for details. 40 | 41 | ## Security 42 | 43 | If you discover any security related issues, please email syed+gh@lukonet.com instead of using the issue tracker. 44 | 45 | ## Credits 46 | 47 | - [Syed Irfaq R.][link-author] 48 | - [All Contributors][link-contributors] 49 | 50 | ## License 51 | 52 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 53 | 54 | [ico-version]: https://img.shields.io/packagist/v/irazasyed/php-witai.svg?style=flat-square 55 | [ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square 56 | [ico-travis]: https://img.shields.io/travis/irazasyed/php-witai/master.svg?style=flat-square 57 | [ico-scrutinizer]: https://img.shields.io/scrutinizer/coverage/g/irazasyed/php-witai.svg?style=flat-square 58 | [ico-code-quality]: https://img.shields.io/scrutinizer/g/irazasyed/php-witai.svg?style=flat-square 59 | [ico-downloads]: https://img.shields.io/packagist/dt/irazasyed/php-witai.svg?style=flat-square 60 | 61 | [link-packagist]: https://packagist.org/packages/irazasyed/php-witai 62 | [link-travis]: https://travis-ci.org/irazasyed/php-witai 63 | [link-scrutinizer]: https://scrutinizer-ci.com/g/irazasyed/php-witai/code-structure 64 | [link-code-quality]: https://scrutinizer-ci.com/g/irazasyed/php-witai 65 | [link-downloads]: https://packagist.org/packages/irazasyed/php-witai 66 | [link-author]: https://github.com/irazasyed 67 | [link-contributors]: ../../contributors 68 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "irazasyed/php-witai", 3 | "type": "library", 4 | "description": "PHP Library for Wit.ai API with Laravel & Lumen Support out of the box!", 5 | "keywords": [ 6 | "irazasyed", 7 | "php-witai", 8 | "wit", 9 | "wit.ai", 10 | "witai", 11 | "php-wit", 12 | "laravel", 13 | "lumen", 14 | "laravel wit", 15 | "lumen wit" 16 | ], 17 | "homepage": "https://github.com/irazasyed/php-witai", 18 | "license": "MIT", 19 | "authors": [ 20 | { 21 | "name": "Syed Irfaq R.", 22 | "email": "syed+gh@lukonet.com", 23 | "homepage": "https://lukonet.com", 24 | "role": "Developer" 25 | } 26 | ], 27 | "require": { 28 | "php" : "~5.5|~7.0", 29 | "guzzlehttp/guzzle": "~6.0", 30 | "illuminate/support": "~5.0" 31 | }, 32 | "require-dev": { 33 | "phpunit/phpunit" : "4.*", 34 | "scrutinizer/ocular": "~1.1", 35 | "squizlabs/php_codesniffer": "~2.3" 36 | }, 37 | "autoload": { 38 | "psr-4": { 39 | "Irazasyed\\Wit\\": "src" 40 | } 41 | }, 42 | "autoload-dev": { 43 | "psr-4": { 44 | "Irazasyed\\Wit\\Test\\": "tests" 45 | } 46 | }, 47 | "scripts": { 48 | "test": "phpunit" 49 | }, 50 | "extra": { 51 | "branch-alias": { 52 | "dev-master": "1.0-dev" 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/Laravel/Facades/Wit.php: -------------------------------------------------------------------------------- 1 | setupConfig($this->app); 28 | } 29 | 30 | /** 31 | * Setup the config. 32 | * 33 | * @param \Illuminate\Contracts\Container\Container $app 34 | * 35 | * @return void 36 | */ 37 | protected function setupConfig(Application $app) 38 | { 39 | $source = __DIR__.'/config/wit.php'; 40 | 41 | if ($app instanceof LaravelApplication && $app->runningInConsole()) { 42 | $this->publishes([$source => config_path('wit.php')]); 43 | } elseif ($app instanceof LumenApplication) { 44 | $app->configure('wit'); 45 | } 46 | 47 | $this->mergeConfigFrom($source, 'wit'); 48 | } 49 | 50 | /** 51 | * Register the service provider. 52 | * 53 | * @return void 54 | */ 55 | public function register() 56 | { 57 | $this->registerBindings($this->app); 58 | } 59 | 60 | /** 61 | * Register the wit class. 62 | * 63 | * @param \Illuminate\Contracts\Container\Container $app 64 | * 65 | * @return void 66 | */ 67 | protected function registerBindings(Application $app) 68 | { 69 | $app->singleton('wit', function ($app) { 70 | $config = $app['config']; 71 | 72 | return new Wit( 73 | $config->get('wit.access_token', null), 74 | $config->get('wit.async_requests', false) 75 | ); 76 | }); 77 | 78 | $app->alias('wit', Wit::class); 79 | } 80 | 81 | /** 82 | * Get the services provided by the provider. 83 | * 84 | * @return array 85 | */ 86 | public function provides() 87 | { 88 | return ['wit']; 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /src/Laravel/config/wit.php: -------------------------------------------------------------------------------- 1 | env('WIT_ACCESS_TOKEN', 'YOUR_WIT_ACCESS_TOKEN'), 14 | 15 | /* 16 | |-------------------------------------------------------------------------- 17 | | Asynchronous Requests [Optional] 18 | |-------------------------------------------------------------------------- 19 | | 20 | | When set to True, All the requests would be made non-blocking (Async). 21 | | 22 | | Default: false 23 | | Possible Values: (Boolean) "true" OR "false" 24 | | 25 | */ 26 | 'async_requests' => env('WIT_ASYNC_REQUESTS', false), 27 | ]; 28 | -------------------------------------------------------------------------------- /src/Wit.php: -------------------------------------------------------------------------------- 1 | access_token = $access_token; 90 | $this->isAsyncRequest = $isAsyncRequest; 91 | $this->client = $httpClient ?: new Client([ 92 | 'base_uri' => self::WIT_API_BASE_URI, 93 | 'timeout' => self::DEFAULT_TIMEOUT, 94 | 'connect_timeout' => self::DEFAULT_TIMEOUT, 95 | ]); 96 | } 97 | 98 | /** 99 | * Check if this is an asynchronous request (non-blocking). 100 | * 101 | * @return bool 102 | */ 103 | public function isAsyncRequests() 104 | { 105 | return $this->isAsyncRequest; 106 | } 107 | 108 | /** 109 | * Make HTTP Requests Non-Blocking (Async). 110 | * 111 | * @param $isAsyncRequest 112 | * 113 | * @return $this 114 | */ 115 | public function setAsyncRequests($isAsyncRequest) 116 | { 117 | $this->isAsyncRequest = $isAsyncRequest; 118 | 119 | return $this; 120 | } 121 | 122 | /** 123 | * Get HTTP Headers. 124 | * 125 | * @return array 126 | */ 127 | public function getHeaders() 128 | { 129 | return $this->headers; 130 | } 131 | 132 | /** 133 | * Set HTTP Headers. 134 | * 135 | * @param array $headers 136 | * 137 | * @return $this 138 | */ 139 | public function setHeaders($headers = []) 140 | { 141 | $this->headers = $headers; 142 | 143 | return $this; 144 | } 145 | 146 | /** 147 | * Get Last HTTP Request Response. 148 | * 149 | * @return object 150 | */ 151 | public function getLastResponse() 152 | { 153 | return $this->lastResponse; 154 | } 155 | 156 | /** 157 | * Get Intent from Text Query! 158 | * 159 | * @param $q 160 | * @param array $params 161 | * 162 | * @return \GuzzleHttp\Promise\PromiseInterface|mixed 163 | */ 164 | public function getIntentByText($q, $params = []) 165 | { 166 | $query = array_merge(compact('q'), $params); 167 | 168 | return $this->makeRequest('GET', self::TEXT_INTENT_API, $query); 169 | } 170 | 171 | /** 172 | * Get Intent from Speech (Audio). 173 | * 174 | * @param $q 175 | * @param array $params 176 | * 177 | * @return \GuzzleHttp\Promise\PromiseInterface|mixed 178 | */ 179 | public function getIntentBySpeech($q, $params = []) 180 | { 181 | // $this->setHeaders(['Content-type' => 'audio/wav']); 182 | // 183 | // $query = array_merge(compact('q'), $params); 184 | // 185 | // return $this->makeRequest('POST', self::SPEECH_INTENT_API, $query); 186 | } 187 | 188 | /** 189 | * Make HTTP Request. 190 | * 191 | * @param $method 192 | * @param $uri 193 | * @param array $query 194 | * 195 | * @return \GuzzleHttp\Promise\PromiseInterface|mixed 196 | */ 197 | protected function makeRequest($method, $uri, $query = []) 198 | { 199 | $options[GuzzleRequestOptions::QUERY] = $query; 200 | $options[GuzzleRequestOptions::HEADERS] = $this->getDefaultHeaders(); 201 | 202 | if ($this->isAsyncRequest) { 203 | return $this->promises[] = $this->client->requestAsync($method, $uri, $options); 204 | } 205 | 206 | $this->lastResponse = $this->client->request($method, $uri, $options); 207 | 208 | return json_decode($this->lastResponse->getBody(), true); 209 | } 210 | 211 | /** 212 | * Get Default Headers for HTTP Requests. 213 | * 214 | * @return array 215 | */ 216 | protected function getDefaultHeaders() 217 | { 218 | return array_merge([ 219 | 'User-Agent' => 'php-witai-'.self::VERSION, 220 | 'Authorization' => 'Bearer '.$this->access_token, 221 | 'Accept' => 'application/vnd.wit.'.self::WIT_API_VERSION.'+json', 222 | ], $this->headers); 223 | } 224 | 225 | /** 226 | * Unwrap Promises. 227 | */ 228 | public function __destruct() 229 | { 230 | Promise\unwrap($this->promises); 231 | } 232 | } 233 | --------------------------------------------------------------------------------