Last active
November 10, 2025 09:36
-
-
Save bagilevi/5423e3cef0490cf9a31b81b4cb4a06bd to your computer and use it in GitHub Desktop.
pushover_api_client.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'net/http' | |
| require 'uri' | |
| require 'json' | |
| # Example usage: | |
| # | |
| # PushoverApiClient.new.send_message( | |
| # title: "Error on website", | |
| # message: "Database connection failed", | |
| # priority: 1, # -2 lowest, -1 = low, 0 = normal, 1 = high (always generate sound and vibration), 2 = emergency (retried until acknowledged) | |
| # url: "https://example.com/admin/errors/1651", | |
| # url_title: "View error details", | |
| # ) | |
| # | |
| # Pushover API documentation: | |
| # | |
| # https://pushover.net/api | |
| # | |
| class PushoverApiClient | |
| API_ENDPOINT = 'https://api.pushover.net/1/messages.json' | |
| def initialize | |
| @token, @user_key = ConfigValue.get_all('pushover.api_key', 'pushover.user_key') | |
| raise 'Missing API token' unless @token | |
| raise 'Missing user key' unless @user_key | |
| end | |
| def send_message(**options) | |
| raise 'Missing message content' unless options[:message] | |
| uri = URI.parse(API_ENDPOINT) | |
| request = Net::HTTP::Post.new(uri) | |
| request.set_form_data({ | |
| token: @token, | |
| user: @user_key, | |
| }.merge(options)) | |
| response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http| | |
| http.request(request) | |
| end | |
| handle_response(response) | |
| end | |
| private | |
| def handle_response(response) | |
| case response | |
| when Net::HTTPSuccess | |
| body = JSON.parse(response.body) | |
| Rails.logger.info "✅ Pushover Message sent successfully: #{body['status'] == 1 ? 'Delivered' : 'Pending'}" | |
| else | |
| Rails.logger.info "❌ Pushover Message failed to deliver: #{response.code} - #{response.message}" | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment