/
pushover.R
187 lines (165 loc) · 5.83 KB
/
pushover.R
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
#' Send a message using Pushover
#'
#' `pushover()` sends a message (push notification) to a user or group.
#' Messages can be given different priorities, play different sounds, or require
#' acknowledgments.
#' `pushover_normal()`, `pushover_silent`, `pushover_quiet`, `pushover_high`,
#' and `pushover_emergency` functions send messages with those priorities.
#'
#' @param message The message to be sent (max. 1024 characters). Messages use
#' [glue::glue()] for formatting and interpolation.
#' @param title (optional) The message's title. Titles use [glue::glue()] for
#' formatting and interpolation.
#' @param priority Message priority (`-2`: silent, `-1`: quiet, `0`: normal
#' (default), `1`: high, `2`: emergency)
#' @param attachment Path of file attachment to include. File must be image
#' format (bmp, jpg, png, tif) and no larger than 2.6 MB.
#' @param user user/group key (see [`set_pushover_user()`])
#' @param app application token (see [`set_pushover_app()`])
#' @param device (optional) name of the device(s) to send message to. Defaults
#' to all devices.
#' @param sound (optional) name of the sound to play (see
#' <https://pushover.net/api#sounds>)
#' @param url (optional) supplementary URL to display with message
#' @param url_title (optional) title to show for supplementary URL
#' @param format Message formatting. If `html` (default), messages can include a
#' [limited subset](https://pushover.net/api#html) of HTML formatting. If
#' `monospace`, text is formatted using monospace font.
#' @param retry (optional) how often (in seconds) to repeat emergency priority
#' messages (min: 30 seconds; default: 60 seconds)
#' @param expire (optional) how long (in seconds) emergency priority messages
#' will be retried (max: 86400 seconds; default: 3600 seconds)
#' @param callback (optional) callback URL to be visited (HTTP POST) once an
#' emergency priority message has been acknowledged
#' ([details](https://pushover.net/api#receipt))
#' @param timestamp (optional) a Unix timestamp containing the date and time to
#' display to the user instead of the time at which the message was received
#'
#' @return an invisible list containing the following fields:
#' \itemize{
#' \item `status`: request status (1 = success)
#' \item `request`: unique request ID
#' \item `raw`: the raw [httr::response] object
#' \item `receipt`: a receipt ID (only for emergency priority messages)
#' \item `errors`: a list of error messages (only for unsuccessful requests)
#' }
#' @export
#'
#' @examples
#' \dontrun{
#' pushover(message = "Hola Mundo!")
#' }
pushover <- function(message,
title = NULL,
priority = 0,
attachment = NULL,
user = get_pushover_user(),
app = get_pushover_app(),
device = NULL,
sound = NULL,
url = NULL,
url_title = NULL,
format = c("html", "monospace"),
retry = 60,
expire = 3600,
callback = NULL,
timestamp = NULL) {
checkmate::assert_string(message, min.chars = 1)
checkmate::assert_true(nchar(message) <= 1024)
format <- arg_match(format)
checkmate::assert(retry <= expire)
params <- list(
"token" = assert_valid_app(app),
"user" = assert_valid_user(user),
"message" = glue(message),
"priority" = checkmate::assert_choice(priority, -2:2),
"retry" = checkmate::assert_integerish(
retry,
lower = 30,
any.missing = FALSE
),
"expire" = checkmate::assert_integerish(
expire,
upper = 10800,
any.missing = FALSE
)
)
if (!is.null(attachment)) {
checkmate::assert_file_exists(
attachment,
access = "r",
extension = c("bmp", "gif", "jpg", "jpeg", "png", "tif", "tiff")
)
checkmate::check_true(file.size(attachment) <= 2621440)
params$attachment <- httr::upload_file(attachment)
}
if (format == "html") {
params$html <- 1
} else if (format == "monospace") {
params$monospace <- 1
}
if (!is.null(device)) {
# TODO: handle >1 device
assert_valid_device(device)
params["device"] <- paste0(device, collapse = ",")
}
if (!is.null(title)) {
checkmate::assert_string(title, min.chars = 1)
checkmate::assert_true(nchar(title) <= 250)
params$title <- glue(title)
}
if (!is.null(url)) {
checkmate::assert_string(url, min.chars = 1)
checkmate::assert_true(nchar(url) <= 512)
params$url <- url
}
if (!is.null(url_title)) {
checkmate::assert_string(url_title, min.chars = 1)
checkmate::assert_true(nchar(url_title) <= 100)
params$url_title <- url_title
}
if (!is.null(timestamp)) {
params$timestamp <- checkmate::assert_count(timestamp)
}
if (!is.null(sound)) {
params$sound <- assert_valid_sound(sound)
}
if (!is.null(callback)) {
# Docs don't say what character limit is
checkmate::assert_string(callback, min.chars = 1)
params$callback <- callback
}
invisible(
pushover_api(
verb = "POST",
url = "https://api.pushover.net/1/messages.json",
body = params
)
)
}
#' @rdname pushover
#' @param ... Additional arguments to pass to `pushover()`
#' @export
pushover_silent <- function(message, ...) {
pushover(message = message, priority = -2, ...)
}
#' @rdname pushover
#' @export
pushover_quiet <- function(message, ...) {
pushover(message = message, priority = -1, ...)
}
#' @rdname pushover
#' @export
pushover_normal <- function(message, ...) {
pushover(message = message, priority = 0, ...)
}
#' @rdname pushover
#' @export
pushover_high <- function(message, ...) {
pushover(message = message, priority = 1, ...)
}
#' @rdname pushover
#' @export
pushover_emergency <- function(message, ...) {
pushover(message = message, priority = 2, ...)
}