Compare commits
4 commits
86e21e3b80
...
303e4061c6
Author | SHA1 | Date | |
---|---|---|---|
303e4061c6 | |||
fe32062d98 | |||
7a9ae5076b | |||
aee2126d21 |
6 changed files with 40 additions and 12 deletions
1
backend/.gitignore
vendored
1
backend/.gitignore
vendored
|
@ -46,3 +46,4 @@ bin/
|
||||||
|
|
||||||
### Other ###
|
### Other ###
|
||||||
bot.cfg
|
bot.cfg
|
||||||
|
ntfyHistory.txt
|
|
@ -14,6 +14,7 @@ import io.ktor.server.plugins.contentnegotiation.*
|
||||||
import io.ktor.server.plugins.cors.routing.*
|
import io.ktor.server.plugins.cors.routing.*
|
||||||
import io.ktor.server.plugins.ratelimit.*
|
import io.ktor.server.plugins.ratelimit.*
|
||||||
import kotlinx.coroutines.runBlocking
|
import kotlinx.coroutines.runBlocking
|
||||||
|
import observer.nelle.nelleObserverBackend.helpers.checkCreate
|
||||||
import observer.nelle.nelleObserverBackend.plugins.configureRouting
|
import observer.nelle.nelleObserverBackend.plugins.configureRouting
|
||||||
import kotlin.time.Duration.Companion.minutes
|
import kotlin.time.Duration.Companion.minutes
|
||||||
|
|
||||||
|
@ -36,6 +37,8 @@ fun Application.module() {
|
||||||
allowHeader(HttpHeaders.ContentType)
|
allowHeader(HttpHeaders.ContentType)
|
||||||
anyHost()
|
anyHost()
|
||||||
}
|
}
|
||||||
|
// check for ntfyhistory file, and if it doesnt exist, create it
|
||||||
|
checkCreate()
|
||||||
runBlocking {
|
runBlocking {
|
||||||
val client =
|
val client =
|
||||||
HttpClient(CIO) {
|
HttpClient(CIO) {
|
||||||
|
|
|
@ -13,8 +13,8 @@ import java.util.*
|
||||||
val prop = Properties()
|
val prop = Properties()
|
||||||
|
|
||||||
class Config {
|
class Config {
|
||||||
val confPath = Paths.get("").toAbsolutePath().toString()
|
private val confPath = Paths.get("").toAbsolutePath().toString()
|
||||||
val confFile = File("$confPath/bot.cfg")
|
private val confFile = File("$confPath/bot.cfg")
|
||||||
|
|
||||||
init {
|
init {
|
||||||
if (confFile.exists()) {
|
if (confFile.exists()) {
|
||||||
|
@ -35,11 +35,4 @@ class Config {
|
||||||
val botToken: String = (prop.getProperty("botToken"))
|
val botToken: String = (prop.getProperty("botToken"))
|
||||||
val instanceDomain: String = (prop.getProperty("instance"))
|
val instanceDomain: String = (prop.getProperty("instance"))
|
||||||
val superSecret: String = (prop.getProperty("superSecret"))
|
val superSecret: String = (prop.getProperty("superSecret"))
|
||||||
|
|
||||||
fun stringToWords(s: String) =
|
|
||||||
s
|
|
||||||
.trim()
|
|
||||||
.splitToSequence(", ")
|
|
||||||
.filter { it.isNotEmpty() } // or: .filter { it.isNotBlank() }
|
|
||||||
.toList()
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,29 @@
|
||||||
|
package observer.nelle.nelleObserverBackend.helpers
|
||||||
|
|
||||||
|
import observer.nelle.nelleObserverBackend.prop
|
||||||
|
import java.io.File
|
||||||
|
import java.io.FileInputStream
|
||||||
|
import java.nio.file.Files
|
||||||
|
import java.nio.file.Paths
|
||||||
|
|
||||||
|
val historyPath = Paths.get("").toAbsolutePath().toString()
|
||||||
|
val historyFile = File("$historyPath/ntfyHistory.txt")
|
||||||
|
|
||||||
|
fun checkCreate() {
|
||||||
|
if (historyFile.exists()) {
|
||||||
|
FileInputStream(historyFile).use { prop.load(it) }
|
||||||
|
} else {
|
||||||
|
Files.createFile(Paths.get("$historyPath/ntfyHistory.txt"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun storeMessage(
|
||||||
|
title: String,
|
||||||
|
message: String,
|
||||||
|
attach: String,
|
||||||
|
click: String,
|
||||||
|
) {
|
||||||
|
val text = "Title: $title, Message: $message, Attach: $attach, Click: $click\n"
|
||||||
|
|
||||||
|
historyFile.appendText(text, Charsets.UTF_8)
|
||||||
|
}
|
|
@ -44,8 +44,8 @@ suspend fun ntfyMsg(
|
||||||
client.post("https://ntfy.ouroboros.group/pushy") {
|
client.post("https://ntfy.ouroboros.group/pushy") {
|
||||||
headers {
|
headers {
|
||||||
append("Title", title)
|
append("Title", title)
|
||||||
append("Attach", title)
|
append("Attach", attach)
|
||||||
append("Click", title)
|
append("Click", click)
|
||||||
}
|
}
|
||||||
setBody(message)
|
setBody(message)
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,6 +12,7 @@ import io.ktor.server.routing.*
|
||||||
import observer.nelle.nelleObserverBackend.*
|
import observer.nelle.nelleObserverBackend.*
|
||||||
import observer.nelle.nelleObserverBackend.helpers.getBeep
|
import observer.nelle.nelleObserverBackend.helpers.getBeep
|
||||||
import observer.nelle.nelleObserverBackend.helpers.getMeow
|
import observer.nelle.nelleObserverBackend.helpers.getMeow
|
||||||
|
import observer.nelle.nelleObserverBackend.helpers.storeMessage
|
||||||
import java.util.*
|
import java.util.*
|
||||||
import kotlin.concurrent.timerTask
|
import kotlin.concurrent.timerTask
|
||||||
import kotlin.time.Duration.Companion.minutes
|
import kotlin.time.Duration.Companion.minutes
|
||||||
|
@ -140,6 +141,7 @@ fun Application.configureRouting(client: HttpClient) {
|
||||||
val click = formParameters["Click"].toString()
|
val click = formParameters["Click"].toString()
|
||||||
call.response.status(HttpStatusCode(201, "Received..."))
|
call.response.status(HttpStatusCode(201, "Received..."))
|
||||||
call.respondText("Received:: Title: $title, Message: $message, Attachment: $attach, Click: $click")
|
call.respondText("Received:: Title: $title, Message: $message, Attachment: $attach, Click: $click")
|
||||||
|
storeMessage(title, message, attach, click)
|
||||||
ntfyMsg(client, title, message, attach, click)
|
ntfyMsg(client, title, message, attach, click)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue