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

673 lines
15 KiB
Go
Raw Permalink Normal View History

2018-08-03 18:11:37 +03:00
package v1
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"time"
"github.com/google/go-querystring/query"
)
// New initialize client
func New(url string, token string) *MgClient {
return &MgClient{
URL: url,
Token: token,
httpClient: &http.Client{Timeout: 20 * time.Second},
}
}
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 err := json.Unmarshal(data, &resp); err != nil {
return resp, status, err
}
if status > http.StatusCreated || status < http.StatusOK {
return resp, status, c.Error(data)
}
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 err := json.Unmarshal(data, &resp); err != nil {
return resp, status, err
}
if status > http.StatusCreated || status < http.StatusOK {
return resp, status, c.Error(data)
}
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 err := json.Unmarshal(data, &resp); err != nil {
return resp, status, err
}
if status > http.StatusCreated || status < http.StatusOK {
return resp, status, c.Error(data)
}
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 err := json.Unmarshal(data, &resp); err != nil {
return resp, status, err
}
if status > http.StatusCreated || status < http.StatusOK {
return resp, status, c.Error(data)
}
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 err := json.Unmarshal(data, &resp); err != nil {
return resp, status, err
}
if status > http.StatusCreated || status < http.StatusOK {
return resp, status, c.Error(data)
}
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 err := json.Unmarshal(data, &resp); err != nil {
return resp, status, err
}
if status > http.StatusCreated || status < http.StatusOK {
return resp, status, c.Error(data)
}
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 err := json.Unmarshal(data, &resp); err != nil {
return resp, status, err
}
if status > http.StatusCreated || status < http.StatusOK {
return resp, status, c.Error(data)
}
return resp, status, err
}
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 err := json.Unmarshal(data, &resp); err != nil {
return resp, status, err
}
if status > http.StatusCreated || status < http.StatusOK {
return resp, status, c.Error(data)
}
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 err := json.Unmarshal(data, &resp); err != nil {
return resp, status, err
}
if status > http.StatusCreated || status < http.StatusOK {
return resp, status, c.Error(data)
}
return resp, status, err
}
2018-08-29 02:44:06 +03:00
// Messages get all available messages
//
// Example:
//
// var client = v1.New("https://demo.url", "09jIJ")
//
// data, status, err := client.Messages(MessagesRequest{ManagerID:5})
//
// 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 err := json.Unmarshal(data, &resp); err != nil {
return resp, status, err
}
if status > http.StatusCreated || status < http.StatusOK {
return resp, status, c.Error(data)
}
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)
data, status, err := c.PostRequest("/messages", []byte(outgoing))
if err != nil {
return resp, status, err
}
if err := json.Unmarshal(data, &resp); err != nil {
return resp, status, err
}
if status > http.StatusCreated || status < http.StatusOK {
return resp, status, c.Error(data)
}
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 err := json.Unmarshal(data, &resp); err != nil {
return resp, status, err
}
if status > http.StatusCreated || status < http.StatusOK {
return resp, status, c.Error(data)
}
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 err := json.Unmarshal(data, &resp); err != nil {
return resp, status, err
}
if status > http.StatusCreated || status < http.StatusOK {
return resp, status, c.Error(data)
}
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 err := json.Unmarshal(data, &resp); err != nil {
return resp, status, err
}
if status > http.StatusCreated || status < http.StatusOK {
return resp, status, c.Error(data)
}
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 err := json.Unmarshal(data, &resp); err != nil {
return resp, status, err
}
if status > http.StatusCreated || status < http.StatusOK {
return resp, status, c.Error(data)
}
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 err := json.Unmarshal(data, &resp); err != nil {
return resp, status, err
}
if status > http.StatusCreated || status < http.StatusOK {
return resp, status, c.Error(data)
}
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 err := json.Unmarshal(data, &resp); err != nil {
return resp, status, err
}
if status > http.StatusCreated || 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{}
if err := json.Unmarshal(info, &data); err != nil {
return err
}
values := data["errors"].([]interface{})
return errors.New(values[0].(string))
}