feat(ntfy): add Ntfy::Publisher service with HTTP POST + auth
This commit is contained in:
49
lib/my_smsa_pio/notifications/ntfy/publisher.rb
Normal file
49
lib/my_smsa_pio/notifications/ntfy/publisher.rb
Normal 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
|
||||
Reference in New Issue
Block a user