Files

50 lines
1.2 KiB
Ruby

require "httparty"
module MySmsaPio
module Notifications
module Ntfy
class Publisher
def initialize(admin)
@admin = admin
end
def publish(title:, message:, priority: 3, tags: [], click: nil)
return false unless @admin.ntfy_configured?
body = {
topic: @admin.ntfy_topic,
title: title,
message: message,
priority: priority,
tags: Array(tags)
}
body[:click] = click if click.present?
url = "#{server_url}/#{@admin.ntfy_topic}"
response = HTTParty.post(
url,
body: body.to_json,
headers: {
"Content-Type" => "application/json",
"Authorization" => "Bearer #{@admin.ntfy_token}"
},
timeout: 10
)
response.success?
rescue StandardError => e
Rails.logger.error("ntfy publish failed for admin #{@admin.id}: #{e.message}")
false
end
private
def server_url
@admin.ntfy_server_url.presence || ENV["NTFY_SERVER_URL"] || "https://ntfy.sh"
end
end
end
end
end