1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BotMan\BotMan\Messages\Conversations; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use BotMan\BotMan\BotMan; |
7
|
|
|
use Spatie\Macroable\Macroable; |
8
|
|
|
use Illuminate\Support\Collection; |
9
|
|
|
use BotMan\BotMan\Interfaces\ShouldQueue; |
10
|
|
|
use BotMan\BotMan\Messages\Attachments\Audio; |
11
|
|
|
use BotMan\BotMan\Messages\Attachments\Image; |
12
|
|
|
use BotMan\BotMan\Messages\Attachments\Video; |
13
|
|
|
use BotMan\BotMan\Messages\Outgoing\Question; |
14
|
|
|
use BotMan\BotMan\Messages\Attachments\Location; |
15
|
|
|
use BotMan\BotMan\Messages\Incoming\IncomingMessage; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class Conversation. |
19
|
|
|
*/ |
20
|
|
|
abstract class Conversation |
21
|
|
|
{ |
22
|
|
|
use Macroable; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var BotMan |
26
|
|
|
*/ |
27
|
|
|
protected $bot; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $token; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Number of minutes this specific conversation should be cached. |
36
|
|
|
* @var int |
37
|
|
|
*/ |
38
|
|
|
protected $cacheTime; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param BotMan $bot |
42
|
|
|
*/ |
43
|
|
|
public function setBot(BotMan $bot) |
44
|
|
|
{ |
45
|
|
|
$this->bot = $bot; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return BotMan |
50
|
|
|
*/ |
51
|
|
|
public function getBot() |
52
|
|
|
{ |
53
|
|
|
return $this->bot; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param string|Question $question |
58
|
|
|
* @param array|Closure $next |
59
|
|
|
* @param array $additionalParameters |
60
|
|
|
* @return $this |
61
|
|
|
*/ |
62
|
|
|
public function ask($question, $next, $additionalParameters = []) |
63
|
|
|
{ |
64
|
|
|
$this->bot->reply($question, $additionalParameters); |
65
|
|
|
$this->bot->storeConversation($this, $next, $question, $additionalParameters); |
66
|
|
|
|
67
|
|
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param string|\BotMan\BotMan\Messages\Outgoing\Question $question |
72
|
|
|
* @param array|Closure $next |
73
|
|
|
* @param array|Closure $repeat |
74
|
|
|
* @param array $additionalParameters |
75
|
|
|
* @return $this |
76
|
|
|
*/ |
77
|
|
View Code Duplication |
public function askForImages($question, $next, $repeat = null, $additionalParameters = []) |
|
|
|
|
78
|
|
|
{ |
79
|
|
|
$additionalParameters['__getter'] = 'getImages'; |
80
|
|
|
$additionalParameters['__pattern'] = Image::PATTERN; |
81
|
|
|
$additionalParameters['__repeat'] = ! is_null($repeat) ? $this->bot->serializeClosure($repeat) : $repeat; |
|
|
|
|
82
|
|
|
|
83
|
|
|
return $this->ask($question, $next, $additionalParameters); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param string|\BotMan\BotMan\Messages\Outgoing\Question $question |
88
|
|
|
* @param array|Closure $next |
89
|
|
|
* @param array|Closure $repeat |
90
|
|
|
* @param array $additionalParameters |
91
|
|
|
* @return $this |
92
|
|
|
*/ |
93
|
|
View Code Duplication |
public function askForVideos($question, $next, $repeat = null, $additionalParameters = []) |
|
|
|
|
94
|
|
|
{ |
95
|
|
|
$additionalParameters['__getter'] = 'getVideos'; |
96
|
|
|
$additionalParameters['__pattern'] = Video::PATTERN; |
97
|
|
|
$additionalParameters['__repeat'] = ! is_null($repeat) ? $this->bot->serializeClosure($repeat) : $repeat; |
|
|
|
|
98
|
|
|
|
99
|
|
|
return $this->ask($question, $next, $additionalParameters); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param string|\BotMan\BotMan\Messages\Outgoing\Question $question |
104
|
|
|
* @param array|Closure $next |
105
|
|
|
* @param array|Closure $repeat |
106
|
|
|
* @param array $additionalParameters |
107
|
|
|
* @return $this |
108
|
|
|
*/ |
109
|
|
View Code Duplication |
public function askForAudio($question, $next, $repeat = null, $additionalParameters = []) |
|
|
|
|
110
|
|
|
{ |
111
|
|
|
$additionalParameters['__getter'] = 'getAudio'; |
112
|
|
|
$additionalParameters['__pattern'] = Audio::PATTERN; |
113
|
|
|
$additionalParameters['__repeat'] = ! is_null($repeat) ? $this->bot->serializeClosure($repeat) : $repeat; |
|
|
|
|
114
|
|
|
|
115
|
|
|
return $this->ask($question, $next, $additionalParameters); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param string|\BotMan\BotMan\Messages\Outgoing\Question $question |
120
|
|
|
* @param array|Closure $next |
121
|
|
|
* @param array|Closure $repeat |
122
|
|
|
* @param array $additionalParameters |
123
|
|
|
* @return $this |
124
|
|
|
*/ |
125
|
|
View Code Duplication |
public function askForLocation($question, $next, $repeat = null, $additionalParameters = []) |
|
|
|
|
126
|
|
|
{ |
127
|
|
|
$additionalParameters['__getter'] = 'getLocation'; |
128
|
|
|
$additionalParameters['__pattern'] = Location::PATTERN; |
129
|
|
|
$additionalParameters['__repeat'] = ! is_null($repeat) ? $this->bot->serializeClosure($repeat) : $repeat; |
|
|
|
|
130
|
|
|
|
131
|
|
|
return $this->ask($question, $next, $additionalParameters); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Repeat the previously asked question. |
136
|
|
|
* @param string|Question $question |
137
|
|
|
*/ |
138
|
|
|
public function repeat($question = '') |
139
|
|
|
{ |
140
|
|
|
$conversation = $this->bot->getStoredConversation(); |
141
|
|
|
|
142
|
|
|
if (! $question instanceof Question && ! $question) { |
143
|
|
|
$question = unserialize($conversation['question']); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
$next = $conversation['next']; |
147
|
|
|
$additionalParameters = unserialize($conversation['additionalParameters']); |
148
|
|
|
|
149
|
|
|
if (is_string($next)) { |
150
|
|
|
$next = unserialize($next)->getClosure(); |
151
|
|
|
} elseif (is_array($next)) { |
152
|
|
|
$next = Collection::make($next)->map(function ($callback) { |
153
|
|
|
if ($this->bot->getDriver()->serializesCallbacks() && ! $this->bot->runsOnSocket()) { |
154
|
|
|
$callback['callback'] = unserialize($callback['callback'])->getClosure(); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return $callback; |
158
|
|
|
})->toArray(); |
159
|
|
|
} |
160
|
|
|
$this->ask($question, $next, $additionalParameters); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @param string|\BotMan\BotMan\Messages\Outgoing\Question $message |
165
|
|
|
* @param array $additionalParameters |
166
|
|
|
* @return $this |
167
|
|
|
*/ |
168
|
|
|
public function say($message, $additionalParameters = []) |
169
|
|
|
{ |
170
|
|
|
$this->bot->reply($message, $additionalParameters); |
171
|
|
|
|
172
|
|
|
return $this; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Should the conversation be skipped (temporarily). |
177
|
|
|
* @param IncomingMessage $message |
178
|
|
|
* @return bool |
179
|
|
|
*/ |
180
|
|
|
public function skipsConversation(IncomingMessage $message) |
|
|
|
|
181
|
|
|
{ |
182
|
|
|
// |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Should the conversation be removed and stopped (permanently). |
187
|
|
|
* @param IncomingMessage $message |
188
|
|
|
* @return bool |
189
|
|
|
*/ |
190
|
|
|
public function stopsConversation(IncomingMessage $message) |
|
|
|
|
191
|
|
|
{ |
192
|
|
|
// |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Override default conversation cache time (only for this conversation). |
197
|
|
|
* @return mixed |
198
|
|
|
*/ |
199
|
|
|
public function getConversationCacheTime() |
200
|
|
|
{ |
201
|
|
|
return $this->cacheTime ?? null; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @return mixed |
206
|
|
|
*/ |
207
|
|
|
abstract public function run(); |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @return array |
211
|
|
|
*/ |
212
|
|
|
public function __sleep() |
213
|
|
|
{ |
214
|
|
|
$properties = get_object_vars($this); |
215
|
|
|
if (! $this instanceof ShouldQueue) { |
216
|
|
|
unset($properties['bot']); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
return array_keys($properties); |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.