This commit is contained in:
gigirassy
2026-03-18 00:02:23 +01:00
parent 4e3f89ae91
commit 37becdc703
+62
View File
@@ -0,0 +1,62 @@
require "http/client"
require "json"
CONFIG = ARGV[0]? || "ntfy.conf"
struct Msg
include JSON::Serializable
getter event : String
getter message : String?
getter title : String?
end
def notify(title : String, body : String)
Process.run("notify-send", [title, body], output: STDOUT, error: STDERR)
end
def handle_stream(url : String)
loop do
begin
HTTP::Client.get(url, headers: HTTP::Headers{
"Connection" => "keep-alive",
"Cache-Control" => "no-cache",
"Accept" => "application/json"
}) do |res|
io = res.body_io
while line = io.gets
begin
msg = Msg.from_json(line)
next unless msg.event == "message"
title = msg.title || "ntfy"
body = msg.message || ""
notify(title, body)
rescue
end
end
end
rescue
sleep 3
end
end
end
urls = File.read_lines(CONFIG)
.map(&.strip)
.reject(&.empty?)
.map do |url|
u = url.rstrip('/')
u.ends_with?("/json") ? u : "#{u}/json"
end
urls.each do |url|
spawn do
handle_stream(url)
end
end
sleep