pushover

package module
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 19, 2021 License: GPL-3.0 Imports: 9 Imported by: 1

README

Pushover API Go Package

DocumentationBuild Status

A Golang package for sending notifications via https://api.pushover.net.

This library is a mash-up of https://github.com/bdenning/go-pushover and https://github.com/gregdel/pushover. Neither did exactly what I wanted, in the way that I wanted.

Library Example

You can use the pushover package within your Golang applications as follows:

// Set your pushover API keys. These keys are fake.
export PUSHOVER_TOKEN="KzGDOReQKggMaC0QOYAMyEEuZJnyUi"
export PUSHOVER_USER="e9e1495ec45826de5983cd1abc8031"
package main
import (
  "fmt"

  "github.com/invisiblethreat/go-pushover/pushover"

)
// from environmental variables. Example of setting is above
config := pushover.GetConfigEnv()
// or form a YAML config file. pushover.yaml.sample is the template
config := pushover.GetConfigFile(file)

msg := pushover.NewMessageConfig(config)
res, err := msg.Push("message")

if err != nil {
    fmt.Errorf("Error sending message: %s\n", err.Error())
}

Dependencies

The dependency set is small- glide can be used to quickly resolve all of the needed repositories with glide install. More information about glide can be found here.

Command Line Tool

A binary is provided by:

  • running go install github.com/invisiblethreat/go-pushover, which installs in $GOPATH/bin/
  • running go build, which builds in the root directory.

Usage of ./go-pushover:
  -f, --file string      YAML config file location (default "pushover.yaml")
  -m, --message string   Message to send. Omit if piping from STDIN
  -t, --title string     Title for message. If empty, default name for the token will be used

Then messages can be sent by piping output to the pushover command.


export PUSHOVER_TOKEN="KzGDORePKggMaC0QOYAMyEEuZJnyUi" # fake
export PUSHOVER_USER="e9e1495ec75826de5983cd1abc8031"  # fake
echo "Foo is in a critical state" | go-pushover
# or
echo "Foo is in a critical state" | go-pushover --file ../../pushover.yaml
# or
go-pushover --file ../../pushover.yaml --message "Foo is in a critical state"


Documentation

Overview

Package pushover provides methods for sending messages using the http://pushover.net API.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	UserKey string `yaml:"user_key"`
	Token   string `yaml:"api_token"`
}

Config helps with managing the ability to enforce constraints more easily

func GetConfigEnv

func GetConfigEnv() (Config, error)

GetConfigEnv reads environment variables returns a Config struct.

func GetConfigFile

func GetConfigFile(file string) (Config, error)

GetConfigFile reads YAML file and returns a Config struct.

func (*Config) AllSet

func (c *Config) AllSet() bool

AllSet is used to see if we need more items to be set for sending a message

type Message

type Message struct {

	// Required
	Token   string `json:"token"`
	User    string `json:"user"`
	Message string `json:"message"`
	//Optional
	Device    string   `json:"device"`
	Title     string   `json:"title"`
	URL       string   `json:"url"`
	URLTitle  string   `json:"url_title"`
	Priority  Priority `json:"priority"`
	Timestamp int32    `json:"timestamp"`
	Sound     Sound    `json:"sound"`
	// For when we have PriorityEmergency messages
	Retry  int `json:"retry"`  // Must be greater than 30s
	Expire int `json:"expire"` // Must be less than 10800s (3h)
}

Message contains all the required settings for sending messages via the pushover.net API. https://pushover.net/api

func NewMessage

func NewMessage(token, user string) *Message

NewMessage returns a new Message with API token values and a user configured .

func NewMessageConfig

func NewMessageConfig(config Config) *Message

NewMessageConfig returns a new Message with API token values and a user configured. The argument of Config type allow for easier constraint checking

func (*Message) EmergencyParamsSet

func (m *Message) EmergencyParamsSet() error

EmergencyParamsSet tests if required items are set for sending an emergency message

func (*Message) GetDevice

func (m *Message) GetDevice() string

GetDevice returns the value of the device

func (*Message) GetExpiry

func (m *Message) GetExpiry() int

GetExpiry returns the set value of the expiry time set for the message

func (*Message) GetMessage

func (m *Message) GetMessage() string

GetMessage returns the set message for the message.

func (*Message) GetPriority

func (m *Message) GetPriority() Priority

GetPriority returns the priority of the message

func (*Message) GetRetry

func (m *Message) GetRetry() int

GetRetry returns the set value of the retry time set for the message

func (*Message) GetSound

func (m *Message) GetSound() Sound

GetSound returns the set value of the sound set for the message

func (*Message) GetTitle

func (m *Message) GetTitle() string

GetTitle returns the set title for the message.

func (*Message) GetURL

func (m *Message) GetURL() string

GetURL returns the value of the URL

func (*Message) GetURLTitle

func (m *Message) GetURLTitle() string

GetURLTitle returns the value of the title for the clickable URL

func (*Message) GetUnixTimestamp

func (m *Message) GetUnixTimestamp() int32

GetUnixTimestamp returns the timestamp supplied to the record.

func (*Message) Push

func (m *Message) Push(message string) (r *Response, err error)

Push sends a message via the pushover.net API and returns the json response

func (*Message) SetDevice

func (m *Message) SetDevice(s string)

SetDevice to send message. This should also work for groups.

func (*Message) SetEmergencyDefault

func (m *Message) SetEmergencyDefault()

SetEmergencyDefault sets the expiry to 1h and retry to 5m

func (*Message) SetExpiry

func (m *Message) SetExpiry(i int) error

SetExpiry sets the sound for the alert. See vars.go for the list.

func (*Message) SetMessage

func (m *Message) SetMessage(s string)

SetMessage will be in frequently used due to the current Push method

func (*Message) SetPriority

func (m *Message) SetPriority(p Priority)

SetPriority sets the priority of the message. See vars.go for the levels.

func (*Message) SetRetry

func (m *Message) SetRetry(i int) error

SetRetry sets the retry interval for the alert.

func (*Message) SetSound

func (m *Message) SetSound(s Sound)

SetSound sets the sound for the alert. See vars.go for the list.

func (*Message) SetTitle

func (m *Message) SetTitle(s string)

SetTitle sets the title of a message. If unset, the default Pushover name will be used

func (*Message) SetURL

func (m *Message) SetURL(s string)

SetURL sets the clickable URL

func (*Message) SetURLTitle

func (m *Message) SetURLTitle(s string)

SetURLTitle Alternate text for the clickable URL

func (*Message) SetUnixTimestamp

func (m *Message) SetUnixTimestamp(t int32)

SetUnixTimestamp will set a time to be used rather than the time the message was received at the Pushover server

type Priority

type Priority int

Priority is an acceptable priority for the Pushover API

const (
	// PriorityLowest makes no sounds, alerts, or popups
	PriorityLowest Priority = -2

	// PriorityLow makes no sounds or alerts, but will make a popup
	PriorityLow Priority = -1

	// PriorityNormal will make a sound, alert, and popup. If this message is
	// received during quiet hours, it will be treated as PriorityLow
	PriorityNormal Priority = 0

	// PriorityHigh will bypass quiet hours, and will be highlighted in red
	PriorityHigh Priority = 1

	// PriorityEmergency are the same as PriorityHigh but require acknowledged.
	// Additionally, 'retry' and 'expiry' must be set in the message
	PriorityEmergency Priority = 2
)

type Response

type Response struct {
	Request string   `json:"request"`
	Status  int      `json:"status"`
	Errors  []string `json:"errors"`
}

Response contains the JSON response returned by the pushover.net API

type Sound

type Sound string

Sound is a an acceptable string for the Pushover API

const (
	// PushoverURL is the API endpoint that will be used for sending all messages.
	PushoverURL = "https://api.pushover.net/1/messages.json"
	// StatusSuccess is the expected status code when a message has been succesfully sent.
	StatusSuccess = 1

	// SoundPushover is a Pushover API sound
	SoundPushover Sound = "pushover"
	// SoundClassical is a Pushover API sound
	SoundClassical Sound = "classical"

	// SoundCosmic is a Pushover API sound
	SoundCosmic Sound = "cosmic"

	// SoundFalling is a Pushover API sound
	SoundFalling Sound = "falling"

	// SoundGamelan is a Pushover API sound
	SoundGamelan Sound = "gamelan"

	// SoundIncoming is a Pushover API sound
	SoundIncoming Sound = "incoming"

	// SoundIntermission is a Pushover API sound
	SoundIntermission Sound = "intermission"

	// SoundMagic is a Pushover API sound
	SoundMagic Sound = "magic"

	// SoundMechanical is a Pushover API sound
	SoundMechanical Sound = "mechanical"

	// SoundPianobar is a Pushover API sound
	SoundPianobar Sound = "pianobar"

	// SoundSiren is a Pushover API sound
	SoundSiren Sound = "siren"

	// SoundSpaceAlarm is a Pushover API sound
	SoundSpaceAlarm Sound = "spacealarm"

	// SoundTugBoat is a Pushover API sound
	SoundTugBoat Sound = "tugboat"

	// SoundAlien is a Pushover API sound
	SoundAlien Sound = "alien"

	// SoundClimb is a Pushover API sound
	SoundClimb Sound = "climb"

	// SoundPersistent is a Pushover API sound
	SoundPersistent Sound = "persistent"

	// SoundEcho is a Pushover API sound
	SoundEcho Sound = "echo"

	// SoundUpDown is a Pushover API sound
	SoundUpDown Sound = "updown"
)

Directories

Path Synopsis
cmd
pushover module

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL