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
|
|
|
)
|
|
|
|
|
2023-12-27 18:21:46 +03:00
|
|
|
// New initializes the MgClient.
|
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})
|
|
|
|
}
|
|
|
|
|
2023-12-27 18:21:46 +03:00
|
|
|
// NewWithClient initializes the MgClient with specified *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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-27 18:21:46 +03:00
|
|
|
// WithLogger sets the provided logger instance into the Client.
|
2021-11-22 16:16:01 +03:00
|
|
|
func (c *MgClient) WithLogger(logger BasicLogger) *MgClient {
|
|
|
|
c.logger = logger
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2024-02-15 21:59:10 +03:00
|
|
|
// WithLimiter sets the provided limiter instance into the Client.
|
|
|
|
func (c *MgClient) WithLimiter(limiter *TokensBucket) *MgClient {
|
|
|
|
c.limiter = limiter
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2023-12-27 18:21:46 +03:00
|
|
|
// writeLog writes a message to the log.
|
2021-11-22 16:16:01 +03:00
|
|
|
func (c *MgClient) writeLog(format string, v ...interface{}) {
|
|
|
|
if c.logger != nil {
|
|
|
|
c.logger.Printf(format, v...)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf(format, v...)
|
|
|
|
}
|
|
|
|
|
2023-12-27 18:21:46 +03:00
|
|
|
// TransportTemplates returns templates list.
|
2020-04-07 16:34:40 +03:00
|
|
|
//
|
|
|
|
// Example:
|
|
|
|
//
|
2023-12-28 14:26:49 +03:00
|
|
|
// client := New("https://message-gateway.url", "cb8ccf05e38a47543ad8477d4999be73bff503ea6")
|
2020-04-07 16:34:40 +03:00
|
|
|
//
|
2022-11-02 17:00:13 +03:00
|
|
|
// data, status, err := client.TransportTemplates()
|
|
|
|
// if err != nil {
|
2023-12-27 18:21:46 +03:00
|
|
|
// log.Fatalf("request error: %s (%d)", err, status)
|
2022-11-02 17:00:13 +03:00
|
|
|
// }
|
2020-04-07 16:34:40 +03:00
|
|
|
//
|
2023-12-27 18:21:46 +03:00
|
|
|
// log.Printf("status: %d, response: %#v", status, 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 {
|
2022-10-26 18:07:04 +03:00
|
|
|
return resp, status, NewAPIClientError(data)
|
2020-04-07 16:34:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return resp, status, err
|
|
|
|
}
|
|
|
|
|
2023-12-28 14:26:49 +03:00
|
|
|
// ActivateTemplate activates template with provided structure.
|
2020-04-07 16:34:40 +03:00
|
|
|
//
|
|
|
|
// Example:
|
2022-11-02 17:00:13 +03:00
|
|
|
//
|
2023-12-28 14:26:49 +03:00
|
|
|
// client := New("https://message-gateway.url", "cb8ccf05e38a47543ad8477d4999be73bff503ea6")
|
2022-11-02 17:00:13 +03:00
|
|
|
//
|
2023-12-28 14:26:49 +03:00
|
|
|
// status, err := client.ActivateTemplate(1, ActivateTemplateRequest{
|
|
|
|
// UpdateTemplateRequest: UpdateTemplateRequest{
|
2023-12-27 18:21:46 +03:00
|
|
|
// Name: "New Template",
|
|
|
|
// Body: "Hello, {{1}}! Welcome to our store!",
|
|
|
|
// Lang: "en",
|
|
|
|
// Category: "marketing",
|
2023-12-28 14:26:49 +03:00
|
|
|
// Example: &TemplateExample{
|
2023-12-27 18:21:46 +03:00
|
|
|
// Header: []string{"https://example.com/image.png"},
|
|
|
|
// Body: []string{"John"},
|
2022-11-02 17:00:13 +03:00
|
|
|
// },
|
2023-12-28 14:26:49 +03:00
|
|
|
// VerificationStatus: TemplateStatusApproved,
|
|
|
|
// Header: &TemplateHeader{
|
|
|
|
// Content: HeaderContentImage{},
|
2023-12-27 18:21:46 +03:00
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// Code: "new_template",
|
2023-12-28 14:26:49 +03:00
|
|
|
// Type: TemplateTypeMedia,
|
2023-12-27 18:21:46 +03:00
|
|
|
// })
|
2022-11-02 17:00:13 +03:00
|
|
|
// if err != nil {
|
2023-12-27 18:21:46 +03:00
|
|
|
// log.Fatalf("request error: %s (%d)", err, status)
|
2022-11-02 17:00:13 +03:00
|
|
|
// }
|
2023-12-27 18:21:46 +03:00
|
|
|
//
|
|
|
|
// log.Printf("status: %d", status)
|
2020-04-07 16:34:40 +03:00
|
|
|
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 {
|
2022-10-26 18:07:04 +03:00
|
|
|
return status, NewAPIClientError(data)
|
2020-04-07 16:34:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return status, err
|
|
|
|
}
|
|
|
|
|
2023-12-28 14:26:49 +03:00
|
|
|
// UpdateTemplate updates existing template by its code.
|
|
|
|
//
|
2020-04-07 16:34:40 +03:00
|
|
|
// Example:
|
2022-11-02 17:00:13 +03:00
|
|
|
//
|
2023-12-28 14:26:49 +03:00
|
|
|
// client := New("https://message-gateway.url", "cb8ccf05e38a47543ad8477d4999be73bff503ea6")
|
2022-11-02 17:00:13 +03:00
|
|
|
//
|
2023-12-28 14:26:49 +03:00
|
|
|
// status, err := client.UpdateTemplate(1, "new_template", UpdateTemplateRequest{
|
|
|
|
// Name: "New Template",
|
|
|
|
// Body: "Hello, {{1}}! Welcome to our store!",
|
|
|
|
// Lang: "en",
|
|
|
|
// Category: "marketing",
|
|
|
|
// Example: &TemplateExample{
|
|
|
|
// Header: []string{"https://example.com/image.png"},
|
|
|
|
// Body: []string{"John"},
|
2022-11-02 17:00:13 +03:00
|
|
|
// },
|
2023-12-28 14:26:49 +03:00
|
|
|
// VerificationStatus: TemplateStatusApproved,
|
|
|
|
// Header: &TemplateHeader{
|
|
|
|
// Content: HeaderContentImage{},
|
|
|
|
// },
|
|
|
|
// })
|
2022-11-02 17:00:13 +03:00
|
|
|
// if err != nil {
|
2023-12-28 14:26:49 +03:00
|
|
|
// log.Fatalf("request error: %s (%d)", err, status)
|
2022-11-02 17:00:13 +03:00
|
|
|
// }
|
2023-12-28 14:26:49 +03:00
|
|
|
//
|
|
|
|
// log.Printf("status: %d", status)
|
2023-10-25 19:07:05 +03:00
|
|
|
func (c *MgClient) UpdateTemplate(channelID uint64, code string, request UpdateTemplateRequest) (int, error) {
|
2020-04-07 16:34:40 +03:00
|
|
|
outgoing, _ := json.Marshal(&request)
|
|
|
|
|
2023-10-25 19:07:05 +03:00
|
|
|
if channelID == 0 || code == "" {
|
2020-05-11 17:41:17 +03:00
|
|
|
return 0, errors.New("`ChannelID` and `Code` cannot be blank")
|
|
|
|
}
|
|
|
|
|
2021-11-22 16:08:53 +03:00
|
|
|
data, status, err := c.PutRequest(
|
2023-10-25 19:07:05 +03:00
|
|
|
fmt.Sprintf("/channels/%d/templates/%s", channelID, url.PathEscape(code)), outgoing)
|
2020-04-07 16:34:40 +03:00
|
|
|
if err != nil {
|
|
|
|
return status, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if status != http.StatusOK {
|
2022-10-26 18:07:04 +03:00
|
|
|
return status, NewAPIClientError(data)
|
2020-04-07 16:34:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return status, err
|
|
|
|
}
|
|
|
|
|
2023-12-28 14:26:49 +03:00
|
|
|
// DeactivateTemplate deactivates the template by its code.
|
2020-04-07 16:34:40 +03:00
|
|
|
//
|
|
|
|
// Example:
|
|
|
|
//
|
2023-12-28 14:26:49 +03:00
|
|
|
// client := New("https://message-gateway.url", "cb8ccf05e38a47543ad8477d4999be73bff503ea6")
|
2020-04-07 16:34:40 +03:00
|
|
|
//
|
2023-12-28 14:26:49 +03:00
|
|
|
// status, err := client.DeactivateTemplate(1, "new_template")
|
2022-11-02 17:00:13 +03:00
|
|
|
// if err != nil {
|
2023-12-28 14:26:49 +03:00
|
|
|
// log.Fatalf("request error: %s (%d)", err, status)
|
2022-11-02 17:00:13 +03:00
|
|
|
// }
|
2023-12-28 14:26:49 +03:00
|
|
|
//
|
|
|
|
// log.Printf("status: %d", status)
|
2020-04-07 16:34:40 +03:00
|
|
|
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 {
|
2022-10-26 18:07:04 +03:00
|
|
|
return status, NewAPIClientError(data)
|
2020-04-07 16:34:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return status, err
|
|
|
|
}
|
|
|
|
|
2023-12-28 14:26:49 +03:00
|
|
|
// TransportChannels returns channels for current transport.
|
2020-04-08 13:11:46 +03:00
|
|
|
//
|
|
|
|
// Example:
|
|
|
|
//
|
2023-12-28 14:26:49 +03:00
|
|
|
// client := New("https://message-gateway.url", "cb8ccf05e38a47543ad8477d4999be73bff503ea6")
|
2020-04-08 13:11:46 +03:00
|
|
|
//
|
2023-12-28 14:26:49 +03:00
|
|
|
// resp, status, err := client.TransportChannels(Channels{
|
|
|
|
// Active: true,
|
|
|
|
// })
|
2022-11-02 17:00:13 +03:00
|
|
|
// if err != nil {
|
2023-12-28 14:26:49 +03:00
|
|
|
// log.Fatalf("request error: %s (%d)", err, status)
|
2022-11-02 17:00:13 +03:00
|
|
|
// }
|
2020-04-08 13:11:46 +03:00
|
|
|
//
|
2023-12-28 14:26:49 +03:00
|
|
|
// log.Printf("status: %d, channels: %#v", status, resp)
|
2020-04-08 13:11:46 +03:00
|
|
|
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 {
|
2022-10-26 18:07:04 +03:00
|
|
|
return resp, status, NewAPIClientError(data)
|
2020-04-08 13:11:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return resp, status, err
|
|
|
|
}
|
|
|
|
|
2023-12-28 14:26:49 +03:00
|
|
|
// ActivateTransportChannel activates the channel with provided settings.
|
2018-05-17 17:26:18 +03:00
|
|
|
//
|
|
|
|
// Example:
|
|
|
|
//
|
2023-12-28 14:26:49 +03:00
|
|
|
// client := New("https://message-gateway.url", "cb8ccf05e38a47543ad8477d4999be73bff503ea6")
|
|
|
|
// uint16Ptr := func(val uint16) *uint16 {
|
|
|
|
// return &val
|
|
|
|
// }
|
|
|
|
// mbToBytes := func(val uint64) *uint64 {
|
|
|
|
// val = val * 1024 * 1024
|
|
|
|
// return &val
|
|
|
|
// }
|
2018-05-17 17:26:18 +03:00
|
|
|
//
|
2023-12-28 14:26:49 +03:00
|
|
|
// resp, status, err := client.ActivateTransportChannel(Channel{
|
2018-05-17 17:26:18 +03:00
|
|
|
// 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,
|
2023-12-28 14:26:49 +03:00
|
|
|
// Read: ChannelFeatureReceive,
|
2022-11-02 17:00:13 +03:00
|
|
|
// },
|
2018-08-21 13:37:49 +03:00
|
|
|
// Text: ChannelSettingsText{
|
2023-12-28 14:26:49 +03:00
|
|
|
// Creating: ChannelFeatureBoth,
|
|
|
|
// Editing: ChannelFeatureBoth,
|
|
|
|
// Quoting: ChannelFeatureReceive,
|
|
|
|
// Deleting: ChannelFeatureSend,
|
2018-12-05 10:57:45 +03:00
|
|
|
// MaxCharsCount: 2000,
|
2022-11-02 17:00:13 +03:00
|
|
|
// },
|
2018-09-19 18:12:37 +03:00
|
|
|
// Product: Product{
|
|
|
|
// Creating: ChannelFeatureSend,
|
2023-12-28 14:26:49 +03:00
|
|
|
// Editing: ChannelFeatureNone,
|
2018-09-19 18:12:37 +03:00
|
|
|
// Deleting: ChannelFeatureSend,
|
2022-11-02 17:00:13 +03:00
|
|
|
// },
|
2018-09-19 18:12:37 +03:00
|
|
|
// Order: Order{
|
|
|
|
// Creating: ChannelFeatureBoth,
|
2023-12-28 14:26:49 +03:00
|
|
|
// Editing: ChannelFeatureNone,
|
2018-09-19 18:12:37 +03:00
|
|
|
// Deleting: ChannelFeatureSend,
|
2022-11-02 17:00:13 +03:00
|
|
|
// },
|
2023-12-28 14:26:49 +03:00
|
|
|
// File: ChannelSettingsFilesBase{
|
|
|
|
// Creating: ChannelFeatureBoth,
|
|
|
|
// Editing: ChannelFeatureBoth,
|
|
|
|
// Quoting: ChannelFeatureBoth,
|
|
|
|
// Deleting: ChannelFeatureBoth,
|
|
|
|
// Max: 10,
|
|
|
|
// NoteMaxCharsCount: uint16Ptr(256),
|
|
|
|
// MaxItemSize: mbToBytes(50),
|
|
|
|
// },
|
|
|
|
// Image: ChannelSettingsFilesBase{
|
|
|
|
// Creating: ChannelFeatureBoth,
|
|
|
|
// Editing: ChannelFeatureBoth,
|
|
|
|
// Quoting: ChannelFeatureBoth,
|
|
|
|
// Deleting: ChannelFeatureBoth,
|
|
|
|
// Max: 10,
|
|
|
|
// NoteMaxCharsCount: uint16Ptr(256),
|
|
|
|
// MaxItemSize: mbToBytes(10),
|
|
|
|
// },
|
|
|
|
// Suggestions: ChannelSettingsSuggestions{
|
|
|
|
// Text: ChannelFeatureBoth,
|
|
|
|
// Phone: ChannelFeatureBoth,
|
|
|
|
// Email: ChannelFeatureBoth,
|
|
|
|
// },
|
|
|
|
// Audio: ChannelSettingsAudio{
|
|
|
|
// Creating: ChannelFeatureBoth,
|
|
|
|
// Quoting: ChannelFeatureBoth,
|
|
|
|
// Deleting: ChannelFeatureBoth,
|
|
|
|
// MaxItemSize: mbToBytes(10),
|
|
|
|
// },
|
2022-11-02 17:00:13 +03:00
|
|
|
// },
|
2023-12-28 14:26:49 +03:00
|
|
|
// })
|
2022-11-02 17:00:13 +03:00
|
|
|
// if err != nil {
|
2023-12-28 14:26:49 +03:00
|
|
|
// log.Fatalf("request error: %s (%d)", err, status)
|
2022-11-02 17:00:13 +03:00
|
|
|
// }
|
2018-05-17 17:26:18 +03:00
|
|
|
//
|
2023-12-28 14:26:49 +03:00
|
|
|
// log.Printf("status: %d, channel external_id: %s", status, resp.ExternalID)
|
2018-05-17 17:26:18 +03:00
|
|
|
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 {
|
2022-10-26 18:07:04 +03:00
|
|
|
return resp, status, NewAPIClientError(data)
|
2018-05-17 17:26:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return resp, status, err
|
|
|
|
}
|
|
|
|
|
2023-12-28 14:26:49 +03:00
|
|
|
// UpdateTransportChannel updates an existing channel with provided settings.
|
2018-05-17 17:26:18 +03:00
|
|
|
//
|
|
|
|
// Example:
|
|
|
|
//
|
2023-12-28 14:26:49 +03:00
|
|
|
// client := New("https://message-gateway.url", "cb8ccf05e38a47543ad8477d4999be73bff503ea6")
|
|
|
|
// uint16Ptr := func(val uint16) *uint16 {
|
|
|
|
// return &val
|
|
|
|
// }
|
|
|
|
// mbToBytes := func(val uint64) *uint64 {
|
|
|
|
// val = val * 1024 * 1024
|
|
|
|
// return &val
|
|
|
|
// }
|
2018-05-17 17:26:18 +03:00
|
|
|
//
|
2023-12-28 14:26:49 +03:00
|
|
|
// resp, status, err := client.UpdateTransportChannel(Channel{
|
|
|
|
// ID: 305,
|
2018-05-17 17:26:18 +03:00
|
|
|
// 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,
|
2023-12-28 14:26:49 +03:00
|
|
|
// Read: ChannelFeatureReceive,
|
2022-11-02 17:00:13 +03:00
|
|
|
// },
|
2018-08-21 13:37:49 +03:00
|
|
|
// Text: ChannelSettingsText{
|
2023-12-28 14:26:49 +03:00
|
|
|
// Creating: ChannelFeatureBoth,
|
|
|
|
// Editing: ChannelFeatureBoth,
|
|
|
|
// Quoting: ChannelFeatureReceive,
|
|
|
|
// Deleting: ChannelFeatureSend,
|
|
|
|
// MaxCharsCount: 2000,
|
2022-11-02 17:00:13 +03:00
|
|
|
// },
|
2018-09-19 18:12:37 +03:00
|
|
|
// Product: Product{
|
|
|
|
// Creating: ChannelFeatureSend,
|
2023-12-28 14:26:49 +03:00
|
|
|
// Editing: ChannelFeatureNone,
|
2018-09-19 18:12:37 +03:00
|
|
|
// Deleting: ChannelFeatureSend,
|
2022-11-02 17:00:13 +03:00
|
|
|
// },
|
2018-09-19 18:12:37 +03:00
|
|
|
// Order: Order{
|
|
|
|
// Creating: ChannelFeatureBoth,
|
2023-12-28 14:26:49 +03:00
|
|
|
// Editing: ChannelFeatureNone,
|
2018-09-19 18:12:37 +03:00
|
|
|
// Deleting: ChannelFeatureSend,
|
2022-11-02 17:00:13 +03:00
|
|
|
// },
|
2023-12-28 14:26:49 +03:00
|
|
|
// File: ChannelSettingsFilesBase{
|
|
|
|
// Creating: ChannelFeatureBoth,
|
|
|
|
// Editing: ChannelFeatureBoth,
|
|
|
|
// Quoting: ChannelFeatureBoth,
|
|
|
|
// Deleting: ChannelFeatureBoth,
|
|
|
|
// Max: 10,
|
|
|
|
// NoteMaxCharsCount: uint16Ptr(256),
|
|
|
|
// MaxItemSize: mbToBytes(50),
|
|
|
|
// },
|
|
|
|
// Image: ChannelSettingsFilesBase{
|
|
|
|
// Creating: ChannelFeatureBoth,
|
|
|
|
// Editing: ChannelFeatureBoth,
|
|
|
|
// Quoting: ChannelFeatureBoth,
|
|
|
|
// Deleting: ChannelFeatureBoth,
|
|
|
|
// Max: 10,
|
|
|
|
// NoteMaxCharsCount: uint16Ptr(256),
|
|
|
|
// MaxItemSize: mbToBytes(10),
|
|
|
|
// },
|
|
|
|
// Suggestions: ChannelSettingsSuggestions{
|
|
|
|
// Text: ChannelFeatureBoth,
|
|
|
|
// Phone: ChannelFeatureBoth,
|
|
|
|
// Email: ChannelFeatureBoth,
|
|
|
|
// },
|
|
|
|
// Audio: ChannelSettingsAudio{
|
|
|
|
// Creating: ChannelFeatureBoth,
|
|
|
|
// Quoting: ChannelFeatureBoth,
|
|
|
|
// Deleting: ChannelFeatureBoth,
|
|
|
|
// MaxItemSize: mbToBytes(10),
|
|
|
|
// },
|
2022-11-02 17:00:13 +03:00
|
|
|
// },
|
2023-12-28 14:26:49 +03:00
|
|
|
// })
|
2022-11-02 17:00:13 +03:00
|
|
|
// if err != nil {
|
2023-12-28 14:26:49 +03:00
|
|
|
// log.Fatalf("request error: %s (%d)", err, status)
|
2022-11-02 17:00:13 +03:00
|
|
|
// }
|
2018-05-17 17:26:18 +03:00
|
|
|
//
|
2023-12-28 14:26:49 +03:00
|
|
|
// log.Printf("status: %d, channel_id: %d", status, resp.ChannelID)
|
2018-05-17 17:26:18 +03:00
|
|
|
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 {
|
2022-10-26 18:07:04 +03:00
|
|
|
return resp, status, NewAPIClientError(data)
|
2018-05-17 17:26:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return resp, status, err
|
|
|
|
}
|
|
|
|
|
2023-12-28 14:26:49 +03:00
|
|
|
// DeactivateTransportChannel deactivates the channel by its ID.
|
2018-05-17 17:26:18 +03:00
|
|
|
//
|
|
|
|
// Example:
|
|
|
|
//
|
2023-12-28 14:26:49 +03:00
|
|
|
// client := New("https://message-gateway.url", "cb8ccf05e38a47543ad8477d4999be73bff503ea6")
|
2018-05-17 17:26:18 +03:00
|
|
|
//
|
2023-12-28 14:26:49 +03:00
|
|
|
// resp, status, err := client.DeactivateTransportChannel(305)
|
2022-11-02 17:00:13 +03:00
|
|
|
// if err != nil {
|
2023-12-28 14:26:49 +03:00
|
|
|
// log.Fatalf("request error: %s (%d)", err, status)
|
2022-11-02 17:00:13 +03:00
|
|
|
// }
|
2018-05-17 17:26:18 +03:00
|
|
|
//
|
2023-12-28 14:26:49 +03:00
|
|
|
// log.Printf("status: %d, deactivated at: %s", status, resp.DeactivatedAt)
|
2018-05-17 17:26:18 +03:00
|
|
|
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 {
|
2022-10-26 18:07:04 +03:00
|
|
|
return resp, status, NewAPIClientError(data)
|
2018-05-17 17:26:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return resp, status, err
|
|
|
|
}
|
|
|
|
|
2023-12-28 14:26:49 +03:00
|
|
|
// Messages sends new message.
|
2018-05-17 17:26:18 +03:00
|
|
|
//
|
|
|
|
// Example:
|
|
|
|
//
|
2023-12-28 14:26:49 +03:00
|
|
|
// client := New("https://message-gateway.url", "cb8ccf05e38a47543ad8477d4999be73bff503ea6")
|
|
|
|
// getReplyDeadline := func(after time.Duration) *time.Time {
|
|
|
|
// deadline := time.Now().Add(after)
|
|
|
|
// return &deadline
|
2018-05-17 17:26:18 +03:00
|
|
|
// }
|
|
|
|
//
|
2023-12-28 14:26:49 +03:00
|
|
|
// resp, status, err := client.Messages(SendData{
|
|
|
|
// Message: Message{
|
|
|
|
// ExternalID: "uid_1",
|
|
|
|
// Type: MsgTypeText,
|
|
|
|
// Text: "Hello customer!",
|
|
|
|
// PageLink: "https://example.com",
|
|
|
|
// },
|
|
|
|
// Originator: OriginatorCustomer,
|
|
|
|
// Customer: Customer{
|
|
|
|
// ExternalID: "client_id_1",
|
|
|
|
// Nickname: "customer",
|
|
|
|
// Firstname: "Tester",
|
|
|
|
// Lastname: "Tester",
|
|
|
|
// Avatar: "https://example.com/image.png",
|
|
|
|
// ProfileURL: "https://example.com/user/client_id_1",
|
|
|
|
// Language: "en",
|
|
|
|
// Utm: &Utm{
|
|
|
|
// Source: "myspace.com",
|
|
|
|
// Medium: "social",
|
|
|
|
// Campaign: "something",
|
|
|
|
// Term: "fedora",
|
|
|
|
// Content: "autumn_collection",
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// Channel: 305,
|
|
|
|
// ExternalChatID: "chat_id_1",
|
|
|
|
// ReplyDeadline: getReplyDeadline(24 * time.Hour),
|
|
|
|
// })
|
2022-11-02 17:00:13 +03:00
|
|
|
// if err != nil {
|
2023-12-28 14:26:49 +03:00
|
|
|
// log.Fatalf("request error: %s (%d)", err, status)
|
2022-11-02 17:00:13 +03:00
|
|
|
// }
|
2018-05-17 17:26:18 +03:00
|
|
|
//
|
2023-12-28 14:26:49 +03:00
|
|
|
// log.Printf("status: %d, message ID: %d", status, resp.MessageID)
|
2018-05-17 17:26:18 +03:00
|
|
|
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 {
|
2022-10-26 18:07:04 +03:00
|
|
|
return resp, status, NewAPIClientError(data)
|
2018-05-17 17:26:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return resp, status, err
|
|
|
|
}
|
|
|
|
|
2023-12-28 14:26:49 +03:00
|
|
|
// MessagesHistory sends history message.
|
2022-11-03 17:26:48 +03:00
|
|
|
//
|
|
|
|
// Example:
|
|
|
|
//
|
2023-12-28 14:26:49 +03:00
|
|
|
// client := New("https://message-gateway.url", "cb8ccf05e38a47543ad8477d4999be73bff503ea6")
|
|
|
|
// getModifiedNow := func(after time.Duration) *time.Time {
|
|
|
|
// deadline := time.Now().Add(after)
|
|
|
|
// return &deadline
|
2022-11-03 17:26:48 +03:00
|
|
|
// }
|
|
|
|
//
|
2023-12-28 14:26:49 +03:00
|
|
|
// resp, status, err := client.MessagesHistory(SendHistoryMessageRequest{
|
|
|
|
// Message: SendMessageRequestMessage{
|
|
|
|
// ExternalID: "uid_1",
|
|
|
|
// Type: MsgTypeText,
|
|
|
|
// Text: "Hello customer!",
|
|
|
|
// CreatedAt: getModifiedNow(-time.Hour),
|
|
|
|
// },
|
|
|
|
// Originator: OriginatorCustomer,
|
|
|
|
// Customer: &Customer{
|
|
|
|
// ExternalID: "client_id_1",
|
|
|
|
// Nickname: "customer",
|
|
|
|
// Firstname: "Tester",
|
|
|
|
// Lastname: "Tester",
|
|
|
|
// Avatar: "https://example.com/image.png",
|
|
|
|
// ProfileURL: "https://example.com/user/client_id_1",
|
|
|
|
// Language: "en",
|
|
|
|
// Utm: &Utm{
|
|
|
|
// Source: "myspace.com",
|
|
|
|
// Medium: "social",
|
|
|
|
// Campaign: "something",
|
|
|
|
// Term: "fedora",
|
|
|
|
// Content: "autumn_collection",
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// ChannelID: 305,
|
|
|
|
// ExternalChatID: "chat_id_1",
|
|
|
|
// ReplyDeadline: getModifiedNow(24 * time.Hour),
|
|
|
|
// })
|
2022-11-03 17:26:48 +03:00
|
|
|
// if err != nil {
|
2023-12-28 14:26:49 +03:00
|
|
|
// log.Fatalf("request error: %s (%d)", err, status)
|
2022-11-03 17:26:48 +03:00
|
|
|
// }
|
|
|
|
//
|
2023-12-28 14:26:49 +03:00
|
|
|
// log.Printf("status: %d, message ID: %d", status, resp.MessageID)
|
2022-11-03 17:26:48 +03:00
|
|
|
func (c *MgClient) MessagesHistory(request SendHistoryMessageRequest) (MessagesResponse, int, error) {
|
|
|
|
var (
|
|
|
|
resp MessagesResponse
|
|
|
|
outgoing = &bytes.Buffer{}
|
|
|
|
)
|
|
|
|
_ = json.NewEncoder(outgoing).Encode(request)
|
|
|
|
|
|
|
|
data, status, err := c.PostRequest("/messages/history", 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, NewAPIClientError(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, status, err
|
|
|
|
}
|
|
|
|
|
2023-12-28 14:26:49 +03:00
|
|
|
// UpdateMessages edits existing message. Only text messages are supported.
|
2018-05-17 17:26:18 +03:00
|
|
|
//
|
|
|
|
// Example:
|
|
|
|
//
|
2023-12-28 14:26:49 +03:00
|
|
|
// client := New("https://message-gateway.url", "cb8ccf05e38a47543ad8477d4999be73bff503ea6")
|
2018-05-17 17:26:18 +03:00
|
|
|
//
|
2023-12-28 14:26:49 +03:00
|
|
|
// resp, status, err := client.UpdateMessages(EditMessageRequest{
|
|
|
|
// Message: EditMessageRequestMessage{
|
|
|
|
// ExternalID: "message_id_1",
|
|
|
|
// Text: "This is a new text!",
|
|
|
|
// },
|
|
|
|
// Channel: 305,
|
|
|
|
// })
|
2022-11-02 17:00:13 +03:00
|
|
|
// if err != nil {
|
2023-12-28 14:26:49 +03:00
|
|
|
// log.Fatalf("request error: %s (%d)", err, status)
|
2022-11-02 17:00:13 +03:00
|
|
|
// }
|
2018-05-17 17:26:18 +03:00
|
|
|
//
|
2023-12-28 14:26:49 +03:00
|
|
|
// log.Printf("status: %d, message ID: %d", status, resp.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 {
|
2022-10-26 18:07:04 +03:00
|
|
|
return resp, status, NewAPIClientError(data)
|
2018-10-03 09:48:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return resp, status, err
|
|
|
|
}
|
|
|
|
|
2023-12-28 14:26:49 +03:00
|
|
|
// MarkMessageRead send message read event to MG.
|
2018-10-03 09:48:07 +03:00
|
|
|
//
|
|
|
|
// Example:
|
|
|
|
//
|
2023-12-28 14:26:49 +03:00
|
|
|
// client := New("https://message-gateway.url", "cb8ccf05e38a47543ad8477d4999be73bff503ea6")
|
2018-10-03 09:48:07 +03:00
|
|
|
//
|
2023-12-28 14:26:49 +03:00
|
|
|
// _, status, err := client.MarkMessageRead(MarkMessageReadRequest{
|
|
|
|
// Message: MarkMessageReadRequestMessage{
|
|
|
|
// ExternalID: "message_id_1",
|
|
|
|
// },
|
|
|
|
// ChannelID: 305,
|
|
|
|
// })
|
2022-11-02 17:00:13 +03:00
|
|
|
// if err != nil {
|
2023-12-28 14:26:49 +03:00
|
|
|
// log.Fatalf("request error: %s (%d)", err, status)
|
2022-11-02 17:00:13 +03:00
|
|
|
// }
|
2018-10-03 09:48:07 +03:00
|
|
|
//
|
2023-12-28 14:26:49 +03:00
|
|
|
// log.Printf("status: %d", status)
|
2018-10-03 09:48:07 +03:00
|
|
|
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 {
|
2022-10-26 18:07:04 +03:00
|
|
|
return resp, status, NewAPIClientError(data)
|
2018-05-17 17:26:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return resp, status, err
|
|
|
|
}
|
|
|
|
|
2023-12-28 14:26:49 +03:00
|
|
|
// AckMessage sets success status for message or appends an error to message.
|
2020-12-23 11:54:46 +03:00
|
|
|
//
|
|
|
|
// Example:
|
|
|
|
//
|
2023-12-28 14:26:49 +03:00
|
|
|
// client := New("https://message-gateway.url", "cb8ccf05e38a47543ad8477d4999be73bff503ea6")
|
2020-12-23 11:54:46 +03:00
|
|
|
//
|
2023-12-28 14:26:49 +03:00
|
|
|
// status, err := client.AckMessage(AckMessageRequest{
|
|
|
|
// ExternalMessageID: "message_id_1",
|
|
|
|
// Channel: 305,
|
|
|
|
// })
|
2020-12-23 11:54:46 +03:00
|
|
|
// if err != nil {
|
2023-12-28 14:26:49 +03:00
|
|
|
// log.Fatalf("request error: %s (%d)", err, status)
|
2020-12-23 11:54:46 +03:00
|
|
|
// }
|
2023-12-28 14:26:49 +03:00
|
|
|
//
|
|
|
|
// log.Printf("status: %d", status)
|
2020-12-23 11:54:46 +03:00
|
|
|
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 {
|
2022-10-26 18:07:04 +03:00
|
|
|
return status, NewAPIClientError(data)
|
2020-12-23 11:54:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return status, err
|
|
|
|
}
|
|
|
|
|
2022-11-02 17:00:13 +03:00
|
|
|
// ReadUntil will mark all messages from specified timestamp as read.
|
2018-05-17 17:26:18 +03:00
|
|
|
//
|
|
|
|
// Example:
|
|
|
|
//
|
2023-12-28 14:26:49 +03:00
|
|
|
// client := New("https://message-gateway.url", "cb8ccf05e38a47543ad8477d4999be73bff503ea6")
|
2022-11-02 17:00:13 +03:00
|
|
|
//
|
2023-12-28 14:26:49 +03:00
|
|
|
// resp, status, err := client.ReadUntil(MarkMessagesReadUntilRequest{
|
|
|
|
// CustomerExternalID: "customer_id_1",
|
|
|
|
// ChannelID: 305,
|
|
|
|
// Until: time.Now().Add(-time.Hour),
|
|
|
|
// })
|
2022-11-02 17:00:13 +03:00
|
|
|
// if err != nil {
|
2023-12-28 14:26:49 +03:00
|
|
|
// log.Fatalf("request error: %s (%d)", err, status)
|
2018-05-21 17:56:42 +03:00
|
|
|
// }
|
2023-12-28 14:26:49 +03:00
|
|
|
//
|
|
|
|
// log.Printf("status: %d, marked messages: %+v", status, resp.IDs)
|
2022-11-02 17:00:13 +03:00
|
|
|
func (c *MgClient) ReadUntil(request MarkMessagesReadUntilRequest) (*MarkMessagesReadUntilResponse, int, error) {
|
|
|
|
outgoing, _ := json.Marshal(&request)
|
|
|
|
|
|
|
|
data, status, err := c.PostRequest("/messages/read_until", bytes.NewBuffer(outgoing))
|
|
|
|
if err != nil {
|
|
|
|
return nil, status, err
|
|
|
|
}
|
|
|
|
if status != http.StatusOK {
|
|
|
|
return nil, status, NewAPIClientError(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
var resp *MarkMessagesReadUntilResponse
|
|
|
|
if e := json.Unmarshal(data, &resp); e != nil {
|
|
|
|
return nil, status, e
|
|
|
|
}
|
|
|
|
return resp, status, nil
|
|
|
|
}
|
|
|
|
|
2023-12-28 14:26:49 +03:00
|
|
|
// DeleteMessage removes the message.
|
2022-11-02 17:00:13 +03:00
|
|
|
//
|
|
|
|
// Example:
|
2018-05-21 17:56:42 +03:00
|
|
|
//
|
2023-12-28 14:26:49 +03:00
|
|
|
// client := New("https://message-gateway.url", "cb8ccf05e38a47543ad8477d4999be73bff503ea6")
|
2022-11-02 17:00:13 +03:00
|
|
|
//
|
2023-12-28 14:26:49 +03:00
|
|
|
// resp, status, err := client.DeleteMessage(DeleteData{
|
|
|
|
// Message: Message{
|
|
|
|
// ExternalID: "message_id_1",
|
|
|
|
// },
|
|
|
|
// Channel: 305,
|
|
|
|
// })
|
|
|
|
// if err != nil {
|
|
|
|
// log.Fatalf("request error: %s (%d)", err, status)
|
|
|
|
// }
|
2022-11-02 17:00:13 +03:00
|
|
|
//
|
2023-12-28 14:26:49 +03:00
|
|
|
// log.Printf("status: %d, message ID: %d", status, resp.MessageID)
|
2021-07-27 15:31:49 +03:00
|
|
|
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 {
|
2022-10-26 18:07:04 +03:00
|
|
|
return nil, status, NewAPIClientError(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
|
|
|
}
|
|
|
|
|
2023-12-28 14:26:49 +03:00
|
|
|
// GetFile returns file information by its ID.
|
2018-11-08 17:17:24 +03:00
|
|
|
//
|
|
|
|
// Example:
|
|
|
|
//
|
2023-12-28 14:26:49 +03:00
|
|
|
// client := New("https://message-gateway.url", "cb8ccf05e38a47543ad8477d4999be73bff503ea6")
|
2018-11-08 17:17:24 +03:00
|
|
|
//
|
2023-12-28 14:26:49 +03:00
|
|
|
// resp, status, err := client.GetFile("file_id")
|
2022-11-02 17:00:13 +03:00
|
|
|
// if err != nil {
|
2023-12-28 14:26:49 +03:00
|
|
|
// log.Fatalf("request error: %s (%d)", err, status)
|
2022-11-02 17:00:13 +03:00
|
|
|
// }
|
2018-11-08 17:17:24 +03:00
|
|
|
//
|
2023-12-28 14:26:49 +03:00
|
|
|
// log.Printf("status: %d, file URL: %s", status, resp.Url)
|
2018-11-08 17:17:24 +03:00
|
|
|
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 {
|
2022-10-26 18:07:04 +03:00
|
|
|
return resp, status, NewAPIClientError(data)
|
2018-11-08 17:17:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return resp, status, err
|
|
|
|
}
|
|
|
|
|
2023-12-28 14:26:49 +03:00
|
|
|
// UploadFile uploads a file.
|
|
|
|
//
|
|
|
|
// Example:
|
|
|
|
//
|
|
|
|
// client := New("https://message-gateway.url", "cb8ccf05e38a47543ad8477d4999be73bff503ea6")
|
|
|
|
//
|
|
|
|
// file, err := os.Open("/tmp/file.png")
|
|
|
|
// if err != nil {
|
|
|
|
// log.Fatalf("cannot open file for reading: %s", err)
|
|
|
|
// }
|
|
|
|
// defer func() { _ = file.Close() }()
|
|
|
|
//
|
|
|
|
// data, err := io.ReadAll(file)
|
|
|
|
// if err != nil {
|
|
|
|
// log.Fatalf("cannot read file data: %s", err)
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// resp, status, err := client.UploadFile(bytes.NewReader(data))
|
|
|
|
// if err != nil {
|
|
|
|
// log.Fatalf("request error: %s (%d)", err, status)
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// log.Printf("status: %d, file ID: %s", status, resp.ID)
|
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 {
|
2022-10-26 18:07:04 +03:00
|
|
|
return resp, status, NewAPIClientError(data)
|
2018-11-08 17:17:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return resp, status, err
|
|
|
|
}
|
|
|
|
|
2023-12-28 14:26:49 +03:00
|
|
|
// UploadFileByURL uploads a file from provided URL.
|
|
|
|
//
|
|
|
|
// Example:
|
|
|
|
//
|
|
|
|
// client := New("https://message-gateway.url", "cb8ccf05e38a47543ad8477d4999be73bff503ea6")
|
|
|
|
//
|
|
|
|
// resp, status, err := client.UploadFileByURL(UploadFileByUrlRequest{
|
|
|
|
// Url: "https://example.com/file.png",
|
|
|
|
// })
|
|
|
|
// if err != nil {
|
|
|
|
// log.Fatalf("request error: %s (%d)", err, status)
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// log.Printf("status: %d, file ID: %s", status, resp.ID)
|
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 {
|
2022-10-26 18:07:04 +03:00
|
|
|
return resp, status, NewAPIClientError(data)
|
2018-11-08 17:17:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return resp, status, err
|
|
|
|
}
|
|
|
|
|
2023-12-28 14:26:49 +03:00
|
|
|
// MakeTimestamp returns current unix timestamp in milliseconds.
|
|
|
|
//
|
|
|
|
// Example:
|
|
|
|
//
|
|
|
|
// fmt.Printf("UNIX timestamp in milliseconds: %d", MakeTimestamp())
|
2018-08-16 16:53:29 +03:00
|
|
|
func MakeTimestamp() int64 {
|
|
|
|
return time.Now().UnixNano() / (int64(time.Millisecond) / int64(time.Nanosecond))
|
|
|
|
}
|