1
|
|
|
<?php |
2
|
|
|
namespace Kerox\Messenger\Api; |
3
|
|
|
|
4
|
|
|
use GuzzleHttp\Client; |
5
|
|
|
use Kerox\Messenger\Model\Message; |
6
|
|
|
use Kerox\Messenger\Model\Message\Attachment; |
7
|
|
|
use Kerox\Messenger\Request\SendRequest; |
8
|
|
|
use Kerox\Messenger\Response\SendResponse; |
9
|
|
|
|
10
|
|
|
class Send extends AbstractApi |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
const SENDER_ACTION_TYPING_ON = 'typing_on'; |
14
|
|
|
const SENDER_ACTION_TYPING_OFF = 'typing_off'; |
15
|
|
|
const SENDER_ACTION_MARK_SEEN = 'mark_seen'; |
16
|
|
|
|
17
|
|
|
const NOTIFICATION_TYPE_REGULAR = 'REGULAR'; |
18
|
|
|
const NOTIFICATION_TYPE_SILENT_PUSH = 'SILENT_PUSH'; |
19
|
|
|
const NOTIFICATION_TYPE_NO_PUSH = 'NO_PUSH'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Send constructor. |
23
|
|
|
* |
24
|
|
|
* @param string $pageToken |
25
|
|
|
*/ |
26
|
|
|
public function __construct(string $pageToken) |
27
|
|
|
{ |
28
|
|
|
parent::__construct($pageToken); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param string $recipient |
33
|
|
|
* @param $message |
34
|
|
|
* @param string $notificationType |
35
|
|
|
* @return \Kerox\Messenger\Response\SendResponse |
36
|
|
|
*/ |
37
|
|
|
public function sendMessage(string $recipient, $message, string $notificationType = self::NOTIFICATION_TYPE_REGULAR): SendResponse |
38
|
|
|
{ |
39
|
|
|
$message = $this->isValidMessage($message); |
40
|
|
|
$this->isValidNotificationType($notificationType); |
41
|
|
|
|
42
|
|
|
$request = new SendRequest($this->pageToken, $recipient, $message, null, $notificationType); |
43
|
|
|
$response = $this->client->post('/me/messages', $request->build()); |
44
|
|
|
|
45
|
|
|
return new SendResponse($response); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param string $recipient |
50
|
|
|
* @param string $action |
51
|
|
|
* @param string $notificationType |
52
|
|
|
* @return \Kerox\Messenger\Response\SendResponse |
53
|
|
|
*/ |
54
|
|
|
public function sendAction(string $recipient, string $action, string $notificationType = self::NOTIFICATION_TYPE_REGULAR): SendResponse |
55
|
|
|
{ |
56
|
|
|
$this->isValidAction($action); |
57
|
|
|
$this->isValidNotificationType($notificationType); |
58
|
|
|
|
59
|
|
|
$request = new SendRequest($this->pageToken, $recipient, null, $action, $notificationType); |
60
|
|
|
$response = $this->client->post('/me/messages', $request->build()); |
61
|
|
|
|
62
|
|
|
return new SendResponse($response); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param $message |
67
|
|
|
* @return \Kerox\Messenger\Model\Message |
68
|
|
|
* @throws \InvalidArgumentException |
69
|
|
|
*/ |
70
|
|
|
private function isValidMessage($message): Message |
71
|
|
|
{ |
72
|
|
|
if ($message instanceof Message) { |
73
|
|
|
return $message; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
if (is_string($message) || $message instanceof Attachment) { |
77
|
|
|
return new Message($message); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
throw new \InvalidArgumentException('$message must be a string or an instance of Message or Attachment'); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param string $notificationType |
85
|
|
|
*/ |
86
|
|
|
private function isValidNotificationType(string $notificationType) |
87
|
|
|
{ |
88
|
|
|
$allowedNotificationType = $this->getAllowedNotificationType(); |
89
|
|
|
if (!in_array($notificationType, $allowedNotificationType)) { |
90
|
|
|
throw new \InvalidArgumentException('$notificationType must be either ' . implode(', ', $allowedNotificationType)); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return array |
96
|
|
|
*/ |
97
|
|
|
private function getAllowedNotificationType(): array |
98
|
|
|
{ |
99
|
|
|
return [ |
100
|
|
|
self::NOTIFICATION_TYPE_REGULAR, |
101
|
|
|
self::NOTIFICATION_TYPE_NO_PUSH, |
102
|
|
|
self::NOTIFICATION_TYPE_SILENT_PUSH, |
103
|
|
|
]; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param string $action |
108
|
|
|
*/ |
109
|
|
|
private function isValidAction(string $action) |
110
|
|
|
{ |
111
|
|
|
$allowedSenderAction = $this->getAllowedSenderAction(); |
112
|
|
|
if (!in_array($action, $allowedSenderAction)) { |
113
|
|
|
throw new \InvalidArgumentException('$action must be either ' . implode(', ', $allowedSenderAction)); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return array |
119
|
|
|
*/ |
120
|
|
|
private function getAllowedSenderAction(): array |
121
|
|
|
{ |
122
|
|
|
return [ |
123
|
|
|
self::SENDER_ACTION_TYPING_ON, |
124
|
|
|
self::SENDER_ACTION_TYPING_OFF, |
125
|
|
|
self::SENDER_ACTION_MARK_SEEN, |
126
|
|
|
]; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|