2018-05-17 17:26:18 +03:00
|
|
|
package v1
|
|
|
|
|
|
|
|
import (
|
2018-12-05 17:14:51 +03:00
|
|
|
"bytes"
|
2018-05-17 17:26:18 +03:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2018-12-05 17:14:51 +03:00
|
|
|
"io"
|
2021-11-22 16:16:01 +03:00
|
|
|
"log"
|
2018-05-17 17:26:18 +03:00
|
|
|
"net/http"
|
2021-11-22 16:08:53 +03:00
|
|
|
"net/url"
|
2018-05-17 17:26:18 +03:00
|
|
|
"strconv"
|
|
|
|
"time"
|
2018-10-01 18:17:03 +03:00
|
|
|
|
|
|
|
"github.com/google/go-querystring/query"
|
2018-05-17 17:26:18 +03:00
|
|
|
)
|
|
|
|
|
2021-11-22 16:08:53 +03:00
|
|
|
// New initialize client.
|
2018-05-17 17:26:18 +03:00
|
|
|
func New(url string, token string) *MgClient {
|
2019-10-18 17:02:41 +03:00
|
|
|
return NewWithClient(url, token, &http.Client{Timeout: time.Minute})
|
|
|
|
}
|
|
|
|
|
2021-11-22 16:08:53 +03:00
|
|
|
// NewWithClient initializes client with provided http client.
|
2019-10-18 17:02:41 +03:00
|
|
|
func NewWithClient(url string, token string, client *http.Client) *MgClient {
|
2018-05-17 17:26:18 +03:00
|
|
|
return &MgClient{
|
2018-06-20 15:27:46 +03:00
|
|
|
URL: url,
|
|
|
|
Token: token,
|
2019-10-18 17:02:41 +03:00
|
|
|
httpClient: client,
|
2018-05-17 17:26:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-22 16:16:01 +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...)
|
|
|
|
}
|
|
|
|
|
2020-04-07 16:34:40 +03:00
|
|
|
// TransportTemplates returns templates list
|
|
|
|
//
|
|
|
|
// Example:
|
|
|
|
//
|
|
|
|
// var client = v1.New("https://token.url", "cb8ccf05e38a47543ad8477d4999be73bff503ea6")
|
|
|
|
//
|
|
|
|
// data, status, err := client.TransportTemplates()
|
|
|
|
//
|
|
|
|
// if err != nil {
|
|
|
|
// fmt.Printf("%v", err)
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// fmt.Printf("Status: %v, Templates found: %v", status, len(data))
|
2020-04-08 13:11:46 +03:00
|
|
|
func (c *MgClient) TransportTemplates() ([]Template, int, error) {
|
|
|
|
var resp []Template
|
2020-04-07 16:34:40 +03:00
|
|
|
|
|
|
|
data, status, err := c.GetRequest("/templates", []byte{})
|
|
|
|
if err != nil {
|
|
|
|
return resp, status, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if e := json.Unmarshal(data, &resp); e != nil {
|
|
|
|
return resp, status, e
|
|
|
|
}
|
|
|
|
|
|
|
|
if status > http.StatusCreated || status < http.StatusOK {
|
|
|
|
return resp, status, c.Error(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, status, err
|
|
|
|
}
|
|
|
|
|
2021-11-22 16:08:53 +03:00
|
|
|
// ActivateTemplate implements template activation
|
2020-04-07 16:34:40 +03:00
|
|
|
//
|
|
|
|
// Example:
|
|
|
|
// var client = v1.New("https://token.url", "cb8ccf05e38a47543ad8477d4999be73bff503ea6")
|
|
|
|
//
|
|
|
|
// request := v1.ActivateTemplateRequest{
|
|
|
|
// Code: "code",
|
|
|
|
// Name: "name",
|
2020-04-07 17:27:04 +03:00
|
|
|
// Type: v1.TemplateTypeText,
|
|
|
|
// Template: []v1.TemplateItem{
|
2020-04-07 16:34:40 +03:00
|
|
|
// {
|
2020-04-07 17:27:04 +03:00
|
|
|
// Type: v1.TemplateItemTypeText,
|
2020-04-07 16:34:40 +03:00
|
|
|
// Text: "Hello, ",
|
|
|
|
// },
|
|
|
|
// {
|
2020-04-07 17:27:04 +03:00
|
|
|
// Type: v1.TemplateItemTypeVar,
|
|
|
|
// VarType: v1.TemplateVarName,
|
2020-04-07 16:34:40 +03:00
|
|
|
// },
|
|
|
|
// {
|
2020-04-07 17:27:04 +03:00
|
|
|
// Type: v1.TemplateItemTypeText,
|
2020-04-07 16:34:40 +03:00
|
|
|
// Text: "!",
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// _, err := client.ActivateTemplate(uint64(1), request)
|
|
|
|
//
|
|
|
|
// if err != nil {
|
|
|
|
// fmt.Printf("%v", err)
|
|
|
|
// }
|
|
|
|
func (c *MgClient) ActivateTemplate(channelID uint64, request ActivateTemplateRequest) (int, error) {
|
|
|
|
outgoing, _ := json.Marshal(&request)
|
|
|
|
|
|
|
|
data, status, err := c.PostRequest(fmt.Sprintf("/channels/%d/templates", channelID), bytes.NewBuffer(outgoing))
|
|
|
|
if err != nil {
|
|
|
|
return status, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if status > http.StatusCreated || status < http.StatusOK {
|
|
|
|
return status, c.Error(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
return status, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateTemplate implements template updating
|
|
|
|
// Example:
|
|
|
|
// var client = New("https://token.url", "cb8ccf05e38a47543ad8477d4999be73bff503ea6")
|
|
|
|
//
|
2020-04-07 17:27:04 +03:00
|
|
|
// request := v1.Template{
|
2020-04-07 16:34:40 +03:00
|
|
|
// Code: "templateCode",
|
|
|
|
// ChannelID: 1,
|
|
|
|
// Name: "templateName",
|
2020-04-07 17:27:04 +03:00
|
|
|
// Template: []v1.TemplateItem{
|
2020-04-07 16:34:40 +03:00
|
|
|
// {
|
2020-04-07 17:27:04 +03:00
|
|
|
// Type: v1.TemplateItemTypeText,
|
2020-04-07 16:34:40 +03:00
|
|
|
// Text: "Welcome, ",
|
|
|
|
// },
|
|
|
|
// {
|
2020-04-07 17:27:04 +03:00
|
|
|
// Type: v1.TemplateItemTypeVar,
|
|
|
|
// VarType: v1.TemplateVarName,
|
2020-04-07 16:34:40 +03:00
|
|
|
// },
|
|
|
|
// {
|
2020-04-07 17:27:04 +03:00
|
|
|
// Type: v1.TemplateItemTypeText,
|
2020-04-07 16:34:40 +03:00
|
|
|
// Text: "!",
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// _, err := client.UpdateTemplate(request)
|
|
|
|
//
|
|
|
|
// if err != nil {
|
|
|
|
// fmt.Printf("%#v", err)
|
|
|
|
// }
|
2020-04-07 17:27:04 +03:00
|
|
|
func (c *MgClient) UpdateTemplate(request Template) (int, error) {
|
2020-04-07 16:34:40 +03:00
|
|
|
outgoing, _ := json.Marshal(&request)
|
|
|
|
|
2020-05-11 17:41:17 +03:00
|
|
|
if request.ChannelID == 0 || request.Code == "" {
|
|
|
|
return 0, errors.New("`ChannelID` and `Code` cannot be blank")
|
|
|
|
}
|
|
|
|
|
2021-11-22 16:08:53 +03:00
|
|
|
data, status, err := c.PutRequest(
|
|
|
|
fmt.Sprintf("/channels/%d/templates/%s", request.ChannelID, url.PathEscape(request.Code)), outgoing)
|
2020-04-07 16:34:40 +03:00
|
|
|
if err != nil {
|
|
|
|
return status, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if status != http.StatusOK {
|
|
|
|
return status, c.Error(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
return status, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeactivateTemplate implements template deactivation
|
|
|
|
//
|
|
|
|
// Example:
|
|
|
|
//
|
|
|
|
// var client = v1.New("https://token.url", "cb8ccf05e38a47543ad8477d4999be73bff503ea6")
|
|
|
|
//
|
|
|
|
// _, err := client.DeactivateTemplate(3053450384, "templateCode")
|
|
|
|
//
|
|
|
|
// if err != nil {
|
|
|
|
// fmt.Printf("%v", err)
|
|
|
|
// }
|
|
|
|
func (c *MgClient) DeactivateTemplate(channelID uint64, templateCode string) (int, error) {
|
|
|
|
data, status, err := c.DeleteRequest(
|
2021-11-22 16:08:53 +03:00
|
|
|
fmt.Sprintf("/channels/%d/templates/%s", channelID, url.PathEscape(templateCode)), []byte{})
|
2020-04-07 16:34:40 +03:00
|
|
|
if err != nil {
|
|
|
|
return status, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if status > http.StatusCreated || status < http.StatusOK {
|
|
|
|
return status, c.Error(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
return status, err
|
|
|
|
}
|
|
|
|
|
2020-04-08 13:11:46 +03:00
|
|
|
// TransportChannels returns channels list
|
|
|
|
//
|
|
|
|
// Example:
|
|
|
|
//
|
|
|
|
// var client = v1.New("https://token.url", "cb8ccf05e38a47543ad8477d4999be73bff503ea6")
|
|
|
|
//
|
|
|
|
// data, status, err := client.TransportChannels(Channels{Active: true})
|
|
|
|
//
|
|
|
|
// if err != nil {
|
|
|
|
// fmt.Printf("%v", err)
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// fmt.Printf("Status: %v, Channels found: %v", status, len(data))
|
|
|
|
func (c *MgClient) TransportChannels(request Channels) ([]ChannelListItem, int, error) {
|
|
|
|
var resp []ChannelListItem
|
|
|
|
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 e := json.Unmarshal(data, &resp); e != nil {
|
|
|
|
return resp, status, e
|
|
|
|
}
|
|
|
|
|
|
|
|
if status > http.StatusCreated || status < http.StatusOK {
|
|
|
|
return resp, status, c.Error(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, status, err
|
|
|
|
}
|
|
|
|
|
2018-05-17 17:26:18 +03:00
|
|
|
// ActivateTransportChannel implement channel activation
|
|
|
|
//
|
|
|
|
// Example:
|
|
|
|
//
|
2018-10-03 09:48:07 +03:00
|
|
|
// var client = v1.New("https://token.url", "cb8ccf05e38a47543ad8477d4999be73bff503ea6")
|
2018-05-17 17:26:18 +03:00
|
|
|
//
|
|
|
|
// request := ActivateRequest{
|
|
|
|
// Type: "telegram",
|
2018-09-10 12:06:57 +03:00
|
|
|
// Name: "@my_shopping_bot",
|
2018-08-16 16:53:29 +03:00
|
|
|
// Settings: ChannelSettings{
|
2018-08-21 13:37:49 +03:00
|
|
|
// Status: Status{
|
|
|
|
// Delivered: ChannelFeatureNone,
|
|
|
|
// Read: ChannelFeatureReceive,
|
|
|
|
// },
|
|
|
|
// Text: ChannelSettingsText{
|
|
|
|
// Creating: ChannelFeatureBoth,
|
|
|
|
// Editing: ChannelFeatureBoth,
|
|
|
|
// Quoting: ChannelFeatureReceive,
|
|
|
|
// Deleting: ChannelFeatureSend,
|
2018-12-05 10:57:45 +03:00
|
|
|
// MaxCharsCount: 2000,
|
2018-08-16 16:53:29 +03:00
|
|
|
// },
|
2018-09-19 18:12:37 +03:00
|
|
|
// Product: Product{
|
|
|
|
// Creating: ChannelFeatureSend,
|
|
|
|
// Deleting: ChannelFeatureSend,
|
|
|
|
// },
|
|
|
|
// Order: Order{
|
|
|
|
// Creating: ChannelFeatureBoth,
|
|
|
|
// Deleting: ChannelFeatureSend,
|
|
|
|
// },
|
2018-08-16 16:53:29 +03:00
|
|
|
// },
|
2018-05-17 17:26:18 +03:00
|
|
|
// }
|
|
|
|
//
|
|
|
|
// data, status, err := client.ActivateTransportChannel(request)
|
|
|
|
//
|
|
|
|
// if err != nil {
|
|
|
|
// fmt.Printf("%v", err)
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// fmt.Printf("%s\n", data.CreatedAt)
|
|
|
|
func (c *MgClient) ActivateTransportChannel(request Channel) (ActivateResponse, int, error) {
|
|
|
|
var resp ActivateResponse
|
|
|
|
outgoing, _ := json.Marshal(&request)
|
|
|
|
|
2018-12-05 17:14:51 +03:00
|
|
|
data, status, err := c.PostRequest("/channels", bytes.NewBuffer(outgoing))
|
2018-05-17 17:26:18 +03:00
|
|
|
if err != nil {
|
|
|
|
return resp, status, err
|
|
|
|
}
|
|
|
|
|
2018-10-03 09:48:07 +03:00
|
|
|
if e := json.Unmarshal(data, &resp); e != nil {
|
|
|
|
return resp, status, e
|
2018-05-17 17:26:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if status > http.StatusCreated || status < http.StatusOK {
|
|
|
|
return resp, status, c.Error(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, status, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateTransportChannel implement channel activation
|
|
|
|
//
|
|
|
|
// Example:
|
|
|
|
//
|
2018-10-03 09:48:07 +03:00
|
|
|
// var client = v1.New("https://token.url", "cb8ccf05e38a47543ad8477d4999be73bff503ea6")
|
2018-05-17 17:26:18 +03:00
|
|
|
//
|
|
|
|
// request := ActivateRequest{
|
|
|
|
// ID: 3053450384,
|
|
|
|
// Type: "telegram",
|
2018-09-10 12:06:57 +03:00
|
|
|
// Name: "@my_shopping_bot",
|
2018-08-16 16:53:29 +03:00
|
|
|
// Settings: ChannelSettings{
|
2018-08-21 13:37:49 +03:00
|
|
|
// Status: Status{
|
|
|
|
// Delivered: ChannelFeatureNone,
|
|
|
|
// Read: ChannelFeatureReceive,
|
|
|
|
// },
|
|
|
|
// Text: ChannelSettingsText{
|
|
|
|
// Creating: ChannelFeatureBoth,
|
|
|
|
// Editing: ChannelFeatureSend,
|
|
|
|
// Quoting: ChannelFeatureReceive,
|
|
|
|
// Deleting: ChannelFeatureBoth,
|
2018-08-16 16:53:29 +03:00
|
|
|
// },
|
2018-09-19 18:12:37 +03:00
|
|
|
// Product: Product{
|
|
|
|
// Creating: ChannelFeatureSend,
|
|
|
|
// Deleting: ChannelFeatureSend,
|
|
|
|
// },
|
|
|
|
// Order: Order{
|
|
|
|
// Creating: ChannelFeatureBoth,
|
|
|
|
// Deleting: ChannelFeatureSend,
|
|
|
|
// },
|
2018-08-16 16:53:29 +03:00
|
|
|
// },
|
2018-05-17 17:26:18 +03:00
|
|
|
// }
|
|
|
|
//
|
|
|
|
// data, status, err := client.UpdateTransportChannel(request)
|
|
|
|
//
|
|
|
|
// if err != nil {
|
|
|
|
// fmt.Printf("%v", err)
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// fmt.Printf("%s\n", data.UpdatedAt)
|
|
|
|
func (c *MgClient) UpdateTransportChannel(request Channel) (UpdateResponse, int, error) {
|
|
|
|
var resp UpdateResponse
|
|
|
|
outgoing, _ := json.Marshal(&request)
|
|
|
|
|
2021-11-22 16:08:53 +03:00
|
|
|
data, status, err := c.PutRequest(fmt.Sprintf("/channels/%d", request.ID), outgoing)
|
2018-05-17 17:26:18 +03:00
|
|
|
if err != nil {
|
|
|
|
return resp, status, err
|
|
|
|
}
|
|
|
|
|
2018-10-03 09:48:07 +03:00
|
|
|
if e := json.Unmarshal(data, &resp); e != nil {
|
|
|
|
return resp, status, e
|
2018-05-17 17:26:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if status != http.StatusOK {
|
|
|
|
return resp, status, c.Error(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, status, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeactivateTransportChannel implement channel deactivation
|
|
|
|
//
|
|
|
|
// Example:
|
|
|
|
//
|
2018-10-03 09:48:07 +03:00
|
|
|
// var client = v1.New("https://token.url", "cb8ccf05e38a47543ad8477d4999be73bff503ea6")
|
2018-05-17 17:26:18 +03:00
|
|
|
//
|
|
|
|
// data, status, err := client.DeactivateTransportChannel(3053450384)
|
|
|
|
//
|
|
|
|
// if err != nil {
|
|
|
|
// fmt.Printf("%v", err)
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// fmt.Printf("%s\n", data.DeactivatedAt)
|
|
|
|
func (c *MgClient) DeactivateTransportChannel(id uint64) (DeleteResponse, int, error) {
|
|
|
|
var resp DeleteResponse
|
2018-05-21 17:56:42 +03:00
|
|
|
var buf []byte
|
2018-05-17 17:26:18 +03:00
|
|
|
|
2018-05-21 17:56:42 +03:00
|
|
|
data, status, err := c.DeleteRequest(
|
2018-07-21 18:56:25 +03:00
|
|
|
fmt.Sprintf("/channels/%s", strconv.FormatUint(id, 10)),
|
2018-05-21 17:56:42 +03:00
|
|
|
buf,
|
|
|
|
)
|
2018-05-17 17:26:18 +03:00
|
|
|
if err != nil {
|
|
|
|
return resp, status, err
|
|
|
|
}
|
|
|
|
|
2018-10-03 09:48:07 +03:00
|
|
|
if e := json.Unmarshal(data, &resp); e != nil {
|
|
|
|
return resp, status, e
|
2018-05-17 17:26:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if status != http.StatusOK {
|
|
|
|
return resp, status, c.Error(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, status, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Messages implement send message
|
|
|
|
//
|
|
|
|
// Example:
|
|
|
|
//
|
2018-10-03 09:48:07 +03:00
|
|
|
// var client = v1.New("https://token.url", "cb8ccf05e38a47543ad8477d4999be73bff503ea6")
|
2018-05-21 17:56:42 +03:00
|
|
|
// msg := SendData{
|
2018-05-17 17:26:18 +03:00
|
|
|
// SendMessage{
|
|
|
|
// Message{
|
2018-05-21 17:56:42 +03:00
|
|
|
// ExternalID: "274628",
|
2018-05-17 17:26:18 +03:00
|
|
|
// Type: "text",
|
|
|
|
// Text: "hello!",
|
|
|
|
// },
|
|
|
|
// time.Now(),
|
|
|
|
// },
|
|
|
|
// User{
|
|
|
|
// ExternalID: "8",
|
2018-05-21 17:56:42 +03:00
|
|
|
// Nickname: "@octopus",
|
2018-05-17 17:26:18 +03:00
|
|
|
// Firstname: "Joe",
|
|
|
|
// },
|
2018-05-21 17:56:42 +03:00
|
|
|
// 10,
|
2018-05-17 17:26:18 +03:00
|
|
|
// }
|
|
|
|
//
|
2018-05-21 17:56:42 +03:00
|
|
|
// data, status, err := client.Messages(msg)
|
2018-05-17 17:26:18 +03:00
|
|
|
//
|
|
|
|
// if err != nil {
|
|
|
|
// fmt.Printf("%v", err)
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// fmt.Printf("%s\n", data.MessageID)
|
|
|
|
func (c *MgClient) Messages(request SendData) (MessagesResponse, int, error) {
|
|
|
|
var resp MessagesResponse
|
|
|
|
outgoing, _ := json.Marshal(&request)
|
|
|
|
|
2018-12-05 17:14:51 +03:00
|
|
|
data, status, err := c.PostRequest("/messages", bytes.NewBuffer(outgoing))
|
2018-05-17 17:26:18 +03:00
|
|
|
if err != nil {
|
|
|
|
return resp, status, err
|
|
|
|
}
|
|
|
|
|
2018-10-03 09:48:07 +03:00
|
|
|
if e := json.Unmarshal(data, &resp); e != nil {
|
|
|
|
return resp, status, e
|
2018-05-17 17:26:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if status != http.StatusOK {
|
|
|
|
return resp, status, c.Error(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, status, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateMessages implement edit message
|
|
|
|
//
|
|
|
|
// Example:
|
|
|
|
//
|
2018-10-03 09:48:07 +03:00
|
|
|
// var client = v1.New("https://token.url", "cb8ccf05e38a47543ad8477d4999be73bff503ea6")
|
2018-05-21 17:56:42 +03:00
|
|
|
// msg := UpdateData{
|
|
|
|
// UpdateMessage{
|
2018-05-17 17:26:18 +03:00
|
|
|
// Message{
|
2018-05-21 17:56:42 +03:00
|
|
|
// ExternalID: "274628",
|
2018-05-17 17:26:18 +03:00
|
|
|
// Type: "text",
|
2018-05-21 17:56:42 +03:00
|
|
|
// Text: "hello hello!",
|
2018-05-17 17:26:18 +03:00
|
|
|
// },
|
2018-08-16 16:53:29 +03:00
|
|
|
// MakeTimestamp(),
|
2018-05-17 17:26:18 +03:00
|
|
|
// },
|
2018-05-21 17:56:42 +03:00
|
|
|
// 10,
|
2018-05-17 17:26:18 +03:00
|
|
|
// }
|
|
|
|
//
|
2018-05-21 17:56:42 +03:00
|
|
|
// data, status, err := client.UpdateMessages(msg)
|
2018-05-17 17:26:18 +03:00
|
|
|
//
|
|
|
|
// if err != nil {
|
|
|
|
// fmt.Printf("%v", err)
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// fmt.Printf("%s\n", data.MessageID)
|
2018-10-03 09:48:07 +03:00
|
|
|
func (c *MgClient) UpdateMessages(request EditMessageRequest) (MessagesResponse, int, error) {
|
2018-05-17 17:26:18 +03:00
|
|
|
var resp MessagesResponse
|
|
|
|
outgoing, _ := json.Marshal(&request)
|
|
|
|
|
2021-11-22 16:08:53 +03:00
|
|
|
data, status, err := c.PutRequest("/messages", outgoing)
|
2018-05-17 17:26:18 +03:00
|
|
|
if err != nil {
|
|
|
|
return resp, status, err
|
|
|
|
}
|
|
|
|
|
2018-10-03 09:48:07 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarkMessageRead send message read event to MG
|
|
|
|
//
|
|
|
|
// Example:
|
|
|
|
//
|
|
|
|
// var client = v1.New("https://token.url", "cb8ccf05e38a47543ad8477d4999be73bff503ea6")
|
|
|
|
// msg := MarkMessageReadRequest{
|
|
|
|
// Message{
|
|
|
|
// ExternalID: "274628",
|
|
|
|
// },
|
|
|
|
// 10,
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// data, status, err := client.MarkMessageRead(msg)
|
|
|
|
//
|
|
|
|
// if err != nil {
|
|
|
|
// fmt.Printf("%v", err)
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// fmt.Printf("%v %v\n", status, data)
|
|
|
|
func (c *MgClient) MarkMessageRead(request MarkMessageReadRequest) (MarkMessageReadResponse, int, error) {
|
|
|
|
var resp MarkMessageReadResponse
|
|
|
|
outgoing, _ := json.Marshal(&request)
|
|
|
|
|
2018-12-05 17:14:51 +03:00
|
|
|
data, status, err := c.PostRequest("/messages/read", bytes.NewBuffer(outgoing))
|
2018-10-03 09:48:07 +03:00
|
|
|
if err != nil {
|
2018-05-17 17:26:18 +03:00
|
|
|
return resp, status, err
|
|
|
|
}
|
|
|
|
|
2018-10-03 09:48:07 +03:00
|
|
|
if e := json.Unmarshal(data, &resp); e != nil {
|
|
|
|
return resp, status, e
|
|
|
|
}
|
|
|
|
|
2018-05-17 17:26:18 +03:00
|
|
|
if status != http.StatusOK {
|
|
|
|
return resp, status, c.Error(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, status, err
|
|
|
|
}
|
|
|
|
|
2020-12-23 11:54:46 +03:00
|
|
|
// AckMessage implements ack of message
|
|
|
|
//
|
|
|
|
// Example:
|
|
|
|
//
|
|
|
|
// var client = v1.New("https://token.url", "cb8ccf05e38a47543ad8477d4999be73bff503ea6")
|
|
|
|
//
|
|
|
|
// request := AckMessageRequest{
|
|
|
|
// ExternalMessageID: "274628",
|
|
|
|
// Channel: 10,
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// status, err := client.AckMessage(request)
|
|
|
|
//
|
|
|
|
// if err != nil {
|
|
|
|
// fmt.Printf("%v", err)
|
|
|
|
// }
|
|
|
|
func (c *MgClient) AckMessage(request AckMessageRequest) (int, error) {
|
|
|
|
outgoing, _ := json.Marshal(&request)
|
|
|
|
|
|
|
|
data, status, err := c.PostRequest("/messages/ack", bytes.NewBuffer(outgoing))
|
|
|
|
if err != nil {
|
|
|
|
return status, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if status != http.StatusOK {
|
|
|
|
return status, c.Error(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
return status, err
|
|
|
|
}
|
|
|
|
|
2018-05-17 17:26:18 +03:00
|
|
|
// DeleteMessage implement delete message
|
|
|
|
//
|
|
|
|
// Example:
|
|
|
|
//
|
2018-10-03 09:48:07 +03:00
|
|
|
// var client = v1.New("https://token.url", "cb8ccf05e38a47543ad8477d4999be73bff503ea6")
|
2018-05-17 17:26:18 +03:00
|
|
|
//
|
2018-05-21 17:56:42 +03:00
|
|
|
// msg := DeleteData{
|
|
|
|
// Message{
|
|
|
|
// ExternalID: "274628",
|
|
|
|
// },
|
|
|
|
// 10,
|
|
|
|
// }
|
|
|
|
//
|
2021-07-27 15:31:49 +03:00
|
|
|
// previousChatMessage, status, err := client.DeleteMessage(msg)
|
2018-05-17 17:26:18 +03:00
|
|
|
// if err != nil {
|
|
|
|
// fmt.Printf("%v", err)
|
|
|
|
// }
|
|
|
|
//
|
2021-07-27 15:31:49 +03:00
|
|
|
// if previousChatMessage != nil {
|
|
|
|
// fmt.Printf("Previous chat message id = %d", previousChatMessage.MessageID)
|
|
|
|
// }
|
|
|
|
func (c *MgClient) DeleteMessage(request DeleteData) (*MessagesResponse, int, error) {
|
2018-05-21 17:56:42 +03:00
|
|
|
outgoing, _ := json.Marshal(&request)
|
2018-05-17 17:26:18 +03:00
|
|
|
|
2018-05-21 17:56:42 +03:00
|
|
|
data, status, err := c.DeleteRequest(
|
2018-08-17 15:12:54 +03:00
|
|
|
"/messages",
|
2021-11-22 16:08:53 +03:00
|
|
|
outgoing,
|
2018-05-21 17:56:42 +03:00
|
|
|
)
|
2018-05-17 17:26:18 +03:00
|
|
|
if err != nil {
|
2021-07-27 15:31:49 +03:00
|
|
|
return nil, status, err
|
2018-05-17 17:26:18 +03:00
|
|
|
}
|
2021-07-27 15:31:49 +03:00
|
|
|
if status != http.StatusOK {
|
|
|
|
return nil, status, c.Error(data)
|
2018-05-17 17:26:18 +03:00
|
|
|
}
|
|
|
|
|
2021-07-27 15:31:49 +03:00
|
|
|
var previousChatMessage *MessagesResponse
|
|
|
|
if e := json.Unmarshal(data, &previousChatMessage); e != nil {
|
|
|
|
return nil, status, e
|
2018-05-17 17:26:18 +03:00
|
|
|
}
|
|
|
|
|
2021-07-27 15:31:49 +03:00
|
|
|
return previousChatMessage, status, nil
|
2018-05-17 17:26:18 +03:00
|
|
|
}
|
|
|
|
|
2018-11-08 17:17:24 +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.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
|
|
|
|
}
|
|
|
|
|
2021-11-22 16:08:53 +03:00
|
|
|
// UploadFile upload file.
|
2018-12-05 17:14:51 +03:00
|
|
|
func (c *MgClient) UploadFile(request io.Reader) (UploadFileResponse, int, error) {
|
2018-11-08 17:17:24 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-11-22 16:08:53 +03:00
|
|
|
// UploadFileByURL upload file by url.
|
2018-11-08 17:17:24 +03:00
|
|
|
func (c *MgClient) UploadFileByURL(request UploadFileByUrlRequest) (UploadFileResponse, int, error) {
|
|
|
|
var resp UploadFileResponse
|
|
|
|
outgoing, _ := json.Marshal(&request)
|
|
|
|
|
2018-12-05 17:14:51 +03:00
|
|
|
data, status, err := c.PostRequest("/files/upload_by_url", bytes.NewBuffer(outgoing))
|
2018-11-08 17:17:24 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-05-17 17:26:18 +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))
|
|
|
|
}
|
2018-08-16 16:53:29 +03:00
|
|
|
|
2021-11-22 16:08:53 +03:00
|
|
|
// MakeTimestamp returns current unix timestamp.
|
2018-08-16 16:53:29 +03:00
|
|
|
func MakeTimestamp() int64 {
|
|
|
|
return time.Now().UnixNano() / (int64(time.Millisecond) / int64(time.Nanosecond))
|
|
|
|
}
|