Compare commits
4 commits
86e21e3b80
...
303e4061c6
Author | SHA1 | Date | |
---|---|---|---|
303e4061c6 | |||
fe32062d98 | |||
7a9ae5076b | |||
aee2126d21 |
6 changed files with 40 additions and 12 deletions
3
backend/.gitignore
vendored
3
backend/.gitignore
vendored
|
@ -45,4 +45,5 @@ bin/
|
|||
.DS_Store
|
||||
|
||||
### 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.ratelimit.*
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import observer.nelle.nelleObserverBackend.helpers.checkCreate
|
||||
import observer.nelle.nelleObserverBackend.plugins.configureRouting
|
||||
import kotlin.time.Duration.Companion.minutes
|
||||
|
||||
|
@ -36,6 +37,8 @@ fun Application.module() {
|
|||
allowHeader(HttpHeaders.ContentType)
|
||||
anyHost()
|
||||
}
|
||||
// check for ntfyhistory file, and if it doesnt exist, create it
|
||||
checkCreate()
|
||||
runBlocking {
|
||||
val client =
|
||||
HttpClient(CIO) {
|
||||
|
|
|
@ -13,8 +13,8 @@ import java.util.*
|
|||
val prop = Properties()
|
||||
|
||||
class Config {
|
||||
val confPath = Paths.get("").toAbsolutePath().toString()
|
||||
val confFile = File("$confPath/bot.cfg")
|
||||
private val confPath = Paths.get("").toAbsolutePath().toString()
|
||||
private val confFile = File("$confPath/bot.cfg")
|
||||
|
||||
init {
|
||||
if (confFile.exists()) {
|
||||
|
@ -35,11 +35,4 @@ class Config {
|
|||
val botToken: String = (prop.getProperty("botToken"))
|
||||
val instanceDomain: String = (prop.getProperty("instance"))
|
||||
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") {
|
||||
headers {
|
||||
append("Title", title)
|
||||
append("Attach", title)
|
||||
append("Click", title)
|
||||
append("Attach", attach)
|
||||
append("Click", click)
|
||||
}
|
||||
setBody(message)
|
||||
}
|
||||
|
|
|
@ -12,6 +12,7 @@ import io.ktor.server.routing.*
|
|||
import observer.nelle.nelleObserverBackend.*
|
||||
import observer.nelle.nelleObserverBackend.helpers.getBeep
|
||||
import observer.nelle.nelleObserverBackend.helpers.getMeow
|
||||
import observer.nelle.nelleObserverBackend.helpers.storeMessage
|
||||
import java.util.*
|
||||
import kotlin.concurrent.timerTask
|
||||
import kotlin.time.Duration.Companion.minutes
|
||||
|
@ -140,6 +141,7 @@ fun Application.configureRouting(client: HttpClient) {
|
|||
val click = formParameters["Click"].toString()
|
||||
call.response.status(HttpStatusCode(201, "Received..."))
|
||||
call.respondText("Received:: Title: $title, Message: $message, Attachment: $attach, Click: $click")
|
||||
storeMessage(title, message, attach, click)
|
||||
ntfyMsg(client, title, message, attach, click)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue