From f77d77a0cca70f6f9f87f42463c47f45033aafc6 Mon Sep 17 00:00:00 2001 From: kha7iq Date: Tue, 20 Apr 2021 18:27:20 +0800 Subject: [PATCH 1/4] docs: updates for mattermost service --- README.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 12d08b0..299b7e3 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,7 @@ as message. And most of all this serves as a swiss army knife sort of tool which - *Slack* - *Telegram* - *Pushover* +- *Mattermost* ## Install @@ -114,13 +115,13 @@ NAME: PingMe - Send message to multiple platforms USAGE: - pingme [global options] command [command options] [arguments...] + main [global options] command [command options] [arguments...] 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, Microsoft Teams and email address. + RocketChat, Discord, Pushover, Mattermost, Microsoft Teams and email address. COMMANDS: telegram Send message to telegram @@ -130,6 +131,7 @@ COMMANDS: teams Send message to microsoft teams pushover Send message to pushover email Send an email + mattermost Send message to mattermost help, h Shows a list of commands or help for one command GLOBAL OPTIONS: From 9f34bc7d28cba590b97adb6875b565590608b9c4 Mon Sep 17 00:00:00 2001 From: kha7iq Date: Tue, 20 Apr 2021 18:28:00 +0800 Subject: [PATCH 2/4] fix: disable action on push for branch --- .github/workflows/build.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index fefa31d..ef8a666 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -2,10 +2,10 @@ name: build on: push: - branches: - - 'master' - paths-ignore: - - '**.md' + # branches: + # - 'master' + # paths-ignore: + # - '**.md' tags: - 'v*.*.*' From 817aa02ddcbcbbcd65d4ab6fd757a07bb4f11778 Mon Sep 17 00:00:00 2001 From: kha7iq Date: Tue, 20 Apr 2021 18:28:42 +0800 Subject: [PATCH 3/4] fix: change time format to unix time for defautl title --- cmd/rocketchat.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/rocketchat.go b/cmd/rocketchat.go index d113e4d..e024208 100644 --- a/cmd/rocketchat.go +++ b/cmd/rocketchat.go @@ -24,7 +24,7 @@ type rocketChat struct { var ( // EmptyChannel variable holds default error message if no channel is provided. EmptyChannel = "channel name or id can not be empty" - TimeValue = "⏰ " + time.Now().String() + TimeValue = "⏰ " + time.Now().Format(time.UnixDate) ) // SendToRocketChat parse values from *cli.context and return *cli.Command. From e07c807395928ec448e5aa7b81fddc4b7c9b0f11 Mon Sep 17 00:00:00 2001 From: kha7iq Date: Tue, 20 Apr 2021 18:29:17 +0800 Subject: [PATCH 4/4] feat: added new server mattermost --- cmd/mattermost.go | 164 ++++++++++++++++++++++++++++++++++++++++++++++ main.go | 4 +- 2 files changed, 167 insertions(+), 1 deletion(-) create mode 100644 cmd/mattermost.go diff --git a/cmd/mattermost.go b/cmd/mattermost.go new file mode 100644 index 0000000..18367a6 --- /dev/null +++ b/cmd/mattermost.go @@ -0,0 +1,164 @@ +package cmd + +import ( + "bytes" + "encoding/json" + "fmt" + "io/ioutil" + "net/http" + "strings" + + "github.com/urfave/cli/v2" +) + +// matterMost struct holds data parsed via flags for the service +type matterMost struct { + Title string + Token string + ServerURL string + Scheme string + ApiURL string + Message string + ChanIDs string +} + +// SendToMattermost parse values from *cli.context and return *cli.Command +// and send messages to target channels. +// If multiple channel ids are provided then the string is split with "," separator and +// message is sent to each channel. +func SendToMattermost() *cli.Command { + var mattermostOpts matterMost + return &cli.Command{ + Name: "mattermost", + Usage: "Send message to mattermost", + UsageText: "pingme mattermost --token '123' --channel '12345,567' --url 'localhost' --scheme http " + + "--message 'some message'", + Description: `Mattermost uses token to authenticate and channel ids for targets. +You can specify multiple channels by separating the value with ','.`, + Flags: []cli.Flag{ + &cli.StringFlag{ + Destination: &mattermostOpts.Token, + Name: "token", + Aliases: []string{"t"}, + Required: true, + Usage: "Personal access token or Bot token for authorization.", + EnvVars: []string{"MATTERMOST_TOKEN"}, + }, + &cli.StringFlag{ + Destination: &mattermostOpts.ChanIDs, + Name: "channel", + Required: false, + Aliases: []string{"c"}, + Usage: "Channel IDs, if sending to multiple channels separate with ','.", + EnvVars: []string{"MATTERMOST_CHANNELS"}, + }, + &cli.StringFlag{ + Destination: &mattermostOpts.Message, + Name: "msg", + Aliases: []string{"m"}, + Usage: "Message content.", + EnvVars: []string{"MATTERMOST_MESSAGE"}, + }, + &cli.StringFlag{ + Destination: &mattermostOpts.Title, + Name: "title", + Value: TimeValue, + Usage: "Title of the message.", + EnvVars: []string{"MATTERMOST_TITLE"}, + }, + &cli.StringFlag{ + Destination: &mattermostOpts.ServerURL, + Name: "url", + Aliases: []string{"u"}, + Value: "localhost", + Required: true, + Usage: "URL of your mattermost server i.e example.com", + EnvVars: []string{"MATTERMOST_SERVER_URL"}, + }, + &cli.StringFlag{ + Destination: &mattermostOpts.Scheme, + Name: "scheme", + Value: "https", + Usage: "For server with no tls chose http, by default it uses https", + EnvVars: []string{"MATTERMOST_SCHEME"}, + }, + &cli.StringFlag{ + Destination: &mattermostOpts.ApiURL, + Name: "api", + Value: "/api/v4/posts", + Usage: "Unless using older version of api default is fine.", + EnvVars: []string{"MATTERMOST_API_URL"}, + }, + }, + Action: func(ctx *cli.Context) error { + endPointURL := mattermostOpts.Scheme + "://" + mattermostOpts.ServerURL + mattermostOpts.ApiURL + + // Create a Bearer string by appending string access token + bearer := "Bearer " + mattermostOpts.Token + + fullMessage := mattermostOpts.Title + "\n" + mattermostOpts.Message + + ids := strings.Split(mattermostOpts.ChanIDs, ",") + for _, v := range ids { + if len(v) == 0 { + return fmt.Errorf(EmptyChannel) + } + + jsonData, err := toJson(v, fullMessage) + if err != nil { + return fmt.Errorf("error parsing json\n[ERROR] - %v", err) + } + + if err := sendMattermost(endPointURL, bearer, jsonData); err != nil { + return fmt.Errorf("failed to send message\n[ERROR] - %v", err) + } + + } + return nil + }, + } +} + +// toJson takes strings and convert them to json byte array +func toJson(channel string, msg string) ([]byte, error) { + if len(msg) == 0 { + return nil, fmt.Errorf("Empty message") + } + m := make(map[string]string, 2) + m["channel_id"] = channel + m["message"] = msg + js, err := json.Marshal(m) + if err != nil { + return nil, err + } + return js, nil +} + +// sendMattermost function take the server url , authentication token +// message and channel id in the for of json byte array and sends +// message to mattermost vi http client. +func sendMattermost(url string, token string, jsonPayload []byte) error { + // Create a new request using http + r, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonPayload)) + if err != nil { + return err + } + + // add authorization header to the request + r.Header.Set("Authorization", token) + r.Header.Set("Content-Type", "application/json; charset=UTF-8") + + // Send request using http Client + c := &http.Client{} + resp, err := c.Do(r) + if err != nil { + return err + } + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + print(err) + } + fmt.Println(string(body)) + resp.Body.Close() + return nil +} diff --git a/main.go b/main.go index 3249f5b..36064b5 100644 --- a/main.go +++ b/main.go @@ -12,6 +12,7 @@ import ( // Version variable is used for semVer var Version string +// main with combile all the function into commands func main() { app := cli.NewApp() app.Name = "PingMe" @@ -20,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, Microsoft Teams and email address.` +RocketChat, Discord, Pushover, Mattermost, Microsoft Teams and email address.` // app.Commands contains the subcommands as functions which return []*cli.Command. app.Commands = []*cli.Command{ cmd.SendToTelegram(), @@ -30,6 +31,7 @@ RocketChat, Discord, Pushover, Microsoft Teams and email address.` cmd.SendToTeams(), cmd.SendToPushOver(), cmd.SendToEmail(), + cmd.SendToMattermost(), } err := app.Run(os.Args)