1
0
mirror of synced 2024-11-22 04:56:06 +03:00

add files & images support

This commit is contained in:
Alex Lushpai 2018-11-08 17:17:24 +03:00
parent 7b29b9d33d
commit 97b0741eb6
2 changed files with 131 additions and 5 deletions

View File

@ -384,6 +384,81 @@ func (c *MgClient) DeleteMessage(request DeleteData) (MessagesResponse, int, err
return resp, status, err
}
// GetFile implement get file url
//
// Example:
//
// var client = v1.New("https://token.url", "cb8ccf05e38a47543ad8477d4999be73bff503ea6")
//
// data, status, err := client.GetFile("file_ID")
//
// if err != nil {
// fmt.Printf("%v", err)
// }
//
// fmt.Printf("%s\n", data.MessageID)
func (c *MgClient) GetFile(request string) (FullFileResponse, int, error) {
var resp FullFileResponse
var b []byte
data, status, err := c.GetRequest(fmt.Sprintf("/files/%s", request), b)
if err != nil {
return resp, status, err
}
if e := json.Unmarshal(data, &resp); e != nil {
return resp, status, e
}
if status != http.StatusOK {
return resp, status, c.Error(data)
}
return resp, status, err
}
// UploadFile upload file
func (c *MgClient) UploadFile(request []byte) (UploadFileResponse, int, error) {
var resp UploadFileResponse
data, status, err := c.PostRequest("/files/upload", request)
if err != nil {
return resp, status, err
}
if e := json.Unmarshal(data, &resp); e != nil {
return resp, status, e
}
if status != http.StatusOK {
return resp, status, c.Error(data)
}
return resp, status, err
}
// UploadFileByURL upload file by url
func (c *MgClient) UploadFileByURL(request UploadFileByUrlRequest) (UploadFileResponse, int, error) {
var resp UploadFileResponse
outgoing, _ := json.Marshal(&request)
data, status, err := c.PostRequest("/files/upload_by_url", []byte(outgoing))
if err != nil {
return resp, status, err
}
if e := json.Unmarshal(data, &resp); e != nil {
return resp, status, e
}
if status != http.StatusOK {
return resp, status, c.Error(data)
}
return resp, status, err
}
func (c *MgClient) Error(info []byte) error {
var data map[string]interface{}

View File

@ -39,6 +39,8 @@ const (
MsgOrderStatusCodeComplete = "complete"
// MsgOrderStatusCodeCancel order status group cancel
MsgOrderStatusCodeCancel = "cancel"
FileSizeLimit = 20 * 1024 * 1024
)
// MgClient type
@ -64,6 +66,8 @@ type ChannelSettings struct {
Text ChannelSettingsText `json:"text"`
Product Product `json:"product"`
Order Order `json:"order"`
Files ChannelSettingsFilesBase `json:"files"`
Images ChannelSettingsFilesBase `json:"images"`
}
// Product type
@ -94,6 +98,44 @@ type ChannelSettingsText struct {
Deleting string `json:"deleting"`
}
// ChannelSettingsFilesBase struct
type ChannelSettingsFilesBase struct {
Creating string `json:"creating"`
Editing string `json:"editing"`
Quoting string `json:"quoting"`
Deleting string `json:"deleting"`
Max uint64 `json:"max"`
}
// FullFileResponse uploaded file data
type FullFileResponse struct {
UploadFileResponse
Link string `json:"link,omitempty"`
}
// UploadFileResponse uploaded file data
type UploadFileResponse struct {
ID []byte `json:"id"`
Hash string `json:"hash"`
Type string `json:"type"`
Meta FileMeta `json:"meta"`
MimeType string `json:"mime_type"`
Size int `json:"size"`
Url *string `json:"source_url"`
CreatedAt time.Time `json:"created_at"`
}
// FileMeta file metadata
type FileMeta struct {
Width *int `json:"width,omitempty"`
Height *int `json:"height,omitempty"`
}
// UploadFileByUrlRequest file url to upload
type UploadFileByUrlRequest struct {
Url string `json:"url"`
}
// ActivateResponse channel activation response
type ActivateResponse struct {
ChannelID uint64 `json:"id"`
@ -238,6 +280,15 @@ type WebhookData struct {
Bot *MessageDataBot `json:"bot,omitempty"`
Product *MessageDataProduct `json:"product,omitempty"`
Order *MessageDataOrder `json:"order,omitempty"`
Images *[]FileItem `json:"images,omitempty"`
Files *[]FileItem `json:"files,omitempty"`
}
// FileItem struct
type FileItem struct {
ID string `json:"id"`
Name string `json:"name"`
Size int `json:"size"`
}
// MessageDataUser user data from webhook