mg-bot-api-client-go/v1/client.go

918 lines
21 KiB
Go
Raw Normal View History

2018-08-03 18:11:37 +03:00
package v1
import (
2018-12-26 17:38:41 +03:00
"bytes"
2018-08-03 18:11:37 +03:00
"encoding/json"
"errors"
"fmt"
2018-12-26 17:38:41 +03:00
"io"
2021-11-25 12:51:29 +03:00
"log"
2018-08-03 18:11:37 +03:00
"net/http"
2018-08-31 17:54:53 +03:00
"strings"
2018-08-03 18:11:37 +03:00
"time"
"github.com/google/go-querystring/query"
)
// New initialize client
func New(url string, token string) *MgClient {
return &MgClient{
URL: url,
Token: token,
2019-02-04 12:40:58 +03:00
httpClient: &http.Client{Timeout: time.Minute},
2018-08-03 18:11:37 +03:00
}
}
2021-11-25 12:51:29 +03:00
// WithLogger sets the provided logger instance into the Client.
func (c *MgClient) WithLogger(logger BasicLogger) *MgClient {
c.logger = logger
return c
}
// writeLog writes to the log.
func (c *MgClient) writeLog(format string, v ...interface{}) {
if c.logger != nil {
c.logger.Printf(format, v...)
return
}
log.Printf(format, v...)
}
2018-08-29 02:44:06 +03:00
// Bots get all available bots
//
// Example:
//
// var client = v1.New("https://demo.url", "09jIJ")
//
// data, status, err := client.Bots()
//
// if err != nil {
// fmt.Printf("%v", err)
// }
//
// if status >= http.StatusBadRequest {
// fmt.Printf("%v", err)
// }
//
// for _, bot := range data {
// fmt.Printf("%v %v\n", bot.Name, bot.CreatedAt)
// }
func (c *MgClient) Bots(request BotsRequest) ([]BotsResponseItem, int, error) {
var resp []BotsResponseItem
2018-08-03 18:11:37 +03:00
var b []byte
outgoing, _ := query.Values(request)
data, status, err := c.GetRequest(fmt.Sprintf("/bots?%s", outgoing.Encode()), b)
if err != nil {
return resp, status, err
}
if status > http.StatusCreated || status < http.StatusOK {
return resp, status, c.Error(data)
}
if err := json.Unmarshal(data, &resp); err != nil {
return resp, status, err
}
2018-08-03 18:11:37 +03:00
return resp, status, err
}
2018-08-29 02:44:06 +03:00
// Channels get all available channels
//
// Example:
//
// var client = v1.New("https://demo.url", "09jIJ")
//
// data, status, err := client.Channels()
//
// if err != nil {
// fmt.Printf("%v", err)
// }
//
// if status >= http.StatusBadRequest {
// fmt.Printf("%v", err)
// }
//
// for _, channel := range data {
// fmt.Printf("%v %v\n", channel.Type, channel.CreatedAt)
// }
func (c *MgClient) Channels(request ChannelsRequest) ([]ChannelResponseItem, int, error) {
var resp []ChannelResponseItem
2018-08-03 18:11:37 +03:00
var b []byte
outgoing, _ := query.Values(request)
data, status, err := c.GetRequest(fmt.Sprintf("/channels?%s", outgoing.Encode()), b)
if err != nil {
return resp, status, err
}
if status > http.StatusCreated || status < http.StatusOK {
return resp, status, c.Error(data)
}
if err := json.Unmarshal(data, &resp); err != nil {
return resp, status, err
}
2018-08-03 18:11:37 +03:00
return resp, status, err
}
2018-08-29 02:44:06 +03:00
// Users get all available users
//
// Example:
//
// var client = v1.New("https://demo.url", "09jIJ")
//
// data, status, err := client.Users(UsersRequest:{Active:1})
//
// if err != nil {
// fmt.Printf("%v", err)
// }
//
// if status >= http.StatusBadRequest {
// fmt.Printf("%v", err)
// }
//
// for _, user := range data {
// fmt.Printf("%v %v\n", user.FirstName, user.IsOnline)
// }
func (c *MgClient) Users(request UsersRequest) ([]UsersResponseItem, int, error) {
var resp []UsersResponseItem
2018-08-03 18:11:37 +03:00
var b []byte
outgoing, _ := query.Values(request)
2018-08-29 02:44:06 +03:00
data, status, err := c.GetRequest(fmt.Sprintf("/users?%s", outgoing.Encode()), b)
2018-08-03 18:11:37 +03:00
if err != nil {
return resp, status, err
}
if status > http.StatusCreated || status < http.StatusOK {
return resp, status, c.Error(data)
}
if err := json.Unmarshal(data, &resp); err != nil {
return resp, status, err
}
2018-08-03 18:11:37 +03:00
return resp, status, err
}
2018-08-29 02:44:06 +03:00
// Customers get all available customers
//
// Example:
//
// var client = v1.New("https://demo.url", "09jIJ")
//
// data, status, err := client.Customers()
//
// if err != nil {
// fmt.Printf("%v", err)
// }
//
// if status >= http.StatusBadRequest {
// fmt.Printf("%v", err)
// }
//
// for _, customer := range data {
// fmt.Printf("%v %v\n", customer.FirstName, customer.Avatar)
// }
func (c *MgClient) Customers(request CustomersRequest) ([]CustomersResponseItem, int, error) {
var resp []CustomersResponseItem
2018-08-03 18:11:37 +03:00
var b []byte
outgoing, _ := query.Values(request)
data, status, err := c.GetRequest(fmt.Sprintf("/customers?%s", outgoing.Encode()), b)
if err != nil {
return resp, status, err
}
if status > http.StatusCreated || status < http.StatusOK {
return resp, status, c.Error(data)
}
if err := json.Unmarshal(data, &resp); err != nil {
return resp, status, err
}
2018-08-03 18:11:37 +03:00
return resp, status, err
}
2018-08-29 02:44:06 +03:00
// Chats get all available chats
//
// Example:
//
// var client = v1.New("https://demo.url", "09jIJ")
//
// data, status, err := client.Chats(ChatsRequest{ChannelType:ChannelTypeWhatsapp})
//
// if err != nil {
// fmt.Printf("%v", err)
// }
//
// if status >= http.StatusBadRequest {
// fmt.Printf("%v", err)
// }
//
// for _, chat := range data {
// fmt.Printf("%v %v\n", chat.Customer, chat.LastMessage)
// }
func (c *MgClient) Chats(request ChatsRequest) ([]ChatResponseItem, int, error) {
var resp []ChatResponseItem
2018-08-03 18:11:37 +03:00
var b []byte
outgoing, _ := query.Values(request)
data, status, err := c.GetRequest(fmt.Sprintf("/chats?%s", outgoing.Encode()), b)
if err != nil {
return resp, status, err
}
if status > http.StatusCreated || status < http.StatusOK {
return resp, status, c.Error(data)
}
if err := json.Unmarshal(data, &resp); err != nil {
return resp, status, err
}
2018-08-03 18:11:37 +03:00
return resp, status, err
}
2018-08-29 02:44:06 +03:00
// Members get all available chat members
//
// Example:
//
// var client = v1.New("https://demo.url", "09jIJ")
//
// data, status, err := client.Members(MembersRequest{State:ChatMemberStateActive})
//
// if err != nil {
// fmt.Printf("%v", err)
// }
//
// if status >= http.StatusBadRequest {
// fmt.Printf("%v", err)
// }
//
// for _, member := range data {
// fmt.Printf("%v\n", member.CreatedAt)
// }
func (c *MgClient) Members(request MembersRequest) ([]MemberResponseItem, int, error) {
var resp []MemberResponseItem
2018-08-03 18:11:37 +03:00
var b []byte
outgoing, _ := query.Values(request)
data, status, err := c.GetRequest(fmt.Sprintf("/members?%s", outgoing.Encode()), b)
if err != nil {
return resp, status, err
}
if status > http.StatusCreated || status < http.StatusOK {
return resp, status, c.Error(data)
}
if err := json.Unmarshal(data, &resp); err != nil {
return resp, status, err
}
2018-08-03 18:11:37 +03:00
return resp, status, err
}
2018-08-29 02:44:06 +03:00
// Dialogs get all available dialogs
//
// Example:
//
// var client = v1.New("https://demo.url", "09jIJ")
//
// data, status, err := client.Dialogs(DialogsRequest{Active:1})
//
// if err != nil {
// fmt.Printf("%v", err)
// }
//
// if status >= http.StatusBadRequest {
// fmt.Printf("%v", err)
// }
//
// for _, dialog := range data {
// fmt.Printf("%v %v\n", dialog.ChatID, dialog.CreatedAt)
// }
func (c *MgClient) Dialogs(request DialogsRequest) ([]DialogResponseItem, int, error) {
var resp []DialogResponseItem
2018-08-03 18:11:37 +03:00
var b []byte
outgoing, _ := query.Values(request)
data, status, err := c.GetRequest(fmt.Sprintf("/dialogs?%s", outgoing.Encode()), b)
if err != nil {
return resp, status, err
}
if status > http.StatusCreated || status < http.StatusOK {
return resp, status, c.Error(data)
}
if err := json.Unmarshal(data, &resp); err != nil {
return resp, status, err
}
2018-08-03 18:11:37 +03:00
return resp, status, err
}
2018-09-18 17:27:47 +03:00
// DialogAssign allows to assign dialog to Bot or User
//
// Example:
//
// var client = v1.New("https://demo.url", "09jIJ")
//
// data, status, err := client.DialogAssign(DialogAssignRequest{DialogID: 1, UserID: 6})
//
// if err != nil {
// fmt.Printf("%v", err)
// }
//
// if status >= http.StatusBadRequest {
// fmt.Printf("%v", err)
// }
//
// fmt.Printf("%v %v\n", data.Responsible, data.LeftUserID )
2018-08-03 18:11:37 +03:00
func (c *MgClient) DialogAssign(request DialogAssignRequest) (DialogAssignResponse, int, error) {
var resp DialogAssignResponse
outgoing, _ := json.Marshal(&request)
2018-08-29 02:44:06 +03:00
data, status, err := c.PatchRequest(fmt.Sprintf("/dialogs/%d/assign", request.DialogID), []byte(outgoing))
2018-08-03 18:11:37 +03:00
if err != nil {
return resp, status, err
}
if status > http.StatusCreated || status < http.StatusOK {
return resp, status, c.Error(data)
2020-07-14 14:37:46 +03:00
}
if err := json.Unmarshal(data, &resp); err != nil {
return resp, status, err
}
return resp, status, err
}
// DialogUnassign allows to remove responsible from the dialogue
//
// Example:
//
// var client = v1.New("https://demo.url", "09jIJ")
//
// data, status, err := client.DialogUnassign(1)
//
// if err != nil {
// fmt.Printf("%v", err)
// }
//
// if status >= http.StatusBadRequest {
// fmt.Printf("%v", err)
// }
//
// fmt.Printf("%v\n", data.Responsible)
func (c *MgClient) DialogUnassign(dialogID uint64) (DialogUnassignResponse, int, error) {
var resp DialogUnassignResponse
data, status, err := c.PatchRequest(fmt.Sprintf("/dialogs/%d/unassign", dialogID), nil)
if err != nil {
return resp, status, err
}
if status != http.StatusOK {
return resp, status, c.Error(data)
2018-08-03 18:11:37 +03:00
}
if err := json.Unmarshal(data, &resp); err != nil {
return resp, status, err
}
2018-08-03 18:11:37 +03:00
return resp, status, err
}
2018-08-29 02:44:06 +03:00
// DialogClose close selected dialog
//
// Example:
//
// var client = v1.New("https://demo.url", "09jIJ")
//
// _, status, err := client.DialogClose(123)
//
// if err != nil {
// fmt.Printf("%v", err)
// }
//
// if status >= http.StatusBadRequest {
// fmt.Printf("%v", err)
// }
func (c *MgClient) DialogClose(request uint64) (map[string]interface{}, int, error) {
var resp map[string]interface{}
2018-08-03 18:11:37 +03:00
outgoing, _ := json.Marshal(&request)
2018-08-29 02:44:06 +03:00
data, status, err := c.DeleteRequest(fmt.Sprintf("/dialogs/%d/close", request), []byte(outgoing))
2018-08-03 18:11:37 +03:00
if err != nil {
return resp, status, err
}
if status > http.StatusCreated || status < http.StatusOK {
return resp, status, c.Error(data)
}
if err := json.Unmarshal(data, &resp); err != nil {
return resp, status, err
}
2018-08-03 18:11:37 +03:00
return resp, status, err
}
// DialogsTagsAdd allows to assign dialog to Bot or User
//
// Example:
//
// var client = v1.New("https://demo.url", "09jIJ")
//
// data, status, err := client.DialogsTagsAdd(DialogTagsAddRequest{DialogID: uint64(1),Tags: []TagsAdd{{Name: "foo"}}})
func (c *MgClient) DialogsTagsAdd(request DialogTagsAddRequest) (int, error) {
outgoing, _ := json.Marshal(&request)
data, status, err := c.PatchRequest(fmt.Sprintf("/dialogs/%d/tags/add", request.DialogID), outgoing)
if err != nil {
return status, err
}
if status != http.StatusOK {
return status, c.Error(data)
}
return status, err
}
// DialogTagsDelete allows to assign dialog to Bot or User
//
// Example:
//
// var client = v1.New("https://demo.url", "09jIJ")
//
// data, status, err := client.DialogsTagsAdd(DialogTagsDelete{DialogID: uint64(1),Tags: []TagsDelete{{Name: "foo"}}})
func (c *MgClient) DialogTagsDelete(request DialogTagsDeleteRequest) (int, error) {
outgoing, _ := json.Marshal(&request)
data, status, err := c.PatchRequest(fmt.Sprintf("/dialogs/%d/tags/delete", request.DialogID), outgoing)
if err != nil {
return status, err
}
if status != http.StatusOK {
return status, c.Error(data)
}
return status, err
}
2018-08-29 02:44:06 +03:00
// Messages get all available messages
//
// Example:
//
// var client = v1.New("https://demo.url", "09jIJ")
//
2018-09-18 16:12:59 +03:00
// data, status, err := client.Messages(MessagesRequest{UserID:5})
2018-08-29 02:44:06 +03:00
//
// if err != nil {
// fmt.Printf("%v", err)
// }
//
// if status >= http.StatusBadRequest {
// fmt.Printf("%v", err)
// }
//
// for _, message := range data {
// fmt.Printf("%v %v %v\n", message.ChatID, message.CreatedAt, message.CustomerID)
// }
func (c *MgClient) Messages(request MessagesRequest) ([]MessagesResponseItem, int, error) {
var resp []MessagesResponseItem
2018-08-03 18:11:37 +03:00
var b []byte
outgoing, _ := query.Values(request)
data, status, err := c.GetRequest(fmt.Sprintf("/messages?%s", outgoing.Encode()), b)
if err != nil {
return resp, status, err
}
if status > http.StatusCreated || status < http.StatusOK {
return resp, status, c.Error(data)
}
if err := json.Unmarshal(data, &resp); err != nil {
return resp, status, err
}
2018-08-03 18:11:37 +03:00
return resp, status, err
}
2018-08-29 02:44:06 +03:00
// MessageSend send message
//
// Example:
//
// var client = v1.New("https://demo.url", "09jIJ")
//
// data, status, err := client.MessageSend(MessageSendRequest{
// Scope: "public",
// Content: "test",
// ChatID: i,
// })
//
// if err != nil {
// fmt.Printf("%v", err)
// }
//
// if status >= http.StatusBadRequest {
// fmt.Printf("%v", err)
// }
//
// fmt.Printf("%v \n", data.MessageID, data.Time)
func (c *MgClient) MessageSend(request MessageSendRequest) (MessageSendResponse, int, error) {
var resp MessageSendResponse
2018-08-03 18:11:37 +03:00
outgoing, _ := json.Marshal(&request)
2018-12-26 17:38:41 +03:00
data, status, err := c.PostRequest("/messages", bytes.NewBuffer(outgoing))
2018-08-03 18:11:37 +03:00
if err != nil {
return resp, status, err
}
if status > http.StatusCreated || status < http.StatusOK {
return resp, status, c.Error(data)
}
if err := json.Unmarshal(data, &resp); err != nil {
return resp, status, err
}
2018-08-03 18:11:37 +03:00
return resp, status, err
}
2018-08-29 02:44:06 +03:00
// MessageEdit update selected message
//
// Example:
//
// var client = v1.New("https://demo.url", "09jIJ")
//
// _, status, err := client.MessageEdit(MessageEditRequest{
// ID: 123,
// Content: "test",
// })
//
// if err != nil {
// fmt.Printf("%v", err)
// }
//
// if status >= http.StatusBadRequest {
// fmt.Printf("%v", err)
// }
func (c *MgClient) MessageEdit(request MessageEditRequest) (map[string]interface{}, int, error) {
var resp map[string]interface{}
2018-08-03 18:11:37 +03:00
outgoing, _ := json.Marshal(&request)
2018-08-29 02:44:06 +03:00
data, status, err := c.PatchRequest(fmt.Sprintf("/messages/%d", request.ID), []byte(outgoing))
2018-08-03 18:11:37 +03:00
if err != nil {
return resp, status, err
}
if status > http.StatusCreated || status < http.StatusOK {
return resp, status, c.Error(data)
}
if err := json.Unmarshal(data, &resp); err != nil {
return resp, status, err
}
2018-08-03 18:11:37 +03:00
return resp, status, err
}
2018-08-29 02:44:06 +03:00
// MessageDelete delete selected message
//
// Example:
//
// var client = v1.New("https://demo.url", "09jIJ")
//
// _, status, err := client.MessageDelete(123)
//
// if err != nil {
// fmt.Printf("%v", err)
// }
//
// if status >= http.StatusBadRequest {
// fmt.Printf("%v", err)
// }
func (c *MgClient) MessageDelete(request uint64) (map[string]interface{}, int, error) {
var resp map[string]interface{}
2018-08-03 18:11:37 +03:00
outgoing, _ := json.Marshal(&request)
2018-08-29 02:44:06 +03:00
data, status, err := c.DeleteRequest(fmt.Sprintf("/messages/%d", request), []byte(outgoing))
2018-08-03 18:11:37 +03:00
if err != nil {
return resp, status, err
}
if status > http.StatusCreated || status < http.StatusOK {
return resp, status, c.Error(data)
}
if err := json.Unmarshal(data, &resp); err != nil {
return resp, status, err
}
2018-08-03 18:11:37 +03:00
return resp, status, err
}
2018-08-29 02:44:06 +03:00
// Info updates bot information
//
// Example:
//
// var client = v1.New("https://demo.url", "09jIJ")
//
// _, status, err := client.Info(InfoRequest{Name: "AWESOME", Avatar: "https://example.com/logo.svg"})
//
// if err != nil {
// fmt.Printf("%v", err)
// }
//
// if status >= http.StatusBadRequest {
// fmt.Printf("%v", err)
// }
func (c *MgClient) Info(request InfoRequest) (map[string]interface{}, int, error) {
var resp map[string]interface{}
2018-08-03 18:11:37 +03:00
outgoing, _ := json.Marshal(&request)
2018-08-29 02:44:06 +03:00
data, status, err := c.PatchRequest("/my/info", []byte(outgoing))
2018-08-03 18:11:37 +03:00
if err != nil {
return resp, status, err
}
if status > http.StatusCreated || status < http.StatusOK {
return resp, status, c.Error(data)
}
if err := json.Unmarshal(data, &resp); err != nil {
return resp, status, err
}
2018-08-03 18:11:37 +03:00
return resp, status, err
}
2018-08-29 02:44:06 +03:00
// Commands get all available commands for bot
//
// Example:
//
// var client = v1.New("https://demo.url", "09jIJ")
//
// data, status, err := client.Commands()
//
// if err != nil {
// fmt.Printf("%v", err)
// }
//
// if status >= http.StatusBadRequest {
// fmt.Printf("%v", err)
// }
//
// for _, command := range data {
// fmt.Printf("%v %v\n", command.Name, command.Description)
// }
func (c *MgClient) Commands(request CommandsRequest) ([]CommandsResponseItem, int, error) {
var resp []CommandsResponseItem
2018-08-03 18:11:37 +03:00
var b []byte
outgoing, _ := query.Values(request)
data, status, err := c.GetRequest(fmt.Sprintf("/my/commands?%s", outgoing.Encode()), b)
if err != nil {
return resp, status, err
}
if status > http.StatusCreated || status < http.StatusOK {
return resp, status, c.Error(data)
}
if err := json.Unmarshal(data, &resp); err != nil {
return resp, status, err
}
2018-08-03 18:11:37 +03:00
return resp, status, err
}
2018-08-29 02:44:06 +03:00
// CommandEdit create or change command for bot
//
// Example:
//
// var client = v1.New("https://demo.url", "09jIJ")
//
// data, status, err := client.CommandEdit(CommandEditRequest{
// BotID: 1,
// Name: "show_payment_types",
// Description: "Get available payment types",
// })
//
// if err != nil {
// fmt.Printf("%v", err)
// }
//
// if status >= http.StatusBadRequest {
// fmt.Printf("%v", err)
// }
//
// fmt.Printf("%v %v\n", data.Name, data.Description)
func (c *MgClient) CommandEdit(request CommandEditRequest) (CommandsResponseItem, int, error) {
var resp CommandsResponseItem
2018-08-03 18:11:37 +03:00
outgoing, _ := json.Marshal(&request)
data, status, err := c.PutRequest(fmt.Sprintf("/my/commands/%s", request.Name), []byte(outgoing))
if err != nil {
return resp, status, err
}
if status > http.StatusCreated || status < http.StatusOK {
return resp, status, c.Error(data)
}
if err := json.Unmarshal(data, &resp); err != nil {
return resp, status, err
}
2018-08-03 18:11:37 +03:00
return resp, status, err
}
2018-08-29 02:44:06 +03:00
// CommandDelete delete selected command for bot
//
// Example:
//
// var client = v1.New("https://demo.url", "09jIJ")
//
// _, status, err := client.CommandDelete(show_payment_types)
//
// if err != nil {
// fmt.Printf("%v", err)
// }
//
// if status >= http.StatusBadRequest {
// fmt.Printf("%v", err)
// }
func (c *MgClient) CommandDelete(request string) (map[string]interface{}, int, error) {
var resp map[string]interface{}
2018-08-03 18:11:37 +03:00
outgoing, _ := json.Marshal(&request)
2018-08-29 02:44:06 +03:00
data, status, err := c.DeleteRequest(fmt.Sprintf("/my/commands/%s", request), []byte(outgoing))
2018-08-03 18:11:37 +03:00
if err != nil {
return resp, status, err
}
if status > http.StatusCreated || status < http.StatusOK {
return resp, status, c.Error(data)
}
if err := json.Unmarshal(data, &resp); err != nil {
return resp, status, err
}
2018-08-03 18:11:37 +03:00
return resp, status, err
}
2018-12-26 17:38:41 +03:00
// 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.ID)
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 status != http.StatusOK {
return resp, status, c.Error(data)
}
if e := json.Unmarshal(data, &resp); e != nil {
return resp, status, e
}
2018-12-26 17:38:41 +03:00
return resp, status, err
}
// UploadFile upload file
//
// Example:
//
// resp, err := http.Get("https://via.placeholder.com/300")
// if err != nil {
// fmt.Printf("%v", err)
// }
//
// defer resp.Body.Close()
//
// var client = v1.New("https://token.url", "cb8ccf05e38a47543ad8477d4999be73bff503ea6")
//
// data, status, err := client.UploadFile(resp.Body)
//
// if err != nil {
// fmt.Printf("%v", err)
// }
//
// fmt.Printf("%s\n%s", data.ID, status)
func (c *MgClient) UploadFile(request io.Reader) (UploadFileResponse, int, error) {
var resp UploadFileResponse
data, status, err := c.PostRequest("/files/upload", request)
if err != nil {
return resp, status, err
}
if status != http.StatusOK {
return resp, status, c.Error(data)
}
if e := json.Unmarshal(data, &resp); e != nil {
return resp, status, e
}
2018-12-26 17:38:41 +03:00
return resp, status, err
}
// UploadFileByURL upload file by url
//
// Example:
//
// uploadFileResponse, st, err := c.UploadFileByURL(UploadFileByUrlRequest{
// Url: "https://via.placeholder.com/300",
// })
//
// if err != nil {
// fmt.Printf("%v", err)
// }
//
// fmt.Printf("%s\n%s", uploadFileResponse.ID, status)
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", bytes.NewBuffer(outgoing))
if err != nil {
return resp, status, err
}
if status != http.StatusOK {
return resp, status, c.Error(data)
}
if e := json.Unmarshal(data, &resp); e != nil {
return resp, status, e
}
2018-12-26 17:38:41 +03:00
return resp, status, err
}
2018-08-31 17:54:53 +03:00
// WsMeta let you receive url & headers to open web socket connection
func (c *MgClient) WsMeta(events []string) (string, http.Header, error) {
2018-09-01 00:41:29 +03:00
var url string
if len(events) < 1 {
err := errors.New("events list must not be empty")
return url, nil, err
}
2018-09-04 11:30:00 +03:00
url = fmt.Sprintf("%s%s%s%s", strings.Replace(c.URL, "https", "wss", 1), prefix, "/ws?events=", strings.Join(events[:], ","))
if url == "" {
err := errors.New("empty WS URL")
2018-09-01 00:41:29 +03:00
return url, nil, err
}
2018-09-01 00:41:29 +03:00
headers := http.Header{}
headers.Add("x-bot-token", c.Token)
2018-08-31 17:54:53 +03:00
return url, headers, nil
}
2018-08-03 18:11:37 +03:00
func (c *MgClient) Error(info []byte) error {
var data map[string]interface{}
if err := json.Unmarshal(info, &data); err != nil {
return err
}
values := data["errors"].([]interface{})
return errors.New(values[0].(string))
}