|
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
|
|
|
* @param \GuzzleHttp\Client $client |
|
26
|
|
|
*/ |
|
27
|
|
|
public function __construct(string $pageToken, Client $client) |
|
28
|
|
|
{ |
|
29
|
|
|
parent::__construct($pageToken, $client); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param string $recipient |
|
34
|
|
|
* @param $message |
|
35
|
|
|
* @param string $notificationType |
|
36
|
|
|
* @return \Kerox\Messenger\Response\SendResponse |
|
37
|
|
|
*/ |
|
38
|
|
|
public function message(string $recipient, $message, string $notificationType = self::NOTIFICATION_TYPE_REGULAR): SendResponse |
|
39
|
|
|
{ |
|
40
|
|
|
$message = $this->isValidMessage($message); |
|
41
|
|
|
$this->isValidNotificationType($notificationType); |
|
42
|
|
|
|
|
43
|
|
|
$request = new SendRequest($this->pageToken, $recipient, $message, null, $notificationType); |
|
44
|
|
|
$response = $this->client->post('/me/messages', $request->build()); |
|
45
|
|
|
|
|
46
|
|
|
return new SendResponse($response); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param string $recipient |
|
51
|
|
|
* @param string $action |
|
52
|
|
|
* @param string $notificationType |
|
53
|
|
|
* @return \Kerox\Messenger\Response\SendResponse |
|
54
|
|
|
*/ |
|
55
|
|
|
public function action(string $recipient, string $action, string $notificationType = self::NOTIFICATION_TYPE_REGULAR): SendResponse |
|
56
|
|
|
{ |
|
57
|
|
|
$this->isValidAction($action); |
|
58
|
|
|
$this->isValidNotificationType($notificationType); |
|
59
|
|
|
|
|
60
|
|
|
$request = new SendRequest($this->pageToken, $recipient, null, $action, $notificationType); |
|
61
|
|
|
$response = $this->client->post('/me/messages', $request->build()); |
|
62
|
|
|
|
|
63
|
|
|
return new SendResponse($response); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param $message |
|
68
|
|
|
* @return \Kerox\Messenger\Model\Message |
|
69
|
|
|
* @throws \InvalidArgumentException |
|
70
|
|
|
*/ |
|
71
|
|
|
private function isValidMessage($message): Message |
|
72
|
|
|
{ |
|
73
|
|
|
if ($message instanceof Message) { |
|
74
|
|
|
return $message; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
if (is_string($message) || $message instanceof Attachment) { |
|
78
|
|
|
return new Message($message); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
throw new \InvalidArgumentException('$message must be a string or an instance of Message or Attachment'); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param string $notificationType |
|
86
|
|
|
*/ |
|
87
|
|
|
private function isValidNotificationType(string $notificationType) |
|
88
|
|
|
{ |
|
89
|
|
|
$allowedNotificationType = $this->getAllowedNotificationType(); |
|
90
|
|
|
if (!in_array($notificationType, $allowedNotificationType)) { |
|
91
|
|
|
throw new \InvalidArgumentException('$notificationType must be either ' . implode(', ', $allowedNotificationType)); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @return array |
|
97
|
|
|
*/ |
|
98
|
|
|
private function getAllowedNotificationType(): array |
|
99
|
|
|
{ |
|
100
|
|
|
return [ |
|
101
|
|
|
self::NOTIFICATION_TYPE_REGULAR, |
|
102
|
|
|
self::NOTIFICATION_TYPE_NO_PUSH, |
|
103
|
|
|
self::NOTIFICATION_TYPE_SILENT_PUSH, |
|
104
|
|
|
]; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @param string $action |
|
109
|
|
|
*/ |
|
110
|
|
|
private function isValidAction(string $action) |
|
111
|
|
|
{ |
|
112
|
|
|
$allowedSenderAction = $this->getAllowedSenderAction(); |
|
113
|
|
|
if (!in_array($action, $allowedSenderAction)) { |
|
114
|
|
|
throw new \InvalidArgumentException('$action must be either ' . implode(', ', $allowedSenderAction)); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @return array |
|
120
|
|
|
*/ |
|
121
|
|
|
private function getAllowedSenderAction(): array |
|
122
|
|
|
{ |
|
123
|
|
|
return [ |
|
124
|
|
|
self::SENDER_ACTION_TYPING_ON, |
|
125
|
|
|
self::SENDER_ACTION_TYPING_OFF, |
|
126
|
|
|
self::SENDER_ACTION_MARK_SEEN, |
|
127
|
|
|
]; |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|