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.
193 lines
5.6 KiB
193 lines
5.6 KiB
4 years ago
|
package mattermost
|
||
4 years ago
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"encoding/json"
|
||
|
"fmt"
|
||
4 years ago
|
"log"
|
||
4 years ago
|
"net/http"
|
||
|
"strings"
|
||
4 years ago
|
"time"
|
||
4 years ago
|
|
||
4 years ago
|
"github.com/kha7iq/pingme/service/helpers"
|
||
|
|
||
4 years ago
|
"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
|
||
4 years ago
|
APIURL string
|
||
4 years ago
|
Message string
|
||
|
ChanIDs string
|
||
|
}
|
||
|
|
||
4 years ago
|
// matterMostResponse struct holds the server responses
|
||
|
type matterMostResponse struct {
|
||
|
ID string `json:"id"`
|
||
|
CreateAt int64 `json:"create_at"`
|
||
|
UpdateAt int64 `json:"update_at"`
|
||
|
EditAt int `json:"edit_at"`
|
||
|
DeleteAt int `json:"delete_at"`
|
||
|
IsPinned bool `json:"is_pinned"`
|
||
|
UserID string `json:"user_id"`
|
||
|
ChannelID string `json:"channel_id"`
|
||
|
RootID string `json:"root_id"`
|
||
|
ParentID string `json:"parent_id"`
|
||
|
OriginalID string `json:"original_id"`
|
||
|
Message string `json:"message"`
|
||
|
Type string `json:"type"`
|
||
|
Props struct {
|
||
|
FromBot string `json:"from_bot"`
|
||
|
} `json:"props"`
|
||
|
Hashtags string `json:"hashtags"`
|
||
|
PendingPostID string `json:"pending_post_id"`
|
||
|
ReplyCount int `json:"reply_count"`
|
||
|
Metadata struct{} `json:"metadata"`
|
||
|
}
|
||
|
|
||
4 years ago
|
// Send parse values from *cli.context and return *cli.Command
|
||
4 years ago
|
// 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.
|
||
4 years ago
|
func Send() *cli.Command {
|
||
4 years ago
|
var mattermostOpts matterMost
|
||
|
return &cli.Command{
|
||
|
Name: "mattermost",
|
||
|
Usage: "Send message to mattermost",
|
||
4 years ago
|
UsageText: "pingme mattermost --token '123' --channel '12345567' --url 'localhost' --scheme http " +
|
||
4 years ago
|
"--msg 'some message'",
|
||
4 years ago
|
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",
|
||
4 years ago
|
Value: helpers.TimeValue,
|
||
4 years ago
|
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{
|
||
4 years ago
|
Destination: &mattermostOpts.APIURL,
|
||
4 years ago
|
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 {
|
||
4 years ago
|
endPointURL := mattermostOpts.Scheme + "://" + mattermostOpts.ServerURL + mattermostOpts.APIURL
|
||
4 years ago
|
|
||
|
// 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 {
|
||
4 years ago
|
return helpers.ErrChannel
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
jsonData, err := toJSON(v, fullMessage)
|
||
4 years ago
|
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
|
||
4 years ago
|
func toJSON(channel string, msg string) ([]byte, error) {
|
||
4 years ago
|
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
|
||
4 years ago
|
// message and channel id in the form of json byte array and sends
|
||
|
// message to mattermost.
|
||
4 years ago
|
func sendMattermost(url string, token string, jsonPayload []byte) error {
|
||
4 years ago
|
var response matterMostResponse
|
||
|
|
||
4 years ago
|
// Create a new request using http
|
||
4 years ago
|
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonPayload))
|
||
4 years ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
// add authorization header to the request
|
||
4 years ago
|
req.Header.Set("Authorization", token)
|
||
|
req.Header.Set("Content-Type", "application/json; charset=UTF-8")
|
||
4 years ago
|
|
||
4 years ago
|
// create a new http client and send request to server
|
||
|
c := &http.Client{Timeout: 10 * time.Second}
|
||
|
resp, err := c.Do(req)
|
||
4 years ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
4 years ago
|
defer resp.Body.Close()
|
||
|
|
||
|
// decode response sent from server
|
||
|
err = json.NewDecoder(resp.Body).Decode(&response)
|
||
4 years ago
|
if err != nil {
|
||
4 years ago
|
return err
|
||
4 years ago
|
}
|
||
4 years ago
|
log.Printf("Server Reply: %v\nMessage: %v\n", response.ID, response.Message)
|
||
|
|
||
4 years ago
|
return nil
|
||
|
}
|