コード例 #1
0
ファイル: main.go プロジェクト: drone-plugins/drone-pushover
func main() {
	fmt.Printf("Drone Pushover Plugin built from %s\n", buildCommit)

	system := drone.System{}
	repo := drone.Repo{}
	build := drone.Build{}
	vargs := Params{}

	plugin.Param("system", &system)
	plugin.Param("repo", &repo)
	plugin.Param("build", &build)
	plugin.Param("vargs", &vargs)
	plugin.MustParse()

	if vargs.Retry == 0 {
		vargs.Retry = 60 * time.Second
	}

	if vargs.Expire == 0 {
		vargs.Expire = 3600 * time.Second
	}

	if vargs.Token == "" {
		fmt.Println("Please provide a app token")
		os.Exit(1)
	}

	if vargs.User == "" {
		fmt.Println("Please provide a user token")
		os.Exit(1)
	}

	client := pushover.New(vargs.Token)

	resp, err := client.SendMessage(
		&pushover.Message{
			Title:      BuildTitle(system, repo, build, vargs.Title),
			Message:    BuildBody(system, repo, build, vargs.Body),
			URL:        fmt.Sprintf("%s/%s/%d", system.Link, repo.FullName, build.Number),
			URLTitle:   "Link to the Build",
			DeviceName: vargs.Device,
			Sound:      vargs.Sound,
			Priority:   vargs.Priority,
			Retry:      vargs.Retry,
			Expire:     vargs.Expire,
			Timestamp:  time.Now().Unix(),
		},
		pushover.NewRecipient(
			vargs.User,
		),
	)

	fmt.Println(resp)

	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
}
コード例 #2
0
ファイル: pushover.go プロジェクト: odwrtw/polochon
// New returns a new Pushover
func New(params *Params) (polochon.Notifier, error) {
	if !params.IsValid() {
		return nil, ErrMissingArgument
	}

	return &Pushover{
		app:       pushover.New(params.Key),
		recipient: pushover.NewRecipient(params.Recipient),
	}, nil
}
コード例 #3
0
ファイル: pushovercli.go プロジェクト: blang/pushovercli
func main() {
	flag.Parse()

	app := pushover.New(*argToken)
	recipient := pushover.NewRecipient(*argUser)
	body := strings.Join(flag.Args(), " ")
	if body == "" {
		b, err := ioutil.ReadAll(os.Stdin)
		if err != nil {
			fmt.Fprintf(os.Stderr, "Error: %s", err)
			os.Exit(2)
		}
		body = string(b)
	}
	msg := pushover.NewMessage(body)
	if *argTitle != "" {
		msg.Title = *argTitle
	}
	if *argURL != "" {
		msg.URL = *argURL
	}
	if *argURLTitle != "" {
		msg.URLTitle = *argURLTitle
	}
	if *argDevices != "" {
		msg.DeviceName = *argDevices
	}
	msg.Priority = *argPriority
	if *argSound != "" {
		msg.Sound = *argSound
	}
	if *argTimestamp != "" {
		ts, err := time.Parse(*argTimestampFormat, *argTimestamp)
		if err != nil {
			fmt.Fprintf(os.Stderr, "Error parsing timestamp: %s\n", err)
			os.Exit(1)
		}
		msg.Timestamp = ts.Unix()
	}
	resp, err := app.SendMessage(msg, recipient)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Error: %s", err)
		os.Exit(1)
	}
	if len(resp.Errors) > 0 {
		fmt.Fprintf(os.Stderr, "Error: %s", err)
		os.Exit(2)
	}
}
コード例 #4
0
ファイル: pushover.go プロジェクト: tantra35/notifier
//SendEvents implements Sender interface Send
func (sender *Sender) SendEvents(events notifier.EventsData, contact notifier.ContactData, trigger notifier.TriggerData, throttled bool) error {
	api := pushover.New(sender.APIToken)
	recipient := pushover.NewRecipient(contact.Value)

	subjectState := events.GetSubjectState()
	title := fmt.Sprintf("%s %s %s (%d)", subjectState, trigger.Name, trigger.GetTags(), len(events))
	timestamp := events[len(events)-1].Timestamp

	var message string
	priority := pushover.PriorityNormal
	for i, event := range events {
		if i > 4 {
			break
		}
		if event.State == "ERROR" || event.State == "EXCEPTION" {
			priority = pushover.PriorityEmergency
		}
		if priority != pushover.PriorityEmergency && (event.State == "WARN" || event.State == "NODATA") {
			priority = pushover.PriorityHigh
		}
		value := strconv.FormatFloat(event.Value, 'f', -1, 64)
		message += fmt.Sprintf("%s: %s = %s (%s to %s)\n", time.Unix(event.Timestamp, 0).Format("15:04"), event.Metric, value, event.OldState, event.State)
	}

	if len(events) > 5 {
		message += fmt.Sprintf("\n...and %d more events.", len(events)-5)
	}

	if throttled {
		message += "\nPlease, fix your system or tune this trigger to generate less events."
	}

	log.Debug("Calling pushover with message title %s, body %s", title, message)

	pushoverMessage := &pushover.Message{
		Message:   message,
		Title:     title,
		Priority:  priority,
		Retry:     5 * time.Minute,
		Expire:    time.Hour,
		Timestamp: timestamp,
		URL:       fmt.Sprintf("%s/#/events/%s", sender.FrontURI, events[0].TriggerID),
	}
	_, err := api.SendMessage(pushoverMessage, recipient)
	if err != nil {
		return fmt.Errorf("Failed to send message to pushover user %s: %s", contact.Value, err.Error())
	}
	return nil
}
コード例 #5
0
ファイル: email2pushover.go プロジェクト: zoni/email2pushover
func main() {
	app.Version(VERSION)
	kingpin.MustParse(app.Parse(os.Args[1:]))
	headerlist := ParseHeaderList(*headers)

	headers, err := extractHeadersFromMail(os.Stdin, headerlist)
	if err != nil {
		log.Fatal(err)
	}

	pushover_app := pushover.New(*token)
	pushover_recipient := pushover.NewRecipient(*recipient)
	pushover_message := &pushover.Message{
		Message: constructMessageBody(headers, headerlist),
		Title:   *title,
	}

	_, err = pushover_app.SendMessage(pushover_message, pushover_recipient)
	if err != nil {
		log.Fatal(err)
	}
}