mirror of https://github.com/kha7iq/pingme
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
121 lines
3.4 KiB
121 lines
3.4 KiB
4 years ago
|
package rocketchat
|
||
4 years ago
|
|
||
|
import (
|
||
|
"context"
|
||
|
"strings"
|
||
|
|
||
4 years ago
|
"github.com/kha7iq/pingme/service/helpers"
|
||
4 years ago
|
"github.com/nikoksr/notify"
|
||
|
"github.com/nikoksr/notify/service/rocketchat"
|
||
|
"github.com/urfave/cli/v2"
|
||
|
)
|
||
|
|
||
|
type rocketChat struct {
|
||
|
Token string
|
||
4 years ago
|
UserID string
|
||
4 years ago
|
Message string
|
||
|
Channel string
|
||
|
Title string
|
||
|
ServerURL string
|
||
|
Scheme string
|
||
|
}
|
||
|
|
||
4 years ago
|
// Send parse values from *cli.context and return *cli.Command.
|
||
4 years ago
|
// Values include rocketchat token, , UserId, channelIDs, ServerURL, Scheme, Message and Title.
|
||
|
// If multiple channels are provided then the string is split with "," separator and
|
||
|
// each channelID is added to receiver.
|
||
4 years ago
|
func Send() *cli.Command {
|
||
4 years ago
|
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{
|
||
4 years ago
|
Destination: &rocketChatOpts.UserID,
|
||
4 years ago
|
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",
|
||
4 years ago
|
Value: helpers.TimeValue,
|
||
4 years ago
|
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,
|
||
4 years ago
|
rocketChatOpts.UserID, rocketChatOpts.Token)
|
||
4 years ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
chn := strings.Split(rocketChatOpts.Channel, ",")
|
||
|
for _, v := range chn {
|
||
|
if len(v) <= 0 {
|
||
4 years ago
|
return helpers.ErrChannel
|
||
4 years ago
|
}
|
||
|
|
||
|
rocketChatSvc.AddReceivers(v)
|
||
|
}
|
||
|
|
||
|
notifier.UseServices(rocketChatSvc)
|
||
|
|
||
|
if err = notifier.Send(
|
||
|
context.Background(),
|
||
|
rocketChatOpts.Title,
|
||
|
rocketChatOpts.Message,
|
||
|
); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
return nil
|
||
|
},
|
||
|
}
|
||
|
}
|