Skip to content

Commit 443fa28

Browse files
author
Sureshkumar
committed
Init
1 parent 014236f commit 443fa28

File tree

7 files changed

+228
-2
lines changed

7 files changed

+228
-2
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.gitignore
2+
.idea/

README.md

Lines changed: 0 additions & 2 deletions
This file was deleted.

composer.json

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{
2+
"name": "jeylabs/wit",
3+
"type": "library",
4+
"description": "PHP Library for Wit.ai API with Laravel 5.3 Support out of the box!",
5+
"keywords": [
6+
"wit",
7+
"wit.ai",
8+
"laravel",
9+
"laravel wit"
10+
],
11+
"homepage": "https://github.com/jeylabs/wit",
12+
"license": "MIT",
13+
"authors": [
14+
{
15+
"name": "jeylabs",
16+
"email": "[email protected]",
17+
"homepage": "https://jeylabs.com",
18+
"role": "Developer"
19+
}
20+
],
21+
"require": {
22+
"php": "~7.0",
23+
"guzzlehttp/guzzle": "~6.2",
24+
"illuminate/support": "5.3.*"
25+
},
26+
"require-dev": {
27+
"phpunit/phpunit": "4.*",
28+
"scrutinizer/ocular": "~1.1",
29+
"squizlabs/php_codesniffer": "~2.3"
30+
},
31+
"autoload": {
32+
"psr-4": {
33+
"Jeylabs\\Wit\\": "src"
34+
}
35+
},
36+
"autoload-dev": {
37+
"psr-4": {
38+
"Jeylabs\\Wit\\Test\\": "tests"
39+
}
40+
},
41+
"scripts": {
42+
"test": "phpunit"
43+
},
44+
"minimum-stability": "stable",
45+
"prefer-stable": true
46+
}

src/Laravel/Facades/Wit.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Jeylabs\Wit\Laravel\Facades;
4+
5+
use Illuminate\Support\Facades\Facade;
6+
7+
class Wit extends Facade
8+
{
9+
protected static function getFacadeAccessor()
10+
{
11+
return 'wit';
12+
}
13+
}

src/Laravel/WitServiceProvider.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace Jeylabs\Wit\Laravel;
4+
5+
use Illuminate\Contracts\Container\Container as Application;
6+
use Illuminate\Foundation\Application as LaravelApplication;
7+
use Illuminate\Support\ServiceProvider;
8+
use Jeylabs\Wit\Wit;
9+
use Laravel\Lumen\Application as LumenApplication;
10+
11+
class WitServiceProvider extends ServiceProvider
12+
{
13+
protected $defer = true;
14+
15+
public function boot()
16+
{
17+
$this->setupConfig($this->app);
18+
}
19+
20+
protected function setupConfig(Application $app)
21+
{
22+
$source = __DIR__ . '/config/wit.php';
23+
24+
if ($app instanceof LaravelApplication && $app->runningInConsole()) {
25+
$this->publishes([$source => config_path('wit.php')]);
26+
} elseif ($app instanceof LumenApplication) {
27+
$app->configure('wit');
28+
}
29+
30+
$this->mergeConfigFrom($source, 'wit');
31+
}
32+
33+
public function register()
34+
{
35+
$this->registerBindings($this->app);
36+
}
37+
38+
protected function registerBindings(Application $app)
39+
{
40+
$app->singleton('wit', function ($app) {
41+
$config = $app['config'];
42+
43+
return new Wit(
44+
$config->get('wit.access_token', null),
45+
$config->get('wit.async_requests', false)
46+
);
47+
});
48+
49+
$app->alias('wit', Wit::class);
50+
}
51+
52+
public function provides()
53+
{
54+
return ['wit'];
55+
}
56+
}

src/Laravel/config/wit.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
return [
4+
'access_token' => env('WIT_ACCESS_TOKEN', 'YOUR_WIT_ACCESS_TOKEN'),
5+
'async_requests' => env('WIT_ASYNC_REQUESTS', false),
6+
];

src/Wit.php

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
3+
namespace Jeylabs\Wit;
4+
5+
use GuzzleHttp\Client;
6+
use GuzzleHttp\Promise;
7+
use GuzzleHttp\Promise\PromiseInterface;
8+
use GuzzleHttp\RequestOptions as GuzzleRequestOptions;
9+
10+
class Wit
11+
{
12+
13+
const VERSION = '1.0.0';
14+
const WIT_API_BASE_URI = 'https://api.wit.ai/';
15+
const TEXT_INTENT_API = 'message';
16+
const SPEECH_INTENT_API = 'speech';
17+
const ENTITIES_API = 'entities';
18+
const WIT_API_VERSION = '20160526';
19+
const DEFAULT_TIMEOUT = 5;
20+
protected $client;
21+
protected $access_token;
22+
protected $isAsyncRequest = false;
23+
protected $headers = [];
24+
protected $promises = [];
25+
protected $lastResponse;
26+
27+
public function __construct($access_token, $isAsyncRequest = false, $httpClient = null)
28+
{
29+
$this->access_token = $access_token;
30+
$this->isAsyncRequest = $isAsyncRequest;
31+
$this->client = $httpClient ?: new Client([
32+
'base_uri' => self::WIT_API_BASE_URI,
33+
'timeout' => self::DEFAULT_TIMEOUT,
34+
'connect_timeout' => self::DEFAULT_TIMEOUT,
35+
]);
36+
}
37+
38+
public function isAsyncRequests()
39+
{
40+
return $this->isAsyncRequest;
41+
}
42+
43+
public function setAsyncRequests($isAsyncRequest)
44+
{
45+
$this->isAsyncRequest = $isAsyncRequest;
46+
47+
return $this;
48+
}
49+
50+
public function getHeaders()
51+
{
52+
return $this->headers;
53+
}
54+
55+
public function setHeaders($headers = [])
56+
{
57+
$this->headers = $headers;
58+
59+
return $this;
60+
}
61+
62+
public function getLastResponse()
63+
{
64+
return $this->lastResponse;
65+
}
66+
67+
public function getIntentByText($q, $params = [])
68+
{
69+
$query = array_merge(compact('q'), $params);
70+
71+
return $this->makeRequest('GET', self::TEXT_INTENT_API, $query);
72+
}
73+
74+
protected function makeRequest($method, $uri, $query = [])
75+
{
76+
$options[GuzzleRequestOptions::QUERY] = $query;
77+
$options[GuzzleRequestOptions::HEADERS] = $this->getDefaultHeaders();
78+
if ($this->isAsyncRequest) {
79+
return $this->promises[] = $this->client->requestAsync($method, $uri, $options);
80+
}
81+
$this->lastResponse = $this->client->request($method, $uri, $options);
82+
return json_decode($this->lastResponse->getBody(), true);
83+
}
84+
85+
protected function getDefaultHeaders()
86+
{
87+
return array_merge([
88+
'User-Agent' => 'wit-' . self::VERSION,
89+
'Authorization' => 'Bearer ' . $this->access_token,
90+
'Accept' => 'application/vnd.wit.' . self::WIT_API_VERSION . '+json',
91+
], $this->headers);
92+
}
93+
94+
public function getIntentBySpeech($q, $params = [])
95+
{
96+
$this->setHeaders(['Content-type' => 'audio/wav']);
97+
$query = array_merge(compact('q'), $params);
98+
return $this->makeRequest('POST', self::SPEECH_INTENT_API, $query);
99+
}
100+
101+
public function __destruct()
102+
{
103+
Promise\unwrap($this->promises);
104+
}
105+
}

0 commit comments

Comments
 (0)