|
| 1 | +package no.nav.hjelpemidler |
| 2 | + |
| 3 | +import com.fasterxml.jackson.annotation.JsonValue |
| 4 | +import io.ktor.client.HttpClient |
| 5 | +import io.ktor.client.call.body |
| 6 | +import io.ktor.client.engine.cio.CIO |
| 7 | +import io.ktor.client.plugins.contentnegotiation.ContentNegotiation |
| 8 | +import io.ktor.client.request.post |
| 9 | +import io.ktor.client.request.setBody |
| 10 | +import io.ktor.http.ContentType |
| 11 | +import io.ktor.http.HttpStatusCode |
| 12 | +import io.ktor.http.contentType |
| 13 | +import io.ktor.serialization.jackson.jackson |
| 14 | +import kotlinx.coroutines.Dispatchers |
| 15 | +import kotlinx.coroutines.runBlocking |
| 16 | +import mu.KotlinLogging |
| 17 | + |
| 18 | +object Ntfy { |
| 19 | + private val log = KotlinLogging.logger {} |
| 20 | + private val client = HttpClient(CIO) { |
| 21 | + expectSuccess = false |
| 22 | + install(ContentNegotiation) { |
| 23 | + jackson() |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + fun publish(notification: Notification) = runBlocking(Dispatchers.IO) { |
| 28 | + val response = client.post("https://ntfy.sh") { |
| 29 | + contentType(ContentType.Application.Json) |
| 30 | + setBody(notification) |
| 31 | + } |
| 32 | + when (response.status) { |
| 33 | + HttpStatusCode.OK -> Unit |
| 34 | + else -> log.warn("Feil ved publisering til ntfy: ${response.body<Map<String, Any?>>()}") |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + data class Notification( |
| 39 | + val topic: String = "teamdigihot.hm-oebs-listener", |
| 40 | + val title: String? = null, |
| 41 | + val message: String? = null, |
| 42 | + val priority: Priority = Priority.DEFAULT, |
| 43 | + val tags: Set<String> = emptySet(), |
| 44 | + val click: String? = null, |
| 45 | + val icon: String? = null, |
| 46 | + val actions: Set<Action> = emptySet(), |
| 47 | + val attach: String? = null, |
| 48 | + val filename: String? = null, |
| 49 | + val email: String? = null, |
| 50 | + val delay: String? = null, |
| 51 | + ) |
| 52 | + |
| 53 | + data class Action( |
| 54 | + val action: ActionType, |
| 55 | + val label: String, |
| 56 | + val clear: Boolean = false, |
| 57 | + val url: String? = null, |
| 58 | + val method: String? = null, |
| 59 | + val headers: Map<String, String> = emptyMap(), |
| 60 | + val body: String? = null, |
| 61 | + val intent: String? = null, |
| 62 | + val extras: Map<String, String> = emptyMap(), |
| 63 | + ) |
| 64 | + |
| 65 | + enum class Priority(@JsonValue val value: Int) { |
| 66 | + MIN(1), |
| 67 | + LOW(2), |
| 68 | + DEFAULT(3), |
| 69 | + HIGH(4), |
| 70 | + MAX(5), |
| 71 | + } |
| 72 | + |
| 73 | + enum class ActionType(@JsonValue val value: String) { |
| 74 | + VIEW("view"), |
| 75 | + BROADCAST("broadcast"), |
| 76 | + HTTP("http"), |
| 77 | + } |
| 78 | +} |
0 commit comments