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