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