1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tgallice\FBMessenger; |
4
|
|
|
|
5
|
|
|
use Tgallice\FBMessenger\Exception\ApiException; |
6
|
|
|
use Tgallice\FBMessenger\Model\Attachment; |
7
|
|
|
use Tgallice\FBMessenger\Model\Attachment\Template; |
8
|
|
|
use Tgallice\FBMessenger\Model\Message; |
9
|
|
|
use Tgallice\FBMessenger\Model\MessageResponse; |
10
|
|
|
use Tgallice\FBMessenger\Model\ThreadSetting; |
11
|
|
|
use Tgallice\FBMessenger\Model\ThreadSetting\GreetingText; |
12
|
|
|
use Tgallice\FBMessenger\Model\ThreadSetting\StartedButton; |
13
|
|
|
use Tgallice\FBMessenger\Model\ThreadSetting\MenuItem; |
14
|
|
|
use Tgallice\FBMessenger\Model\UserProfile; |
15
|
|
|
|
16
|
|
|
class Messenger |
17
|
|
|
{ |
18
|
|
|
use ResponseHandler; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var Client |
22
|
|
|
*/ |
23
|
|
|
private $client; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param string $token |
|
|
|
|
27
|
|
|
* @param Client $client |
28
|
|
|
*/ |
29
|
14 |
|
public function __construct(Client $client) |
30
|
|
|
{ |
31
|
14 |
|
$this->client = $client; |
32
|
14 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param string $recipient |
36
|
|
|
* @param string|Message|Attachment|Template $message |
37
|
|
|
* @param string $notificationType |
38
|
|
|
* |
39
|
|
|
* @return MessageResponse |
40
|
|
|
* |
41
|
|
|
* @throws ApiException |
42
|
|
|
*/ |
43
|
5 |
|
public function sendMessage($recipient, $message, $notificationType = NotificationType::REGULAR) |
44
|
|
|
{ |
45
|
5 |
|
$message = $this->createMessage($message); |
46
|
4 |
|
$options = RequestOptionsFactory::createForMessage($recipient, $message, $notificationType); |
47
|
4 |
|
$response = $this->client->send('POST', '/me/messages', null, [], [], $options); |
48
|
4 |
|
$responseData = $this->decodeResponse($response); |
49
|
|
|
|
50
|
4 |
|
return new MessageResponse($responseData['recipient_id'], $responseData['message_id']); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param string $userId |
55
|
|
|
* @param array $fields |
56
|
|
|
* |
57
|
|
|
* @return UserProfile |
58
|
|
|
*/ |
59
|
1 |
|
public function getUserProfile( |
60
|
|
|
$userId, |
61
|
|
|
array $fields = [ |
62
|
|
|
UserProfile::FIRST_NAME, |
63
|
|
|
UserProfile::LAST_NAME, |
64
|
|
|
UserProfile::PROFILE_PIC, |
65
|
|
|
UserProfile::LOCALE, |
66
|
|
|
UserProfile::TIMEZONE, |
67
|
|
|
UserProfile::GENDER, |
68
|
|
|
] |
69
|
|
|
) { |
70
|
|
|
$query = [ |
71
|
1 |
|
'fields' => implode(',', $fields) |
72
|
1 |
|
]; |
73
|
|
|
|
74
|
1 |
|
$response = $this->client->get(sprintf('/%s', $userId), $query); |
75
|
1 |
|
$data = $this->decodeResponse($response); |
76
|
|
|
|
77
|
1 |
|
return UserProfile::create($data); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Subscribe the app to the page |
82
|
|
|
* |
83
|
|
|
* @return bool |
84
|
|
|
*/ |
85
|
1 |
|
public function subscribe() |
86
|
|
|
{ |
87
|
1 |
|
$response = $this->client->post('/me/subscribed_apps'); |
88
|
1 |
|
$decoded = $this->decodeResponse($response); |
89
|
|
|
|
90
|
1 |
|
return $decoded['success']; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param $text |
95
|
|
|
*/ |
96
|
1 |
|
public function setGreetingText($text) |
97
|
|
|
{ |
98
|
1 |
|
$greeting = new GreetingText($text); |
99
|
1 |
|
$setting = $this->buildSetting(ThreadSetting::TYPE_GREETING, null, $greeting); |
100
|
|
|
|
101
|
1 |
|
$this->postThreadSettings($setting); |
102
|
1 |
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param string $payload |
106
|
|
|
*/ |
107
|
1 |
|
public function setStartedButton($payload) |
108
|
|
|
{ |
109
|
1 |
|
$startedButton = new StartedButton($payload); |
110
|
1 |
|
$setting = $this->buildSetting( |
111
|
1 |
|
ThreadSetting::TYPE_CALL_TO_ACTIONS, |
112
|
1 |
|
ThreadSetting::NEW_THREAD, |
113
|
1 |
|
[$startedButton] |
114
|
1 |
|
); |
115
|
|
|
|
116
|
1 |
|
$this->postThreadSettings($setting); |
117
|
1 |
|
} |
118
|
|
|
|
119
|
1 |
|
public function deleteStartedButton() |
120
|
|
|
{ |
121
|
1 |
|
$setting = $this->buildSetting( |
122
|
1 |
|
ThreadSetting::TYPE_CALL_TO_ACTIONS, |
123
|
|
|
ThreadSetting::NEW_THREAD |
124
|
1 |
|
); |
125
|
|
|
|
126
|
1 |
|
$this->deleteThreadSettings($setting); |
127
|
1 |
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param MenuItem[] $menuItems |
131
|
|
|
*/ |
132
|
2 |
|
public function setPersistentMenu(array $menuItems) |
133
|
|
|
{ |
134
|
2 |
|
if (count($menuItems) > 5) { |
135
|
1 |
|
throw new \InvalidArgumentException('You should not set more than 5 menu items.'); |
136
|
|
|
} |
137
|
|
|
|
138
|
1 |
|
$setting = $this->buildSetting( |
139
|
1 |
|
ThreadSetting::TYPE_CALL_TO_ACTIONS, |
140
|
1 |
|
ThreadSetting::EXISTING_THREAD, |
141
|
|
|
$menuItems |
142
|
1 |
|
); |
143
|
|
|
|
144
|
1 |
|
$this->postThreadSettings($setting); |
145
|
1 |
|
} |
146
|
|
|
|
147
|
1 |
|
public function deletePersistentMenu() |
148
|
|
|
{ |
149
|
1 |
|
$setting = $this->buildSetting( |
150
|
1 |
|
ThreadSetting::TYPE_CALL_TO_ACTIONS, |
151
|
|
|
ThreadSetting::EXISTING_THREAD |
152
|
1 |
|
); |
153
|
|
|
|
154
|
1 |
|
$this->deleteThreadSettings($setting); |
155
|
1 |
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Messenger Factory |
159
|
|
|
* |
160
|
|
|
* @param string $token |
161
|
|
|
* |
162
|
|
|
* @return Messenger |
163
|
|
|
*/ |
164
|
|
|
public static function create($token) |
165
|
|
|
{ |
166
|
|
|
$client = new Client($token); |
167
|
|
|
|
168
|
|
|
return new self($client); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @param array $setting |
174
|
|
|
*/ |
175
|
3 |
|
private function postThreadSettings(array $setting) |
176
|
|
|
{ |
177
|
3 |
|
$this->client->post('/me/thread_settings', json_encode($setting)); |
178
|
3 |
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @param array $setting |
182
|
|
|
*/ |
183
|
2 |
|
private function deleteThreadSettings(array $setting) |
184
|
|
|
{ |
185
|
2 |
|
$this->client->send('DELETE', '/me/thread_settings', json_encode($setting)); |
186
|
2 |
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @param string $type |
190
|
|
|
* @param null|string $threadState |
191
|
|
|
* @param mixed $value |
192
|
|
|
* |
193
|
|
|
* @return array |
194
|
|
|
*/ |
195
|
5 |
|
private function buildSetting($type, $threadState = null, $value = null) |
196
|
|
|
{ |
197
|
|
|
$setting = [ |
198
|
5 |
|
'setting_type' => $type, |
199
|
5 |
|
]; |
200
|
|
|
|
201
|
5 |
|
if ($threadState) { |
|
|
|
|
202
|
4 |
|
$setting['thread_state'] = $threadState; |
203
|
4 |
|
} |
204
|
|
|
|
205
|
5 |
|
if (!empty($value)) { |
206
|
3 |
|
$setting[$type] = $value; |
207
|
3 |
|
} |
208
|
|
|
|
209
|
5 |
|
return $setting; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @param string|Message|Attachment|Template $message |
214
|
|
|
* |
215
|
|
|
* @return Message |
216
|
|
|
*/ |
217
|
5 |
|
private function createMessage($message) |
218
|
|
|
{ |
219
|
5 |
|
if ($message instanceof Message) { |
220
|
1 |
|
return $message; |
221
|
|
|
} |
222
|
|
|
|
223
|
4 |
|
if ($message instanceof Template) { |
224
|
1 |
|
$message = new Attachment(Attachment::TYPE_TEMPLATE, $message); |
|
|
|
|
225
|
1 |
|
} |
226
|
|
|
|
227
|
4 |
|
if (is_string($message) || $message instanceof Attachment) { |
228
|
3 |
|
return new Message($message); |
229
|
|
|
} |
230
|
|
|
|
231
|
1 |
|
throw new \InvalidArgumentException('$message should be a string, Message, Attachment or Template'); |
232
|
|
|
} |
233
|
|
|
} |
234
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.