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.
97 lines
2.5 KiB
97 lines
2.5 KiB
4 years ago
|
package discord
|
||
4 years ago
|
|
||
|
import (
|
||
|
"context"
|
||
|
"fmt"
|
||
|
"log"
|
||
|
"strings"
|
||
|
|
||
4 years ago
|
"github.com/kha7iq/pingme/service/helpers"
|
||
|
|
||
4 years ago
|
"github.com/nikoksr/notify"
|
||
|
"github.com/nikoksr/notify/service/discord"
|
||
|
"github.com/urfave/cli/v2"
|
||
|
)
|
||
|
|
||
4 years ago
|
// discordPingMe struct holds data parsed via flags for discord service.
|
||
4 years ago
|
type discordPingMe 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 discord bot token, userID, 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 discordOpts discordPingMe
|
||
|
return &cli.Command{
|
||
|
Name: "discord",
|
||
|
Usage: "Send message to discord",
|
||
|
Description: `Discord uses bot token to authenticate & send messages to defined channels.
|
||
|
Multiple channel ids can be used separated by comma ','.
|
||
|
All configuration options are also available via environment variables.`,
|
||
4 years ago
|
UsageText: "pingme discord --token '123' --channel '12345,67890' --msg 'some message'",
|
||
4 years ago
|
Flags: []cli.Flag{
|
||
|
&cli.StringFlag{
|
||
|
Destination: &discordOpts.Token,
|
||
|
Name: "token",
|
||
|
Required: true,
|
||
|
Usage: "Token of discord bot used for sending message.",
|
||
|
EnvVars: []string{"DISCORD_TOKEN"},
|
||
|
},
|
||
|
&cli.StringFlag{
|
||
|
Destination: &discordOpts.Channel,
|
||
|
Name: "channel",
|
||
|
Required: true,
|
||
|
Usage: "Channel ids of discord.",
|
||
|
EnvVars: []string{"DISCORD_CHANNELS"},
|
||
|
},
|
||
|
&cli.StringFlag{
|
||
|
Destination: &discordOpts.Message,
|
||
|
Name: "msg",
|
||
|
Usage: "Message content.",
|
||
|
EnvVars: []string{"DISCORD_MESSAGE"},
|
||
|
},
|
||
|
&cli.StringFlag{
|
||
|
Destination: &discordOpts.Title,
|
||
|
Name: "title",
|
||
4 years ago
|
Value: helpers.TimeValue,
|
||
4 years ago
|
Usage: "Title of the message.",
|
||
|
EnvVars: []string{"DISCORD_MSG_TITLE"},
|
||
|
},
|
||
|
},
|
||
|
Action: func(ctx *cli.Context) error {
|
||
|
notifier := notify.New()
|
||
|
discordSvc := discord.New()
|
||
|
|
||
|
if err := discordSvc.AuthenticateWithBotToken(discordOpts.Token); err != nil {
|
||
4 years ago
|
return fmt.Errorf("unable to authenticate %v", err)
|
||
4 years ago
|
}
|
||
|
|
||
|
chn := strings.Split(discordOpts.Channel, ",")
|
||
|
for _, v := range chn {
|
||
|
if len(v) <= 0 {
|
||
4 years ago
|
return helpers.ErrChannel
|
||
4 years ago
|
}
|
||
|
|
||
|
discordSvc.AddReceivers(v)
|
||
|
}
|
||
|
|
||
|
notifier.UseServices(discordSvc)
|
||
|
|
||
|
if err := notifier.Send(
|
||
|
context.Background(),
|
||
|
discordOpts.Title,
|
||
|
discordOpts.Message,
|
||
|
); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
log.Println("Successfully sent!")
|
||
|
return nil
|
||
|
},
|
||
|
}
|
||
|
}
|