Skip to content

Commit af2acbf

Browse files
committed
Initial commit
1 parent 58665ad commit af2acbf

40 files changed

+5966
-19
lines changed

.editorconfig

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
end_of_line = lf
6+
insert_final_newline = true
7+
indent_style = space
8+
indent_size = 4
9+
trim_trailing_whitespace = true
10+
11+
[*.md]
12+
trim_trailing_whitespace = false
13+
14+
[*.yml]
15+
indent_size = 2

.env.example

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
APP_NAME=Google-Assistant-Demo
2+
APP_ENV=local
3+
APP_KEY=
4+
APP_DEBUG=true
5+
APP_URL=http://localhost
6+
APP_TIMEZONE=UTC
7+
APP_USER_AGENT=Google-Assistant-Demo
8+
9+
SL_TIMETABLE_KEY=d74c25350fa64ecca5c66ad199629ad3
10+
SL_ROUTEPLANNING_KEY=27b2b28871ec4e479b310b72468a6c29
11+
RESROBOT_TIMETABLE_KEY=9b74c5e4-9d47-4bf4-a094-c51d9913ef4f
12+
RESROBOT_ROUTEPLANNING_KEY=67f4d905-ad36-4574-aa6b-c01288b808bc

.gitignore

+4-19
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,5 @@
1-
vendor/
2-
node_modules/
3-
npm-debug.log
4-
5-
# Laravel 4 specific
6-
bootstrap/compiled.php
7-
app/storage/
8-
9-
# Laravel 5 & Lumen specific
10-
public/storage
11-
public/hot
12-
storage/*.key
13-
.env.*.php
14-
.env.php
15-
.env
16-
Homestead.yaml
1+
/vendor
2+
/.idea
173
Homestead.json
18-
19-
# Rocketeer PHP task runner and deployment package. https://github.com/rocketeers/rocketeer
20-
.rocketeer/
4+
Homestead.yaml
5+
.env

app/Console/Commands/.gitkeep

Whitespace-only changes.

app/Console/Kernel.php

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace App\Console;
4+
5+
use Illuminate\Console\Scheduling\Schedule;
6+
use Laravel\Lumen\Console\Kernel as ConsoleKernel;
7+
8+
class Kernel extends ConsoleKernel
9+
{
10+
/**
11+
* The Artisan commands provided by your application.
12+
*
13+
* @var array
14+
*/
15+
protected $commands = [
16+
//
17+
];
18+
19+
/**
20+
* Define the application's command schedule.
21+
*
22+
* @param \Illuminate\Console\Scheduling\Schedule $schedule
23+
* @return void
24+
*/
25+
protected function schedule(Schedule $schedule)
26+
{
27+
//
28+
}
29+
}

app/Events/Event.php

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace App\Events;
4+
5+
use Illuminate\Queue\SerializesModels;
6+
7+
abstract class Event
8+
{
9+
use SerializesModels;
10+
}

app/Events/ExampleEvent.php

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace App\Events;
4+
5+
class ExampleEvent extends Event
6+
{
7+
/**
8+
* Create a new event instance.
9+
*
10+
* @return void
11+
*/
12+
public function __construct()
13+
{
14+
//
15+
}
16+
}

app/Exceptions/Handler.php

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace App\Exceptions;
4+
5+
use Exception;
6+
use Illuminate\Validation\ValidationException;
7+
use Illuminate\Auth\Access\AuthorizationException;
8+
use Illuminate\Database\Eloquent\ModelNotFoundException;
9+
use Laravel\Lumen\Exceptions\Handler as ExceptionHandler;
10+
use Symfony\Component\HttpKernel\Exception\HttpException;
11+
12+
class Handler extends ExceptionHandler
13+
{
14+
/**
15+
* A list of the exception types that should not be reported.
16+
*
17+
* @var array
18+
*/
19+
protected $dontReport = [
20+
AuthorizationException::class,
21+
HttpException::class,
22+
ModelNotFoundException::class,
23+
ValidationException::class,
24+
];
25+
26+
/**
27+
* Report or log an exception.
28+
*
29+
* This is a great spot to send exceptions to Sentry, Bugsnag, etc.
30+
*
31+
* @param \Exception $exception
32+
* @return void
33+
*/
34+
public function report(Exception $exception)
35+
{
36+
parent::report($exception);
37+
}
38+
39+
/**
40+
* Render an exception into an HTTP response.
41+
*
42+
* @param \Illuminate\Http\Request $request
43+
* @param \Exception $exception
44+
* @return \Illuminate\Http\Response|\Illuminate\Http\JsonResponse
45+
*/
46+
public function render($request, Exception $exception)
47+
{
48+
return parent::render($request, $exception);
49+
}
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use App\Http\Models\DialogFlowPayload;
6+
use Laravel\Lumen\Routing\Controller as BaseController;
7+
8+
class GoogleHomeController extends BaseController
9+
{
10+
11+
private $_dialogFlowPayload;
12+
13+
public function __construct(array $jsonPayload)
14+
{
15+
$this->_dialogFlowPayload = new DialogFlowPayload($jsonPayload);
16+
}
17+
18+
/**
19+
* Send a JSON response to Google Dialogflow in order to make google assistant answer the question.
20+
*
21+
* @param string $responseText
22+
*/
23+
public function respondWithTextToSpeech(string $responseText)
24+
{
25+
response()->json($this->buildGoogleAssistantResponse($responseText));
26+
}
27+
28+
/**
29+
* @param string $responseText
30+
*
31+
* @return array
32+
*/
33+
public function buildGoogleAssistantResponse(string $responseText): array
34+
{
35+
return [
36+
'payload' => [
37+
'google' => [
38+
'expectUserResponse' => false,
39+
"richResponse" => [
40+
"items" => [
41+
[
42+
"simpleResponse" => [
43+
"textToSpeech" => "$responseText",
44+
],
45+
],
46+
],
47+
],
48+
],
49+
],
50+
];
51+
}
52+
53+
/**
54+
* Get the payload sent to the server by Dialogflow
55+
*
56+
* @return DialogFlowPayload
57+
*/
58+
public function getDialogFlowPayload(): DialogFlowPayload
59+
{
60+
return $this->_dialogFlowPayload;
61+
}
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use App\Http\Repositories\SlDataRepository;
6+
use Illuminate\Http\Request;
7+
use Sl\SlWrapper;
8+
use Trafiklab\Common\Model\Exceptions\InvalidKeyException;
9+
use Trafiklab\Common\Model\Exceptions\InvalidRequestException;
10+
use Trafiklab\Common\Model\Exceptions\InvalidStoplocationException;
11+
use Trafiklab\Common\Model\Exceptions\KeyRequiredException;
12+
use Trafiklab\Common\Model\Exceptions\QuotaExceededException;
13+
use Trafiklab\Common\Model\Exceptions\RequestTimedOutException;
14+
use Trafiklab\Common\Model\Exceptions\ServiceUnavailableException;
15+
use Trafiklab\Sl\Model\SlTimeTableRequest;
16+
17+
class NextDepartureController extends GoogleHomeController
18+
{
19+
/**
20+
* Create a new controller instance.
21+
*
22+
* @return void
23+
*/
24+
public function __construct(Request $request)
25+
{
26+
parent::__construct(json_decode($request->getContent(), true));
27+
}
28+
29+
public function getNextDeparture()
30+
{
31+
$locationName = $this->getDialogFlowPayload()->getParameter('location');
32+
$slData = new SlDataRepository();
33+
$slData->getStationId($locationName);
34+
35+
$timeTableRequest = new SlTimeTableRequest();
36+
37+
/**
38+
* @var $slWrapper SlWrapper
39+
*/
40+
$slWrapper = app(SlWrapper::class);
41+
try {
42+
$response = $slWrapper->getTimeTable($timeTableRequest);
43+
// Create a response
44+
$this->respondWithTextToSpeech("The next {$response->getTimetable()[0]->getTransportType()}
45+
from {$response->getTimetable()[0]->getStopName()} is
46+
{$response->getTimetable()[0]->getLineNumber()} {$response->getTimetable()[0]->getLineName()}
47+
at {$response->getTimetable()[0]->getScheduledStopTime()->format("H:i")}"
48+
);
49+
} catch (InvalidKeyException $e) {
50+
$this->respondWithTextToSpeech("I would like to answer you, but I don't have the right keys");
51+
} catch (InvalidStoplocationException $e) {
52+
$this->respondWithTextToSpeech("I would like to answer you, but don't know that station");
53+
} catch (KeyRequiredException $e) {
54+
$this->respondWithTextToSpeech("I would like to answer you, but I don't have the right keys");
55+
} catch (InvalidRequestException $e) {
56+
$this->respondWithTextToSpeech("I would like to answer you, but I didn't get all the details");
57+
} catch (QuotaExceededException $e) {
58+
$this->respondWithTextToSpeech("I would like to answer you, but I already talked too much");
59+
} catch (RequestTimedOutException $e) {
60+
$this->respondWithTextToSpeech("I could not obtain this data, it took too long");
61+
} catch (ServiceUnavailableException $e) {
62+
$this->respondWithTextToSpeech("I could not obtain this data, the service is not available");
63+
}
64+
}
65+
66+
67+
}

app/Http/Middleware/Authenticate.php

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace App\Http\Middleware;
4+
5+
use Closure;
6+
use Illuminate\Contracts\Auth\Factory as Auth;
7+
8+
class Authenticate
9+
{
10+
/**
11+
* The authentication guard factory instance.
12+
*
13+
* @var \Illuminate\Contracts\Auth\Factory
14+
*/
15+
protected $auth;
16+
17+
/**
18+
* Create a new middleware instance.
19+
*
20+
* @param \Illuminate\Contracts\Auth\Factory $auth
21+
* @return void
22+
*/
23+
public function __construct(Auth $auth)
24+
{
25+
$this->auth = $auth;
26+
}
27+
28+
/**
29+
* Handle an incoming request.
30+
*
31+
* @param \Illuminate\Http\Request $request
32+
* @param \Closure $next
33+
* @param string|null $guard
34+
* @return mixed
35+
*/
36+
public function handle($request, Closure $next, $guard = null)
37+
{
38+
if ($this->auth->guard($guard)->guest()) {
39+
return response('Unauthorized.', 401);
40+
}
41+
42+
return $next($request);
43+
}
44+
}
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace App\Http\Middleware;
4+
5+
use Closure;
6+
7+
class ExampleMiddleware
8+
{
9+
/**
10+
* Handle an incoming request.
11+
*
12+
* @param \Illuminate\Http\Request $request
13+
* @param \Closure $next
14+
* @return mixed
15+
*/
16+
public function handle($request, Closure $next)
17+
{
18+
return $next($request);
19+
}
20+
}

0 commit comments

Comments
 (0)