diff --git a/cmd/pushbullet.go b/cmd/pushbullet.go new file mode 100644 index 0000000..c19b9ef --- /dev/null +++ b/cmd/pushbullet.go @@ -0,0 +1,137 @@ +package cmd + +import ( + "context" + "fmt" + "log" + "strings" + + "github.com/nikoksr/notify" + "github.com/nikoksr/notify/service/pushbullet" + "github.com/urfave/cli/v2" +) + +// pushBullet struct holds data parsed via flags for pushbullet service. +type pushBullet struct { + Token string + Message string + Title string + Device string + PhoneNumber string + SMS bool +} + +// SendToPushBullet parse values from *cli.context and return *cli.Command. +// Values include pushbullet token, Device, phone number, Message and Title. +// If multiple devices are provided they the string is split with "," separator and +// each device is added to receiver. +func SendToPushBullet() *cli.Command { + var pushBulletOpts pushBullet + return &cli.Command{ + Name: "pushbullet", + Usage: "Send message to pushbullet", + Description: `Pushbullet uses API token to authenticate & send messages to defined devices. +Multiple device nicknames or numbers can be used separated by comma.`, + UsageText: "pingme pushbullet --token '123' --device 'Web123, myAndroid' --msg 'some message'\n" + + "pingme pushbullet --token '123' --sms true --device 'Web123' --msg 'some message' --number '00123456789'", + Flags: []cli.Flag{ + &cli.StringFlag{ + Destination: &pushBulletOpts.Token, + Name: "token", + Aliases: []string{"t"}, + Required: true, + Usage: "Token of pushbullet api used for sending message.", + EnvVars: []string{"PUSHBULLET_TOKEN"}, + }, + &cli.StringFlag{ + Destination: &pushBulletOpts.Device, + Name: "device", + Aliases: []string{"d"}, + Required: true, + Usage: "Device's nickname of pushbullet.", + EnvVars: []string{"PUSHBULLET_DEVICE"}, + }, + &cli.StringFlag{ + Destination: &pushBulletOpts.PhoneNumber, + Name: "number", + Aliases: []string{"n"}, + Usage: "Target phone number", + EnvVars: []string{"PUSHBULLET_NUMBER"}, + }, + &cli.StringFlag{ + Destination: &pushBulletOpts.Message, + Name: "msg", + Aliases: []string{"m"}, + Usage: "Message content.", + EnvVars: []string{"PUSHBULLET_MESSAGE"}, + }, + &cli.StringFlag{ + Destination: &pushBulletOpts.Title, + Name: "title", + Value: TimeValue, + Usage: "Title of the message.", + EnvVars: []string{"PUSHBULLET_TITLE"}, + }, + &cli.BoolFlag{ + Destination: &pushBulletOpts.SMS, + Name: "sms", + Value: false, + Usage: "To send sms message set the value to 'true'", + EnvVars: []string{"PUSHBULLET_SMS"}, + }, + }, + Action: func(ctx *cli.Context) error { + notifier := notify.New() + + switch pushBulletOpts.SMS { + case true: + pushBulletSmsSvc, err := pushbullet.NewSMS(pushBulletOpts.Token, pushBulletOpts.Device) + if err != nil { + return err + } + devices := strings.Split(pushBulletOpts.PhoneNumber, ",") + for _, v := range devices { + if len(v) <= 0 { + return fmt.Errorf(EmptyChannel) + } + pushBulletSmsSvc.AddReceivers(v) + + notifier.UseServices(pushBulletSmsSvc) + + if err := notifier.Send( + context.Background(), + pushBulletOpts.Title, + pushBulletOpts.Message, + ); err != nil { + return err + } + + } + default: + pushBulletSvc := pushbullet.New(pushBulletOpts.Token) + + devices := strings.Split(pushBulletOpts.Device, ",") + for _, v := range devices { + if len(v) <= 0 { + return fmt.Errorf(EmptyChannel) + } + pushBulletSvc.AddReceivers(v) + } + + notifier.UseServices(pushBulletSvc) + + if err := notifier.Send( + context.Background(), + pushBulletOpts.Title, + pushBulletOpts.Message, + ); err != nil { + return err + } + + } + + log.Println("Successfully sent!") + return nil + }, + } +} diff --git a/go.sum b/go.sum index 5df3046..42a79a1 100644 --- a/go.sum +++ b/go.sum @@ -32,6 +32,7 @@ github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:ma github.com/cpuguy83/go-md2man/v2 v2.0.0 h1:EoUDS0afbrsXAZ9YQ9jdu/mZ2sXgT1/2yyNng4PGlyM= github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= +github.com/cschomburg/go-pushbullet v0.0.0-20171206132031-67759df45fbb h1:7X9nrm+LNWdxzQOiCjy0G51rNUxbH35IDHCjAMvogyM= github.com/cschomburg/go-pushbullet v0.0.0-20171206132031-67759df45fbb/go.mod h1:RfQ9wji3fjcSEsQ+uFCtIh3+BXgcZum8Kt3JxvzYzlk= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= diff --git a/main.go b/main.go index 36064b5..e789cde 100644 --- a/main.go +++ b/main.go @@ -21,7 +21,7 @@ func main() { app.Description = `PingMe is a CLI tool which provides the ability to send messages or alerts to multiple messaging platforms and also email, everything is configurable via environment variables and command line switches.Currently supported platforms include Slack, Telegram, -RocketChat, Discord, Pushover, Mattermost, Microsoft Teams and email address.` +RocketChat, Discord, Pushover, Mattermost, Pushbullet, Microsoft Teams and email address.` // app.Commands contains the subcommands as functions which return []*cli.Command. app.Commands = []*cli.Command{ cmd.SendToTelegram(), @@ -32,6 +32,7 @@ RocketChat, Discord, Pushover, Mattermost, Microsoft Teams and email address.` cmd.SendToPushOver(), cmd.SendToEmail(), cmd.SendToMattermost(), + cmd.SendToPushBullet(), } err := app.Run(os.Args)