|
|
@ -4,9 +4,10 @@ import (
|
|
|
|
"bytes"
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
)
|
|
|
|
)
|
|
|
@ -22,6 +23,30 @@ type matterMost struct {
|
|
|
|
ChanIDs string
|
|
|
|
ChanIDs string
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 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"`
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// SendToMattermost parse values from *cli.context and return *cli.Command
|
|
|
|
// SendToMattermost parse values from *cli.context and return *cli.Command
|
|
|
|
// and send messages to target channels.
|
|
|
|
// and send messages to target channels.
|
|
|
|
// If multiple channel ids are provided then the string is split with "," separator and
|
|
|
|
// If multiple channel ids are provided then the string is split with "," separator and
|
|
|
@ -31,7 +56,7 @@ func SendToMattermost() *cli.Command {
|
|
|
|
return &cli.Command{
|
|
|
|
return &cli.Command{
|
|
|
|
Name: "mattermost",
|
|
|
|
Name: "mattermost",
|
|
|
|
Usage: "Send message to mattermost",
|
|
|
|
Usage: "Send message to mattermost",
|
|
|
|
UsageText: "pingme mattermost --token '123' --channel '12345,567' --url 'localhost' --scheme http " +
|
|
|
|
UsageText: "pingme mattermost --token '123' --channel '12345567' --url 'localhost' --scheme http " +
|
|
|
|
"--message 'some message'",
|
|
|
|
"--message 'some message'",
|
|
|
|
Description: `Mattermost uses token to authenticate and channel ids for targets.
|
|
|
|
Description: `Mattermost uses token to authenticate and channel ids for targets.
|
|
|
|
You can specify multiple channels by separating the value with ','.`,
|
|
|
|
You can specify multiple channels by separating the value with ','.`,
|
|
|
@ -121,9 +146,7 @@ You can specify multiple channels by separating the value with ','.`,
|
|
|
|
|
|
|
|
|
|
|
|
// toJson takes strings and convert them to json byte array
|
|
|
|
// toJson takes strings and convert them to json byte array
|
|
|
|
func toJson(channel string, msg string) ([]byte, error) {
|
|
|
|
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 := make(map[string]string, 2)
|
|
|
|
m["channel_id"] = channel
|
|
|
|
m["channel_id"] = channel
|
|
|
|
m["message"] = msg
|
|
|
|
m["message"] = msg
|
|
|
@ -135,30 +158,35 @@ func toJson(channel string, msg string) ([]byte, error) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// sendMattermost function take the server url , authentication token
|
|
|
|
// sendMattermost function take the server url , authentication token
|
|
|
|
// message and channel id in the for of json byte array and sends
|
|
|
|
// message and channel id in the form of json byte array and sends
|
|
|
|
// message to mattermost vi http client.
|
|
|
|
// message to mattermost.
|
|
|
|
func sendMattermost(url string, token string, jsonPayload []byte) error {
|
|
|
|
func sendMattermost(url string, token string, jsonPayload []byte) error {
|
|
|
|
|
|
|
|
var response matterMostResponse
|
|
|
|
|
|
|
|
|
|
|
|
// Create a new request using http
|
|
|
|
// Create a new request using http
|
|
|
|
r, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonPayload))
|
|
|
|
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonPayload))
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// add authorization header to the request
|
|
|
|
// add authorization header to the request
|
|
|
|
r.Header.Set("Authorization", token)
|
|
|
|
req.Header.Set("Authorization", token)
|
|
|
|
r.Header.Set("Content-Type", "application/json; charset=UTF-8")
|
|
|
|
req.Header.Set("Content-Type", "application/json; charset=UTF-8")
|
|
|
|
|
|
|
|
|
|
|
|
// Send request using http Client
|
|
|
|
// create a new http client and send request to server
|
|
|
|
c := &http.Client{}
|
|
|
|
c := &http.Client{Timeout: 10 * time.Second}
|
|
|
|
resp, err := c.Do(r)
|
|
|
|
resp, err := c.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// decode response sent from server
|
|
|
|
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(&response)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
print(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fmt.Println(string(body))
|
|
|
|
log.Printf("Server Reply: %v\nMessage: %v\n", response.ID, response.Message)
|
|
|
|
resp.Body.Close()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|