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, array $messageDefaults = [], $httpClient = null) |
39
|
|
|
{ |
40
|
5 |
|
$this->webhook = $webhook; |
41
|
5 |
|
$this->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
|
1 |
|
public function setWebhook($webhook) |
62
|
|
|
{ |
63
|
1 |
|
$this->webhook = $webhook; |
64
|
|
|
|
65
|
1 |
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Change the webhook URL. |
70
|
|
|
* |
71
|
|
|
* @param string $webhook |
72
|
|
|
*/ |
73
|
1 |
|
public function webhook($webhook) |
74
|
|
|
{ |
75
|
1 |
|
return $this->setWebhook($webhook); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Retrieve message defaults. |
80
|
|
|
* |
81
|
|
|
* @param string|null $option |
82
|
|
|
* @return mixed |
83
|
|
|
*/ |
84
|
3 |
|
public function getMessageDefaults($option = null) |
85
|
|
|
{ |
86
|
3 |
|
return is_null($option) ? |
87
|
3 |
|
$this->messageDefaults : |
88
|
3 |
|
(isset($this->messageDefaults[$option]) ? $this->messageDefaults[$option] : null); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Set the message defaults. |
93
|
|
|
* |
94
|
|
|
* @param array $messageDefaults |
95
|
|
|
*/ |
96
|
2 |
|
public function setMessageDefaults(array $messageDefaults) |
97
|
|
|
{ |
98
|
2 |
|
$this->messageDefaults = $messageDefaults; |
99
|
|
|
|
100
|
2 |
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Create a new Message instance. |
105
|
|
|
* |
106
|
|
|
* @return \ElfSundae\BearyChat\Message |
107
|
|
|
*/ |
108
|
1 |
|
public function createMessage() |
109
|
|
|
{ |
110
|
1 |
|
return new Message($this); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Send message to the BearyChat. |
115
|
|
|
* |
116
|
|
|
* @param mixed $message A JSON string, or any arrayable object. |
117
|
|
|
* @return bool |
118
|
|
|
*/ |
119
|
|
|
public function sendMessage($message) |
120
|
|
|
{ |
121
|
|
|
if ($payload = $this->getPayload($message)) { |
122
|
|
|
$response = $this->getHttpClient()->post($this->getWebhook(), [ |
123
|
|
|
'headers' => [ |
124
|
|
|
'Content-Type' => 'application/json', |
125
|
|
|
], |
126
|
|
|
'body' => $payload, |
127
|
|
|
]); |
128
|
|
|
|
129
|
|
|
return 200 === $response->getStatusCode(); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return false; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Get the payload from an object. |
137
|
|
|
* |
138
|
|
|
* @param mixed $message |
139
|
|
|
* @return string |
140
|
|
|
*/ |
141
|
|
|
protected function getPayload($message) |
142
|
|
|
{ |
143
|
|
|
if ($message instanceof JsonSerializable) { |
144
|
|
|
$message = json_encode($message); |
145
|
|
|
} elseif (is_object($message)) { |
146
|
|
|
if (method_exists($message, 'toJson')) { |
147
|
|
|
$message = $message->toJson(); |
148
|
|
|
} elseif (method_exists($message, 'toArray')) { |
149
|
|
|
$message = $message->toArray(); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
if ($message && ! is_string($message)) { |
154
|
|
|
$message = json_encode($message); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return $message; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Get the http client. |
162
|
|
|
* @return \GuzzleHttp\Client |
163
|
|
|
*/ |
164
|
|
|
protected function getHttpClient() |
165
|
|
|
{ |
166
|
|
|
if (! ($this->httpClient instanceof HttpClient)) { |
167
|
|
|
$this->httpClient = new HttpClient; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
return $this->httpClient; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Any unhandled methods will be sent to a new Message instance. |
175
|
|
|
* |
176
|
|
|
* @param string $name |
177
|
|
|
* @param array $args |
178
|
|
|
* @return mixed |
179
|
|
|
*/ |
180
|
|
|
public function __call($name, $args) |
181
|
|
|
{ |
182
|
|
|
$message = $this->createMessage(); |
183
|
|
|
|
184
|
|
|
return call_user_func_array([$message, $name], $args); |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|