commit 16b2a340e10c191500574e4b6687ff0d3aefbcfa Author: kolyuchy Date: Thu Apr 30 17:36:31 2020 +0300 init commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0ccc10d --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.idea +.git \ No newline at end of file diff --git a/chat.go b/chat.go new file mode 100644 index 0000000..91911f8 --- /dev/null +++ b/chat.go @@ -0,0 +1,90 @@ +package gorocket + +import ( + "bytes" + "encoding/json" + "fmt" + "net/http" + "time" +) + +type Message struct { + Alias string `json:"alias"` + Avatar string `json:"avatar"` + Channel string `json:"channel"` + Emoji string `json:"emoji"` + RoomID string `json:"roomId"` + Text string `json:"text"` + Attachments []Attachment `json:"attachments"` +} + +type Attachment struct { + AudioURL string `json:"audio_url"` + AuthorIcon string `json:"author_icon"` + AuthorLink string `json:"author_link"` + AuthorName string `json:"author_name"` + Collapsed bool `json:"collapsed"` + Color string `json:"color"` + Fields []AttachField `json:"fields"` + ImageURL string `json:"image_url"` + MessageLink string `json:"message_link"` + Text string `json:"text"` + ThumbURL string `json:"thumb_url"` + Title string `json:"title"` + TitleLink string `json:"title_link"` + TitleLinkDownload bool `json:"title_link_download"` + Ts time.Time `json:"ts"` + VideoURL string `json:"video_url"` +} + +type AttachField struct { + Short bool `json:"short"` + Title string `json:"title"` + Value string `json:"value"` +} + +type RespPostMessage struct { + Ts int64 `json:"ts"` + Channel string `json:"channel"` + Message RespMessageData `json:"message"` + Success bool `json:"success"` +} + +type RespMessageData struct { + Alias string `json:"alias"` + Msg string `json:"msg"` + ParseUrls bool `json:"parseUrls"` + Groupable bool `json:"groupable"` + Ts time.Time `json:"ts"` + U U `json:"u"` + Rid string `json:"rid"` + UpdatedAt time.Time `json:"_updatedAt"` + ID string `json:"_id"` +} + +type U struct { + ID string `json:"_id"` + Username string `json:"username"` +} + +func (c *Client) PostMessage(msg *Message) (*RespPostMessage, error) { + + opt, _ := json.Marshal(msg) + + req, err := http.NewRequest("POST", + fmt.Sprintf("%s/api/%s/chat.postMessage", c.baseURL, c.apiVersion), + bytes.NewBuffer(opt)) + + if err != nil { + return nil, err + } + + req.Header.Set("Content-Type", "application/json; charset=utf-8") + + res := RespPostMessage{} + if err := c.sendRequest(req, &res); err != nil { + return nil, err + } + + return &res, nil +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..e69de29 diff --git a/gorocket.go b/gorocket.go new file mode 100644 index 0000000..6b32f18 --- /dev/null +++ b/gorocket.go @@ -0,0 +1,55 @@ +package gorocket + +import ( + "encoding/json" + "log" + "net/http" + "time" +) + +type Client struct { + baseURL string + userID string + xToken string + apiVersion string + HTTPClient *http.Client +} + +// NewClient creates new Facest.io client with given API key +func NewClient(token, user, url string) *Client { + return &Client{ + userID: user, + HTTPClient: &http.Client{ + Timeout: 5 * time.Minute, + }, + xToken: token, + baseURL: url, + apiVersion: "v1", + } +} + +func (c *Client) sendRequest(req *http.Request, v interface{}) error { + req.Header.Set("Accept", "application/json; charset=utf-8") + req.Header.Add("X-Auth-Token", c.xToken) + req.Header.Add("X-User-Id", c.userID) + + res, err := c.HTTPClient.Do(req) + if err != nil { + log.Fatal(err) + } + + defer res.Body.Close() + //body, err := ioutil.ReadAll(res.Body) + + //if err != nil { + // log.Fatal(err) + //} + + resp := v + + if err = json.NewDecoder(res.Body).Decode(&resp); err != nil { + return err + } + + return nil +} \ No newline at end of file diff --git a/info.go b/info.go new file mode 100644 index 0000000..688c93a --- /dev/null +++ b/info.go @@ -0,0 +1,21 @@ +package gorocket + +import ( + "fmt" + "net/http" +) + +func (c *Client) Info() error { + req, err := http.NewRequest("GET", fmt.Sprintf("%s/api/info", c.baseURL), nil) + if err != nil { + return err + } + + req.Header.Set("Content-Type", "application/json; charset=utf-8") + + //if err := c.sendRequest(req); err != nil { + // return err + //} + + return nil +}