1
0
mirror of https://github.com/badkaktus/gorocket.git synced 2025-01-04 03:01:51 +02:00

init commit

This commit is contained in:
kolyuchy 2020-04-30 17:36:31 +03:00
commit 16b2a340e1
5 changed files with 168 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
.idea
.git

90
chat.go Normal file
View File

@ -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
}

0
go.mod Normal file
View File

55
gorocket.go Normal file
View File

@ -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
}

21
info.go Normal file
View File

@ -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
}