feat(ntfy): add Ntfy::Publisher service with HTTP POST + auth

This commit is contained in:
Min Zeya Phyo
2026-07-28 02:11:19 +08:00
parent 8f25dc057b
commit 76742d8791
5 changed files with 165 additions and 0 deletions

View File

@@ -0,0 +1,49 @@
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