1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace MaxBeckers\AmazonAlexa\Helper; |
6
|
|
|
|
7
|
|
|
use MaxBeckers\AmazonAlexa\Response\Card; |
8
|
|
|
use MaxBeckers\AmazonAlexa\Response\Directives\Directive; |
9
|
|
|
use MaxBeckers\AmazonAlexa\Response\OutputSpeech; |
10
|
|
|
use MaxBeckers\AmazonAlexa\Response\Reprompt; |
11
|
|
|
use MaxBeckers\AmazonAlexa\Response\Response; |
12
|
|
|
use MaxBeckers\AmazonAlexa\Response\ResponseBody; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* This helper class can create simple responses for the most needed intents. |
16
|
|
|
*/ |
17
|
|
|
class ResponseHelper |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @param Response|null $response The response object |
21
|
|
|
* @param ResponseBody|null $responseBody The response body object |
22
|
|
|
*/ |
23
|
20 |
|
public function __construct( |
24
|
|
|
public ?Response $response = null, |
25
|
|
|
public ?ResponseBody $responseBody = null, |
26
|
|
|
) { |
27
|
20 |
|
if ($this->response === null || $this->responseBody === null) { |
28
|
20 |
|
$this->resetResponse(); |
29
|
|
|
} |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Add a plaintext respond to response. |
34
|
|
|
*/ |
35
|
2 |
|
public function respond(string $text, bool $endSession = false): ?Response |
36
|
|
|
{ |
37
|
2 |
|
$outputSpeech = OutputSpeech::createByText($text); |
38
|
|
|
|
39
|
2 |
|
$this->responseBody->outputSpeech = $outputSpeech; |
40
|
2 |
|
$this->responseBody->shouldEndSession = $endSession; |
41
|
|
|
|
42
|
2 |
|
return $this->response; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Add a ssml respond to response. |
47
|
|
|
*/ |
48
|
2 |
|
public function respondSsml(string $ssml, bool $endSession = false): ?Response |
49
|
|
|
{ |
50
|
2 |
|
$outputSpeech = OutputSpeech::createBySsml($ssml); |
51
|
|
|
|
52
|
2 |
|
$this->responseBody->outputSpeech = $outputSpeech; |
53
|
2 |
|
$this->responseBody->shouldEndSession = $endSession; |
54
|
|
|
|
55
|
2 |
|
return $this->response; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Add a plaintext reprompt to response. |
60
|
|
|
*/ |
61
|
1 |
|
public function reprompt(string $text): ?Response |
62
|
|
|
{ |
63
|
1 |
|
$outputSpeech = OutputSpeech::createByText($text); |
64
|
1 |
|
$reprompt = new Reprompt($outputSpeech); |
65
|
|
|
|
66
|
1 |
|
$this->responseBody->reprompt = $reprompt; |
67
|
|
|
|
68
|
1 |
|
return $this->response; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Add a ssml reprompt to response. |
73
|
|
|
*/ |
74
|
1 |
|
public function repromptSsml(string $ssml): ?Response |
75
|
|
|
{ |
76
|
1 |
|
$outputSpeech = OutputSpeech::createBySsml($ssml); |
77
|
1 |
|
$reprompt = new Reprompt($outputSpeech); |
78
|
|
|
|
79
|
1 |
|
$this->responseBody->reprompt = $reprompt; |
80
|
|
|
|
81
|
1 |
|
return $this->response; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Add a card to response. |
86
|
|
|
*/ |
87
|
1 |
|
public function card(Card $card): ?Response |
88
|
|
|
{ |
89
|
1 |
|
$this->responseBody->card = $card; |
90
|
|
|
|
91
|
1 |
|
return $this->response; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Add a directive to response. |
96
|
|
|
*/ |
97
|
2 |
|
public function directive(Directive $directive): ?Response |
98
|
|
|
{ |
99
|
2 |
|
$this->responseBody->addDirective($directive); |
|
|
|
|
100
|
|
|
|
101
|
2 |
|
return $this->response; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Add a new attribute to response session attributes. |
106
|
|
|
*/ |
107
|
1 |
|
public function addSessionAttribute(string $key, string $value): void |
108
|
|
|
{ |
109
|
1 |
|
$this->response->sessionAttributes[$key] = $value; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Reset the response in ResponseHelper. |
114
|
|
|
*/ |
115
|
20 |
|
public function resetResponse(): void |
116
|
|
|
{ |
117
|
20 |
|
$this->responseBody = new ResponseBody(); |
118
|
20 |
|
$this->response = new Response([], '1.0', $this->responseBody); |
119
|
|
|
} |
120
|
|
|
|
121
|
1 |
|
public function getResponse(): Response |
122
|
|
|
{ |
123
|
1 |
|
return $this->response; |
|
|
|
|
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.