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.
94 lines
2.5 KiB
94 lines
2.5 KiB
4 years ago
|
package slack
|
||
4 years ago
|
|
||
|
import (
|
||
|
"context"
|
||
|
"log"
|
||
|
"strings"
|
||
|
|
||
4 years ago
|
"github.com/kha7iq/pingme/service/helpers"
|
||
|
|
||
4 years ago
|
"github.com/nikoksr/notify"
|
||
|
"github.com/nikoksr/notify/service/slack"
|
||
|
"github.com/urfave/cli/v2"
|
||
|
)
|
||
|
|
||
4 years ago
|
// slackPingMe struct holds data parsed via flags for slack service.
|
||
4 years ago
|
type slackPingMe struct {
|
||
|
Token string
|
||
|
Message string
|
||
|
Channel string
|
||
|
Title string
|
||
|
}
|
||
|
|
||
4 years ago
|
// Send parse values from *cli.context and return *cli.Command.
|
||
4 years ago
|
// Values include slack token, channelIDs, 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 slackOpts slackPingMe
|
||
|
return &cli.Command{
|
||
|
Name: "slack",
|
||
|
Usage: "Send message to slack",
|
||
4 years ago
|
UsageText: "pingme slack --token '123' --channel '12345,67890' --msg 'some message'",
|
||
4 years ago
|
Description: `Slack uses token 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.`,
|
||
|
Flags: []cli.Flag{
|
||
|
&cli.StringFlag{
|
||
|
Destination: &slackOpts.Token,
|
||
|
Name: "token",
|
||
|
Aliases: []string{"t"},
|
||
|
Required: true,
|
||
|
Usage: "Token of slack bot used for sending message.",
|
||
|
EnvVars: []string{"SLACK_TOKEN"},
|
||
|
},
|
||
|
&cli.StringFlag{
|
||
|
Destination: &slackOpts.Channel,
|
||
|
Name: "channel",
|
||
|
Required: true,
|
||
|
Aliases: []string{"c"},
|
||
|
Usage: "Channel ids for slack,if sending to multiple channels separate with ','.",
|
||
|
EnvVars: []string{"SLACK_CHANNELS"},
|
||
|
},
|
||
|
&cli.StringFlag{
|
||
|
Destination: &slackOpts.Message,
|
||
|
Name: "msg",
|
||
|
Aliases: []string{"m"},
|
||
|
Usage: "Message content.",
|
||
|
EnvVars: []string{"SLACK_MESSAGE"},
|
||
|
},
|
||
|
&cli.StringFlag{
|
||
|
Destination: &slackOpts.Title,
|
||
|
Name: "title",
|
||
4 years ago
|
Value: helpers.TimeValue,
|
||
4 years ago
|
Usage: "Title of the message.",
|
||
|
EnvVars: []string{"SLACK_MSG_TITLE"},
|
||
|
},
|
||
|
},
|
||
|
Action: func(ctx *cli.Context) error {
|
||
|
notifier := notify.New()
|
||
|
slackSvc := slack.New(slackOpts.Token)
|
||
|
chn := strings.Split(slackOpts.Channel, ",")
|
||
|
for _, v := range chn {
|
||
|
if len(v) <= 0 {
|
||
4 years ago
|
return helpers.ErrChannel
|
||
4 years ago
|
}
|
||
|
|
||
|
slackSvc.AddReceivers(v)
|
||
|
}
|
||
|
|
||
|
notifier.UseServices(slackSvc)
|
||
|
|
||
|
if err := notifier.Send(
|
||
|
context.Background(),
|
||
|
slackOpts.Title,
|
||
|
slackOpts.Message,
|
||
|
); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
log.Println("Successfully sent!")
|
||
|
return nil
|
||
|
},
|
||
|
}
|
||
|
}
|