-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMessage.php
More file actions
133 lines (114 loc) · 3.52 KB
/
Message.php
File metadata and controls
133 lines (114 loc) · 3.52 KB
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
<?php
declare(strict_types=1);
namespace Gotify\Endpoint;
use Gotify\Json;
use stdClass;
/**
* Class for interacting with Message API endpoint
*
* @see https://gotify.net/api-docs#/message API docs for message endpoint
*/
class Message extends AbstractEndpoint
{
/**
* High message priority
*
* @see https://github.com/gotify/android#message-priorities
*/
public const PRIORITY_HIGH = 8;
/**
* Default message priority
*
* @see https://github.com/gotify/android#message-priorities
*/
public const PRIORITY_DEFAULT = 4;
/**
* Low message priority
*
* @see https://github.com/gotify/android#message-priorities
*/
public const PRIORITY_LOW = 1;
/**
* Minimum message priority
*
* @see https://github.com/gotify/android#message-priorities
*/
public const PRIORITY_MIN = 0;
/** @var string $endpoint API endpoint */
private string $endpoint = 'message';
/**
* Get all messages (ordered by most recent)
*
* @param int $limit Maximum number of messages to return
* @param int $since Return all messages after a message id
*
* @return stdClass
*
* @see https://gotify.net/api-docs#/message/getMessages API docs for getting all messages
*/
public function getAll(int $limit = 100, int $since = 0): stdClass
{
$query = [
'limit' => $limit,
'since' => $since
];
$response = $this->guzzle->get($this->endpoint, $query);
$messages = Json::decode($response->getBody()->getContents());
return (object) $messages;
}
/**
* Create a message
*
* @param string $title Message title
* @param string $message Message body
* @param int $priority Message priority
* @param array<string, array<string, array<string, mixed>>> $extras Message extras
*
* @return stdClass
*
* @see https://gotify.net/docs/msgextras Documentation for message extras
* @see https://github.com/gotify/android#message-priorities Message priority levels
* @see https://gotify.net/api-docs#/message/createMessage API docs for creating a message
*/
public function create(string $title, string $message, int $priority = 0, array $extras = []): stdClass
{
$data = [
'title' => $title,
'message' => $message,
'priority' => $priority
];
if (empty($extras) === false) {
$data['extras'] = $extras;
}
$response = $this->guzzle->post($this->endpoint, $data);
$message = Json::decode($response->getBody()->getContents());
return (object) $message;
}
/**
* Delete a message
*
* @param int $id Message Id
* @return boolean
*
* @see https://gotify.net/api-docs#/message/deleteMessage API docs for deleting a message
*/
public function delete(int $id): bool
{
$response = $this->guzzle->delete($this->endpoint . '/' . $id);
$body = $response->getBody()->getContents();
return $response->getStatusCode() === 200 ? true : false;
}
/**
* Delete all messages
*
* @return boolean
*
* @see https://gotify.net/api-docs#/message/deleteMessages API docs for deleting all messages
*/
public function deleteAll(): bool
{
$response = $this->guzzle->delete($this->endpoint);
$body = $response->getBody()->getContents();
return $response->getStatusCode() === 200 ? true : false;
}
}