Skip to content

Commit 78aa5e5

Browse files
committed
publisering av ordrefeilmelding til ntfy
1 parent 42b418a commit 78aa5e5

File tree

3 files changed

+100
-1
lines changed

3 files changed

+100
-1
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
}

src/main/kotlin/no/nav/hjelpemidler/Slack.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
77
import io.ktor.client.request.post
88
import io.ktor.client.request.setBody
99
import io.ktor.http.ContentType
10+
import io.ktor.http.HttpStatusCode
1011
import io.ktor.http.contentType
1112
import io.ktor.serialization.jackson.jackson
1213
import kotlinx.coroutines.Dispatchers
@@ -17,6 +18,7 @@ import no.nav.hjelpemidler.configuration.Configuration
1718
object Slack {
1819
private val log = KotlinLogging.logger {}
1920
private val client = HttpClient(CIO) {
21+
expectSuccess = false
2022
install(ContentNegotiation) {
2123
jackson()
2224
}
@@ -36,6 +38,9 @@ object Slack {
3638
)
3739
)
3840
}
39-
log.info(response.body<String>())
41+
when (response.status) {
42+
HttpStatusCode.OK -> Unit
43+
else -> log.info(response.body<String>())
44+
}
4045
}
4146
}

src/main/kotlin/no/nav/hjelpemidler/api/OrdreAPI.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import io.ktor.server.routing.Route
77
import io.ktor.server.routing.post
88
import mu.KotlinLogging
99
import no.nav.hjelpemidler.Context
10+
import no.nav.hjelpemidler.Ntfy
1011
import no.nav.hjelpemidler.Slack
1112
import no.nav.hjelpemidler.configuration.Configuration
1213
import java.time.LocalDateTime
@@ -39,6 +40,21 @@ fun Route.ordreAPI(context: Context) {
3940
text = "*${Configuration.profile}* - $feilmelding",
4041
channel = "#papaya-alerts"
4142
)
43+
Ntfy.publish(
44+
Ntfy.Notification(
45+
title = "Mottok ordrefeilmelding",
46+
message = "Status: ${feilmelding.status}",
47+
priority = Ntfy.Priority.HIGH,
48+
actions = setOf(
49+
Ntfy.Action(
50+
action = Ntfy.ActionType.VIEW,
51+
label = "Se detaljer i Slack",
52+
clear = true,
53+
url = "https://nav-it.slack.com/archives/C02LS2W05E1"
54+
)
55+
)
56+
)
57+
)
4258
call.response.status(HttpStatusCode.OK)
4359
}
4460
}

0 commit comments

Comments
 (0)