mirror of
https://github.com/kha7iq/pingme.git
synced 2025-05-15 14:20:18 -07:00
feat: added support for pushover service
This commit is contained in:
parent
61d0bc0138
commit
5377dac6ef
83
cmd/pushover.go
Normal file
83
cmd/pushover.go
Normal file
@ -0,0 +1,83 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"strings"
|
||||
|
||||
"github.com/gregdel/pushover"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
// slackPingMe struct holds data parsed via flags for slack service.
|
||||
type pushOver struct {
|
||||
Token string
|
||||
Recipient string
|
||||
Message string
|
||||
Title string
|
||||
}
|
||||
|
||||
// SendToPushOver parse values from *cli.context and return *cli.Command.
|
||||
// Values include token, users, Message and Title.
|
||||
// If multiple users are provided then the string is split with "," separator and
|
||||
// each user is added to receiver.
|
||||
func SendToPushOver() *cli.Command {
|
||||
var pushOverOpts pushOver
|
||||
return &cli.Command{
|
||||
Name: "pushover",
|
||||
Usage: "Send message to pushover",
|
||||
UsageText: "pingme pushover --token '123' --user '12345,567' --message 'some message'",
|
||||
Description: `Pushover uses token to authenticate application and user token to send messages to the user.
|
||||
All configuration options are also available via environment variables.`,
|
||||
Flags: []cli.Flag{
|
||||
&cli.StringFlag{
|
||||
Destination: &pushOverOpts.Token,
|
||||
Name: "token",
|
||||
Aliases: []string{"t"},
|
||||
Required: true,
|
||||
Usage: "Token of pushover application used for authenticate application.",
|
||||
EnvVars: []string{"PUSHOVER_TOKEN"},
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Destination: &pushOverOpts.Recipient,
|
||||
Name: "user",
|
||||
Required: true,
|
||||
Aliases: []string{"u"},
|
||||
Usage: "User token used for sending message to user,if sending to multiple userss separate with ','.",
|
||||
EnvVars: []string{"PUSHOVER_USER"},
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Destination: &pushOverOpts.Message,
|
||||
Name: "msg",
|
||||
Aliases: []string{"m"},
|
||||
Usage: "Message content.",
|
||||
EnvVars: []string{"PUSHOVER_MESSAGE"},
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Destination: &pushOverOpts.Title,
|
||||
Name: "title",
|
||||
Value: TimeValue,
|
||||
Usage: "Title of the message.",
|
||||
EnvVars: []string{"PUSHOVER_TITLE"},
|
||||
},
|
||||
},
|
||||
Action: func(ctx *cli.Context) error {
|
||||
app := pushover.New(pushOverOpts.Token)
|
||||
message := pushover.NewMessageWithTitle(pushOverOpts.Message, pushOverOpts.Title)
|
||||
users := strings.Split(pushOverOpts.Recipient, ",")
|
||||
|
||||
for _, v := range users {
|
||||
if len(v) == 0 {
|
||||
return fmt.Errorf(EmptyChannel)
|
||||
}
|
||||
recipient := pushover.NewRecipient(v)
|
||||
responsePushOver, err := app.SendMessage(message, recipient)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
log.Printf("Successfully sent!\n%v\n", responsePushOver)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
}
|
1
go.mod
1
go.mod
@ -4,6 +4,7 @@ go 1.16
|
||||
|
||||
require (
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect
|
||||
github.com/gregdel/pushover v0.0.0-20210216095829-2131362cb888
|
||||
github.com/nikoksr/notify v0.15.0
|
||||
github.com/russross/blackfriday/v2 v2.1.0 // indirect
|
||||
github.com/urfave/cli/v2 v2.3.0
|
||||
|
2
go.sum
2
go.sum
@ -79,6 +79,8 @@ github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoA
|
||||
github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
|
||||
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||
github.com/gregdel/pushover v0.0.0-20210216095829-2131362cb888 h1:rnBK/Xp+eaxlNrbMRoBTQ/m5c+jq41NSmqgFscNH7zY=
|
||||
github.com/gregdel/pushover v0.0.0-20210216095829-2131362cb888/go.mod h1:EcaO66Nn1StkpEm1iKtBTV3d2A16SoMsVER1PthX7to=
|
||||
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
|
||||
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
|
||||
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
|
||||
|
Loading…
x
Reference in New Issue
Block a user