From 02abfb8335debdab3e1c9f332a332c8827c645f7 Mon Sep 17 00:00:00 2001 From: kha7iq Date: Tue, 13 Apr 2021 22:07:08 +0800 Subject: [PATCH] feat: Add RocketChat service --- cmd/rocketchat.go | 122 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 cmd/rocketchat.go diff --git a/cmd/rocketchat.go b/cmd/rocketchat.go new file mode 100644 index 0000000..16edb79 --- /dev/null +++ b/cmd/rocketchat.go @@ -0,0 +1,122 @@ +package cmd + +import ( + "context" + "fmt" + "strings" + "time" + + "github.com/nikoksr/notify" + "github.com/nikoksr/notify/service/rocketchat" + "github.com/urfave/cli/v2" +) + +type rocketChat struct { + Token string + UserId string + Message string + Channel string + Title string + ServerURL string + Scheme string +} + +var ( + EmptyChannel = "channel name or id can not be empty" + TimeValue = "⏰ " + time.Now().String() +) + +func SendToRocketChat() *cli.Command { + var rocketChatOpts rocketChat + return &cli.Command{ + Name: "rocketchat", + Usage: "Send message to rocketchat", + Description: `RocketChat uses token & userID to authenticate and send messages to defined channels. +Multiple channel ids can be used separated by comma ','. +All configuration options are also available via environment variables.`, + UsageText: "pingme rocketchat --userid '123' --token 'xyz' --url 'localhost' --scheme 'http'" + + " --channel 'alert' --msg 'some message'", + Flags: []cli.Flag{ + &cli.StringFlag{ + Destination: &rocketChatOpts.UserId, + Name: "userid", + Aliases: []string{"id"}, + Required: true, + Usage: "User ID", + EnvVars: []string{"ROCKETCHAT_USERID"}, + }, + &cli.StringFlag{ + Destination: &rocketChatOpts.Token, + Name: "token", + Required: true, + Aliases: []string{"t"}, + Usage: "Auth token for sending message, can also be set as environment variable", + EnvVars: []string{"ROCKETCHAT_TOKEN"}, + }, + &cli.StringFlag{ + Destination: &rocketChatOpts.ServerURL, + Name: "url", + Aliases: []string{"u"}, + Required: true, + Usage: "Rocketchat server url", + EnvVars: []string{"ROCKETCHAT_SERVER_URL"}, + }, + &cli.StringFlag{ + Destination: &rocketChatOpts.Scheme, + Name: "scheme", + Usage: "URL scheme http/https", + Value: "https", + Aliases: []string{"s"}, + EnvVars: []string{"ROCKETCHAT_URL_SCHEME"}, + }, + &cli.StringFlag{ + Destination: &rocketChatOpts.Channel, + Name: "channel", + Aliases: []string{"c"}, + Usage: "Channel names separated by comma ','", + EnvVars: []string{"ROCKETCHAT_CHANNELS"}, + }, + &cli.StringFlag{ + Destination: &rocketChatOpts.Message, + Name: "msg", + Usage: "Message content", + EnvVars: []string{"ROCKETCHAT_MESSAGE"}, + }, + &cli.StringFlag{ + Destination: &rocketChatOpts.Title, + Name: "title", + Value: TimeValue, + Usage: "Title of the message", + EnvVars: []string{"ROCKETCHAT_TITLE"}, + }, + }, + Action: func(ctx *cli.Context) error { + notifier := notify.New() + + rocketChatSvc, err := rocketchat.New(rocketChatOpts.ServerURL, rocketChatOpts.Scheme, + rocketChatOpts.UserId, rocketChatOpts.Token) + if err != nil { + return err + } + chn := strings.Split(rocketChatOpts.Channel, ",") + for _, v := range chn { + if len(v) <= 0 { + return fmt.Errorf(EmptyChannel) + } + + rocketChatSvc.AddReceivers(v) + } + + notifier.UseServices(rocketChatSvc) + + if err = notifier.Send( + context.Background(), + rocketChatOpts.Title, + rocketChatOpts.Message, + ); err != nil { + return err + } + return nil + }, + } +}