2018-05-17 17:26:18 +03:00
|
|
|
package v1
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// New initialize client
|
|
|
|
func New(url string, token string) *MgClient {
|
|
|
|
return &MgClient{
|
2018-06-20 15:27:46 +03:00
|
|
|
URL: url,
|
|
|
|
Token: token,
|
|
|
|
httpClient: &http.Client{Timeout: 20 * time.Second},
|
2018-05-17 17:26:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ActivateTransportChannel implement channel activation
|
|
|
|
//
|
|
|
|
// Example:
|
|
|
|
//
|
|
|
|
// var client = v1.New("https://token.url", "cb8ccf05e38a47543ad8477d49bcba99be73bff503ea6")
|
|
|
|
//
|
|
|
|
// request := ActivateRequest{
|
|
|
|
// Type: "telegram",
|
2018-08-16 16:53:29 +03:00
|
|
|
// Events: []string{
|
|
|
|
// "message_sent",
|
|
|
|
// "message_read",
|
|
|
|
// },
|
|
|
|
// Settings: ChannelSettings{
|
|
|
|
// ReceiveMessageMode: "always",
|
|
|
|
// SpamAllowed: false,
|
|
|
|
// Features: ChannelFeatures{
|
|
|
|
// StatusDelivered: "none",
|
|
|
|
// MessageDeleting: "send",
|
|
|
|
// MessageEditing: "both",
|
|
|
|
// MessageQuoting: "both",
|
|
|
|
// ImageMessage: "receive",
|
|
|
|
// },
|
|
|
|
// },
|
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-07-21 18:56:25 +03:00
|
|
|
data, status, err := c.PostRequest("/channels", []byte(outgoing))
|
2018-05-17 17:26:18 +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
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateTransportChannel implement channel activation
|
|
|
|
//
|
|
|
|
// Example:
|
|
|
|
//
|
|
|
|
// var client = v1.New("https://token.url", "cb8ccf05e38a47543ad8477d49bcba99be73bff503ea6")
|
|
|
|
//
|
|
|
|
// request := ActivateRequest{
|
|
|
|
// ID: 3053450384,
|
|
|
|
// Type: "telegram",
|
2018-08-16 16:53:29 +03:00
|
|
|
// Events: []string{
|
|
|
|
// "message_sent",
|
|
|
|
// "message_read",
|
|
|
|
// },
|
|
|
|
// Settings: ChannelSettings{
|
|
|
|
// ReceiveMessageMode: "always",
|
|
|
|
// SpamAllowed: false,
|
|
|
|
// Features: ChannelFeatures{
|
|
|
|
// StatusDelivered: "none",
|
|
|
|
// MessageDeleting: "send",
|
|
|
|
// MessageEditing: "both",
|
|
|
|
// MessageQuoting: "both",
|
|
|
|
// ImageMessage: "receive",
|
|
|
|
// },
|
|
|
|
// },
|
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)
|
|
|
|
|
2018-07-21 18:56:25 +03:00
|
|
|
data, status, err := c.PutRequest(fmt.Sprintf("/channels/%d", request.ID), []byte(outgoing))
|
2018-05-17 17:26:18 +03:00
|
|
|
if err != nil {
|
|
|
|
return resp, status, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := json.Unmarshal(data, &resp); err != nil {
|
|
|
|
return resp, status, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if status != http.StatusOK {
|
|
|
|
return resp, status, c.Error(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, status, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeactivateTransportChannel implement channel deactivation
|
|
|
|
//
|
|
|
|
// Example:
|
|
|
|
//
|
|
|
|
// var client = v1.New("https://token.url", "cb8ccf05e38a47543ad8477d49bcba99be73bff503ea6")
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := json.Unmarshal(data, &resp); err != nil {
|
|
|
|
return resp, status, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if status != http.StatusOK {
|
|
|
|
return resp, status, c.Error(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, status, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Messages implement send message
|
|
|
|
//
|
|
|
|
// Example:
|
|
|
|
//
|
|
|
|
// var client = v1.New("https://token.url", "cb8ccf05e38a47543ad8477d49bcba99be73bff503ea6")
|
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-07-21 18:56:25 +03:00
|
|
|
data, status, err := c.PostRequest("/messages", []byte(outgoing))
|
2018-05-17 17:26:18 +03:00
|
|
|
if err != nil {
|
|
|
|
return resp, status, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := json.Unmarshal(data, &resp); err != nil {
|
|
|
|
return resp, status, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if status != http.StatusOK {
|
|
|
|
return resp, status, c.Error(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, status, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateMessages implement edit message
|
|
|
|
//
|
|
|
|
// Example:
|
|
|
|
//
|
|
|
|
// var client = v1.New("https://token.url", "cb8ccf05e38a47543ad8477d49bcba99be73bff503ea6")
|
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-05-21 17:56:42 +03:00
|
|
|
func (c *MgClient) UpdateMessages(request UpdateData) (MessagesResponse, int, error) {
|
2018-05-17 17:26:18 +03:00
|
|
|
var resp MessagesResponse
|
|
|
|
outgoing, _ := json.Marshal(&request)
|
|
|
|
|
2018-07-21 18:56:25 +03:00
|
|
|
data, status, err := c.PutRequest("/messages", []byte(outgoing))
|
2018-05-17 17:26:18 +03:00
|
|
|
if err != nil {
|
|
|
|
return resp, status, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := json.Unmarshal(data, &resp); err != nil {
|
|
|
|
return resp, status, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if status != http.StatusOK {
|
|
|
|
return resp, status, c.Error(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, status, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteMessage implement delete message
|
|
|
|
//
|
|
|
|
// Example:
|
|
|
|
//
|
|
|
|
// var client = v1.New("https://token.url", "cb8ccf05e38a47543ad8477d49bcba99be73bff503ea6")
|
|
|
|
//
|
2018-05-21 17:56:42 +03:00
|
|
|
// msg := DeleteData{
|
|
|
|
// Message{
|
|
|
|
// ExternalID: "274628",
|
|
|
|
// },
|
|
|
|
// 10,
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// data, status, err := client.DeleteMessage(msg)
|
2018-05-17 17:26:18 +03:00
|
|
|
//
|
|
|
|
// if err != nil {
|
|
|
|
// fmt.Printf("%v", err)
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// fmt.Printf("%s\n", data.MessageID)
|
2018-05-21 17:56:42 +03:00
|
|
|
func (c *MgClient) DeleteMessage(request DeleteData) (MessagesResponse, int, error) {
|
2018-05-17 17:26:18 +03:00
|
|
|
var resp MessagesResponse
|
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-07-21 18:56:25 +03:00
|
|
|
"/messages/",
|
2018-05-21 17:56:42 +03:00
|
|
|
[]byte(outgoing),
|
|
|
|
)
|
2018-05-17 17:26:18 +03:00
|
|
|
if err != nil {
|
|
|
|
return resp, status, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := json.Unmarshal(data, &resp); err != nil {
|
|
|
|
return resp, status, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if status != http.StatusOK {
|
|
|
|
return resp, status, c.Error(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, status, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *MgClient) Error(info []byte) error {
|
|
|
|
var data map[string]interface{}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
func MakeTimestamp() int64 {
|
|
|
|
return time.Now().UnixNano() / (int64(time.Millisecond) / int64(time.Nanosecond))
|
|
|
|
}
|