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