|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ElfSundae\BearyChat; |
|
4
|
|
|
|
|
5
|
|
|
use JsonSerializable; |
|
6
|
|
|
use GuzzleHttp\Client as HttpClient; |
|
7
|
|
|
|
|
8
|
|
|
class Client |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* The BearyChat incoming webhook. |
|
12
|
|
|
* |
|
13
|
|
|
* @var string |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $webhook; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* The default fields for messages. |
|
19
|
|
|
* |
|
20
|
|
|
* @var array |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $messageDefaults = []; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* The Guzzle http client. |
|
26
|
|
|
* |
|
27
|
|
|
* @var \GuzzleHttp\Client |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $httpClient; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Create a new Client. |
|
33
|
|
|
* |
|
34
|
|
|
* @param string $webhook |
|
35
|
|
|
* @param array $messageDefaults See `\ElfSundae\BearyChat\MessageDefaults` |
|
36
|
|
|
* @param \GuzzleHttp\Client $httpClient |
|
37
|
|
|
*/ |
|
38
|
5 |
|
public function __construct($webhook = null, $messageDefaults = [], $httpClient = null) |
|
39
|
|
|
{ |
|
40
|
5 |
|
$this->webhook($webhook) |
|
41
|
5 |
|
->messageDefaults($messageDefaults); |
|
42
|
5 |
|
$this->httpClient = $httpClient; |
|
43
|
5 |
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Get the webhook. |
|
47
|
|
|
* |
|
48
|
|
|
* @return string |
|
49
|
|
|
*/ |
|
50
|
1 |
|
public function getWebhook() |
|
51
|
|
|
{ |
|
52
|
1 |
|
return $this->webhook; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Set the webhook. |
|
57
|
|
|
* |
|
58
|
|
|
* @param string $webhook |
|
59
|
|
|
* @return $this |
|
60
|
|
|
*/ |
|
61
|
5 |
|
public function setWebhook($webhook) |
|
62
|
|
|
{ |
|
63
|
5 |
|
$this->webhook = $webhook; |
|
64
|
|
|
|
|
65
|
5 |
|
return $this; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Change the webhook URL. |
|
70
|
|
|
* |
|
71
|
|
|
* @param string $webhook |
|
72
|
|
|
* @return $this |
|
73
|
|
|
*/ |
|
74
|
5 |
|
public function webhook($webhook) |
|
75
|
|
|
{ |
|
76
|
5 |
|
return $this->setWebhook($webhook); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Retrieve message defaults. |
|
81
|
|
|
* |
|
82
|
|
|
* @param string|null $key |
|
83
|
|
|
* @return mixed |
|
84
|
|
|
*/ |
|
85
|
3 |
|
public function getMessageDefaults($key = null) |
|
86
|
|
|
{ |
|
87
|
3 |
|
if (is_null($key)) { |
|
88
|
3 |
|
return $this->messageDefaults; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
1 |
|
if (isset($this->messageDefaults[$key])) { |
|
92
|
1 |
|
return $this->messageDefaults[$key]; |
|
93
|
|
|
} |
|
94
|
1 |
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Set the message defaults. |
|
98
|
|
|
* |
|
99
|
|
|
* @param array $defaults |
|
100
|
|
|
* @return $this |
|
101
|
|
|
*/ |
|
102
|
5 |
|
public function setMessageDefaults($defaults) |
|
103
|
|
|
{ |
|
104
|
5 |
|
$this->messageDefaults = (array) $defaults; |
|
105
|
|
|
|
|
106
|
5 |
|
return $this; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Set the message defaults. |
|
111
|
|
|
* |
|
112
|
|
|
* @param array $defaults |
|
113
|
|
|
* @return $this |
|
114
|
|
|
*/ |
|
115
|
5 |
|
public function messageDefaults($defaults) |
|
116
|
|
|
{ |
|
117
|
5 |
|
return $this->setMessageDefaults($defaults); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Create a new Message instance. |
|
122
|
|
|
* |
|
123
|
|
|
* @return \ElfSundae\BearyChat\Message |
|
124
|
|
|
*/ |
|
125
|
1 |
|
public function createMessage() |
|
126
|
|
|
{ |
|
127
|
1 |
|
return new Message($this); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Send message to the BearyChat. |
|
132
|
|
|
* |
|
133
|
|
|
* @param mixed $message A JSON string, or any arrayable object. |
|
134
|
|
|
* @return bool |
|
135
|
|
|
*/ |
|
136
|
|
|
public function sendMessage($message) |
|
137
|
|
|
{ |
|
138
|
|
|
if ($payload = $this->getPayload($message)) { |
|
139
|
|
|
$response = $this->getHttpClient() |
|
140
|
|
|
->post($this->getWebhook(), [ |
|
141
|
|
|
'headers' => ['Content-Type' => 'application/json'], |
|
142
|
|
|
'body' => $payload, |
|
143
|
|
|
]); |
|
144
|
|
|
|
|
145
|
|
|
return 200 === $response->getStatusCode(); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
return false; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Get the payload from an object. |
|
153
|
|
|
* |
|
154
|
|
|
* @param mixed $message |
|
155
|
|
|
* @return string |
|
156
|
|
|
*/ |
|
157
|
|
|
protected function getPayload($message) |
|
158
|
|
|
{ |
|
159
|
|
|
if ($message instanceof JsonSerializable) { |
|
160
|
|
|
return json_encode($message); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
if (is_object($message)) { |
|
164
|
|
|
if (method_exists($message, 'toJson')) { |
|
165
|
|
|
return $message->toJson(); |
|
166
|
|
|
} elseif (method_exists($message, 'toArray')) { |
|
167
|
|
|
return json_encode($message->toArray()); |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
if (is_string($message) && is_array(json_decode($message))) { |
|
172
|
|
|
return $message; |
|
173
|
|
|
} |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Get the http client. |
|
178
|
|
|
* @return \GuzzleHttp\Client |
|
179
|
|
|
*/ |
|
180
|
|
|
protected function getHttpClient() |
|
181
|
|
|
{ |
|
182
|
|
|
if (! $this->httpClient instanceof HttpClient) { |
|
183
|
|
|
$this->httpClient = new HttpClient; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
return $this->httpClient; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* Any unhandled methods will be sent to a new Message instance. |
|
191
|
|
|
* |
|
192
|
|
|
* @param string $name |
|
193
|
|
|
* @param array $args |
|
194
|
|
|
* @return mixed |
|
195
|
|
|
*/ |
|
196
|
|
|
public function __call($name, $args) |
|
197
|
|
|
{ |
|
198
|
|
|
$message = $this->createMessage(); |
|
199
|
|
|
|
|
200
|
|
|
return call_user_func_array([$message, $name], $args); |
|
201
|
|
|
} |
|
202
|
|
|
} |
|
203
|
|
|
|