|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Kerox\Messenger\Api; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\ClientInterface; |
|
6
|
|
|
use Kerox\Messenger\Model\Message; |
|
7
|
|
|
use Kerox\Messenger\Model\Message\Attachment; |
|
8
|
|
|
use Kerox\Messenger\Request\SendRequest; |
|
9
|
|
|
use Kerox\Messenger\Response\SendResponse; |
|
10
|
|
|
|
|
11
|
|
|
class Send extends AbstractApi |
|
12
|
|
|
{ |
|
13
|
|
|
|
|
14
|
|
|
const SENDER_ACTION_TYPING_ON = 'typing_on'; |
|
15
|
|
|
const SENDER_ACTION_TYPING_OFF = 'typing_off'; |
|
16
|
|
|
const SENDER_ACTION_MARK_SEEN = 'mark_seen'; |
|
17
|
|
|
|
|
18
|
|
|
const NOTIFICATION_TYPE_REGULAR = 'REGULAR'; |
|
19
|
|
|
const NOTIFICATION_TYPE_SILENT_PUSH = 'SILENT_PUSH'; |
|
20
|
|
|
const NOTIFICATION_TYPE_NO_PUSH = 'NO_PUSH'; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var null|\Kerox\Messenger\Api\Send |
|
24
|
|
|
*/ |
|
25
|
|
|
private static $_instance; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Send constructor. |
|
29
|
|
|
* |
|
30
|
|
|
* @param string $pageToken |
|
31
|
|
|
* @param \GuzzleHttp\ClientInterface $client |
|
32
|
|
|
*/ |
|
33
|
9 |
|
public function __construct(string $pageToken, ClientInterface $client) |
|
34
|
|
|
{ |
|
35
|
9 |
|
parent::__construct($pageToken, $client); |
|
36
|
9 |
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param string $pageToken |
|
40
|
|
|
* @param \GuzzleHttp\ClientInterface $client |
|
41
|
|
|
* @return \Kerox\Messenger\Api\Send |
|
42
|
|
|
*/ |
|
43
|
1 |
|
public static function getInstance(string $pageToken, ClientInterface $client): Send |
|
44
|
|
|
{ |
|
45
|
1 |
|
if (self::$_instance === null) { |
|
46
|
1 |
|
self::$_instance = new Send($pageToken, $client); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
1 |
|
return self::$_instance; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param string $recipient |
|
54
|
|
|
* @param $message |
|
55
|
|
|
* @param string $notificationType |
|
56
|
|
|
* @return \Kerox\Messenger\Response\SendResponse |
|
57
|
|
|
*/ |
|
58
|
5 |
|
public function message(string $recipient, $message, string $notificationType = self::NOTIFICATION_TYPE_REGULAR): SendResponse |
|
59
|
|
|
{ |
|
60
|
5 |
|
$message = $this->isValidMessage($message); |
|
61
|
4 |
|
$this->isValidNotificationType($notificationType); |
|
62
|
|
|
|
|
63
|
3 |
|
$request = new SendRequest($this->pageToken, $message, $recipient, $notificationType); |
|
64
|
3 |
|
$response = $this->client->post('me/messages', $request->build()); |
|
65
|
|
|
|
|
66
|
3 |
|
return new SendResponse($response); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @deprecated since 1.2.0 and will be remove in 1.3.0. |
|
71
|
|
|
* @see message() |
|
72
|
|
|
* @param string $recipient |
|
73
|
|
|
* @param $message |
|
74
|
|
|
* @param string $notificationType |
|
75
|
|
|
* @return \Kerox\Messenger\Response\SendResponse |
|
76
|
|
|
*/ |
|
77
|
|
|
public function sendMessage(string $recipient, $message, string $notificationType = self::NOTIFICATION_TYPE_REGULAR): SendResponse |
|
78
|
|
|
{ |
|
79
|
|
|
return $this->message($recipient, $message, $notificationType); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param string $recipient |
|
84
|
|
|
* @param string $action |
|
85
|
|
|
* @param string $notificationType |
|
86
|
|
|
* @return \Kerox\Messenger\Response\SendResponse |
|
87
|
|
|
*/ |
|
88
|
2 |
|
public function action(string $recipient, string $action, string $notificationType = self::NOTIFICATION_TYPE_REGULAR): SendResponse |
|
89
|
|
|
{ |
|
90
|
2 |
|
$this->isValidAction($action); |
|
91
|
1 |
|
$this->isValidNotificationType($notificationType); |
|
92
|
|
|
|
|
93
|
1 |
|
$request = new SendRequest($this->pageToken, $action, $recipient, $notificationType, SendRequest::TYPE_ACTION); |
|
94
|
1 |
|
$response = $this->client->post('me/messages', $request->build()); |
|
95
|
|
|
|
|
96
|
1 |
|
return new SendResponse($response); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @deprecated since 1.2.0 and will be removed in 1.3.0. |
|
101
|
|
|
* @see action() |
|
102
|
|
|
* @param string $recipient |
|
103
|
|
|
* @param string $action |
|
104
|
|
|
* @param string $notificationType |
|
105
|
|
|
* @return \Kerox\Messenger\Response\SendResponse |
|
106
|
|
|
*/ |
|
107
|
|
|
public function sendAction(string $recipient, string $action, string $notificationType = self::NOTIFICATION_TYPE_REGULAR): SendResponse |
|
108
|
|
|
{ |
|
109
|
|
|
return $this->action($recipient, $action, $notificationType); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @param \Kerox\Messenger\Model\Message\Attachment $attachment |
|
114
|
|
|
* @return \Kerox\Messenger\Response\SendResponse |
|
115
|
|
|
*/ |
|
116
|
1 |
|
public function attachment(Attachment $attachment): SendResponse |
|
117
|
|
|
{ |
|
118
|
1 |
|
$message = $this->isValidMessage($attachment); |
|
119
|
|
|
|
|
120
|
1 |
|
$request = new SendRequest($this->pageToken, $message); |
|
121
|
1 |
|
$response = $this->client->post('me/message_attachments', $request->build()); |
|
122
|
|
|
|
|
123
|
1 |
|
return new SendResponse($response); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @param $message |
|
128
|
|
|
* @return \Kerox\Messenger\Model\Message |
|
129
|
|
|
* @throws \InvalidArgumentException |
|
130
|
|
|
*/ |
|
131
|
6 |
|
private function isValidMessage($message): Message |
|
132
|
|
|
{ |
|
133
|
6 |
|
if ($message instanceof Message) { |
|
134
|
1 |
|
return $message; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
5 |
|
if (is_string($message) || $message instanceof Attachment) { |
|
138
|
4 |
|
return new Message($message); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
1 |
|
throw new \InvalidArgumentException('$message must be a string or an instance of Message or Attachment'); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* @param string $notificationType |
|
146
|
|
|
*/ |
|
147
|
5 |
|
private function isValidNotificationType(string $notificationType) |
|
148
|
|
|
{ |
|
149
|
5 |
|
$allowedNotificationType = $this->getAllowedNotificationType(); |
|
150
|
5 |
|
if (!in_array($notificationType, $allowedNotificationType)) { |
|
151
|
1 |
|
throw new \InvalidArgumentException('$notificationType must be either ' . implode(', ', $allowedNotificationType)); |
|
152
|
|
|
} |
|
153
|
4 |
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @return array |
|
157
|
|
|
*/ |
|
158
|
5 |
|
private function getAllowedNotificationType(): array |
|
159
|
|
|
{ |
|
160
|
|
|
return [ |
|
161
|
5 |
|
self::NOTIFICATION_TYPE_REGULAR, |
|
162
|
5 |
|
self::NOTIFICATION_TYPE_SILENT_PUSH, |
|
163
|
5 |
|
self::NOTIFICATION_TYPE_NO_PUSH, |
|
164
|
|
|
]; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* @param string $action |
|
169
|
|
|
*/ |
|
170
|
2 |
|
private function isValidAction(string $action) |
|
171
|
|
|
{ |
|
172
|
2 |
|
$allowedSenderAction = $this->getAllowedSenderAction(); |
|
173
|
2 |
|
if (!in_array($action, $allowedSenderAction)) { |
|
174
|
1 |
|
throw new \InvalidArgumentException('$action must be either ' . implode(', ', $allowedSenderAction)); |
|
175
|
|
|
} |
|
176
|
1 |
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* @return array |
|
180
|
|
|
*/ |
|
181
|
2 |
|
private function getAllowedSenderAction(): array |
|
182
|
|
|
{ |
|
183
|
|
|
return [ |
|
184
|
2 |
|
self::SENDER_ACTION_TYPING_ON, |
|
185
|
2 |
|
self::SENDER_ACTION_TYPING_OFF, |
|
186
|
2 |
|
self::SENDER_ACTION_MARK_SEEN, |
|
187
|
|
|
]; |
|
188
|
|
|
} |
|
189
|
|
|
} |
|
190
|
|
|
|