Messages methods & tests (#2)
This commit is contained in:
parent
11ed32136e
commit
2b5f24ff75
54
v1/client.go
54
v1/client.go
@ -110,8 +110,12 @@ func (c *MgClient) UpdateTransportChannel(request Channel) (UpdateResponse, int,
|
||||
// fmt.Printf("%s\n", data.DeactivatedAt)
|
||||
func (c *MgClient) DeactivateTransportChannel(id uint64) (DeleteResponse, int, error) {
|
||||
var resp DeleteResponse
|
||||
var buf []byte
|
||||
|
||||
data, status, err := c.DeleteRequest(fmt.Sprintf("/transport/channels/%s", strconv.FormatUint(id, 10)))
|
||||
data, status, err := c.DeleteRequest(
|
||||
fmt.Sprintf("/transport/channels/%s", strconv.FormatUint(id, 10)),
|
||||
buf,
|
||||
)
|
||||
if err != nil {
|
||||
return resp, status, err
|
||||
}
|
||||
@ -132,11 +136,10 @@ func (c *MgClient) DeactivateTransportChannel(id uint64) (DeleteResponse, int, e
|
||||
// Example:
|
||||
//
|
||||
// var client = v1.New("https://token.url", "cb8ccf05e38a47543ad8477d49bcba99be73bff503ea6")
|
||||
// snd := SendData{
|
||||
// msg := SendData{
|
||||
// SendMessage{
|
||||
// Message{
|
||||
// ExternalID: "23e23e23",
|
||||
// Channel: channelId,
|
||||
// ExternalID: "274628",
|
||||
// Type: "text",
|
||||
// Text: "hello!",
|
||||
// },
|
||||
@ -144,13 +147,13 @@ func (c *MgClient) DeactivateTransportChannel(id uint64) (DeleteResponse, int, e
|
||||
// },
|
||||
// User{
|
||||
// ExternalID: "8",
|
||||
// Nickname: "@octopulus",
|
||||
// Nickname: "@octopus",
|
||||
// Firstname: "Joe",
|
||||
// },
|
||||
// channelId,
|
||||
// 10,
|
||||
// }
|
||||
//
|
||||
// data, status, err := client.Messages(snd)
|
||||
// data, status, err := client.Messages(msg)
|
||||
//
|
||||
// if err != nil {
|
||||
// fmt.Printf("%v", err)
|
||||
@ -182,32 +185,26 @@ func (c *MgClient) Messages(request SendData) (MessagesResponse, int, error) {
|
||||
// Example:
|
||||
//
|
||||
// var client = v1.New("https://token.url", "cb8ccf05e38a47543ad8477d49bcba99be73bff503ea6")
|
||||
// snd := SendData{
|
||||
// SendMessage{
|
||||
// msg := UpdateData{
|
||||
// UpdateMessage{
|
||||
// Message{
|
||||
// ExternalID: "23e23e23",
|
||||
// Channel: channelId,
|
||||
// ExternalID: "274628",
|
||||
// Type: "text",
|
||||
// Text: "hello!",
|
||||
// Text: "hello hello!",
|
||||
// },
|
||||
// time.Now(),
|
||||
// },
|
||||
// User{
|
||||
// ExternalID: "8",
|
||||
// Nickname: "@octopulus",
|
||||
// Firstname: "Joe",
|
||||
// },
|
||||
// channelId,
|
||||
// 10,
|
||||
// }
|
||||
//
|
||||
// data, status, err := client.UpdateMessages(snd)
|
||||
// data, status, err := client.UpdateMessages(msg)
|
||||
//
|
||||
// if err != nil {
|
||||
// fmt.Printf("%v", err)
|
||||
// }
|
||||
//
|
||||
// fmt.Printf("%s\n", data.MessageID)
|
||||
func (c *MgClient) UpdateMessages(request UpdateMessage) (MessagesResponse, int, error) {
|
||||
func (c *MgClient) UpdateMessages(request UpdateData) (MessagesResponse, int, error) {
|
||||
var resp MessagesResponse
|
||||
outgoing, _ := json.Marshal(&request)
|
||||
|
||||
@ -233,17 +230,28 @@ func (c *MgClient) UpdateMessages(request UpdateMessage) (MessagesResponse, int,
|
||||
//
|
||||
// var client = v1.New("https://token.url", "cb8ccf05e38a47543ad8477d49bcba99be73bff503ea6")
|
||||
//
|
||||
// data, status, err := client.DeleteMessage("3053450384")
|
||||
// msg := DeleteData{
|
||||
// Message{
|
||||
// ExternalID: "274628",
|
||||
// },
|
||||
// 10,
|
||||
// }
|
||||
//
|
||||
// data, status, err := client.DeleteMessage(msg)
|
||||
//
|
||||
// if err != nil {
|
||||
// fmt.Printf("%v", err)
|
||||
// }
|
||||
//
|
||||
// fmt.Printf("%s\n", data.MessageID)
|
||||
func (c *MgClient) DeleteMessage(id string) (MessagesResponse, int, error) {
|
||||
func (c *MgClient) DeleteMessage(request DeleteData) (MessagesResponse, int, error) {
|
||||
var resp MessagesResponse
|
||||
outgoing, _ := json.Marshal(&request)
|
||||
|
||||
data, status, err := c.DeleteRequest(fmt.Sprintf("/transport/messages/%s", id))
|
||||
data, status, err := c.DeleteRequest(
|
||||
"/transport/messages/",
|
||||
[]byte(outgoing),
|
||||
)
|
||||
if err != nil {
|
||||
return resp, status, err
|
||||
}
|
||||
|
@ -5,12 +5,14 @@ import (
|
||||
"os"
|
||||
"strconv"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
mgURL = os.Getenv("MG_URL")
|
||||
mgToken = os.Getenv("MG_TOKEN")
|
||||
channelId, _ = strconv.ParseUint(os.Getenv("MG_CHANNEL"), 10, 64)
|
||||
ext = strconv.FormatInt(time.Now().UTC().UnixNano(), 10)
|
||||
)
|
||||
|
||||
func client() *MgClient {
|
||||
@ -24,8 +26,6 @@ func TestMgClient_ActivateTransportChannel(t *testing.T) {
|
||||
Type: "telegram",
|
||||
Events: []string{
|
||||
"message_sent",
|
||||
"message_updated",
|
||||
"message_deleted",
|
||||
"message_read",
|
||||
},
|
||||
}
|
||||
@ -36,7 +36,7 @@ func TestMgClient_ActivateTransportChannel(t *testing.T) {
|
||||
t.Errorf("%d %v", status, err)
|
||||
}
|
||||
|
||||
t.Logf("%v", data.ChannelID)
|
||||
t.Logf("Activate selected channel: %v", data.ChannelID)
|
||||
}
|
||||
|
||||
func TestMgClient_ActivateNewTransportChannel(t *testing.T) {
|
||||
@ -57,7 +57,19 @@ func TestMgClient_ActivateNewTransportChannel(t *testing.T) {
|
||||
t.Errorf("%d %v", status, err)
|
||||
}
|
||||
|
||||
t.Logf("%v", data.ChannelID)
|
||||
t.Logf("New channel ID %v", data.ChannelID)
|
||||
|
||||
deleteData, status, err := c.DeactivateTransportChannel(data.ChannelID)
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("%d %v", status, err)
|
||||
}
|
||||
|
||||
if deleteData.DectivatedAt.String() == "" {
|
||||
t.Errorf("%v", err)
|
||||
}
|
||||
|
||||
t.Logf("Deactivate new channel with ID %v", deleteData.ChannelID)
|
||||
}
|
||||
|
||||
func TestMgClient_UpdateTransportChannel(t *testing.T) {
|
||||
@ -66,6 +78,8 @@ func TestMgClient_UpdateTransportChannel(t *testing.T) {
|
||||
ID: channelId,
|
||||
Events: []string{
|
||||
"message_sent",
|
||||
"message_updated",
|
||||
"message_deleted",
|
||||
"message_read",
|
||||
},
|
||||
}
|
||||
@ -76,40 +90,25 @@ func TestMgClient_UpdateTransportChannel(t *testing.T) {
|
||||
t.Errorf("%v", err)
|
||||
}
|
||||
|
||||
t.Logf("%v", data.ChannelID)
|
||||
t.Logf("Update selected channel: %v", data.ChannelID)
|
||||
}
|
||||
|
||||
func TestMgClient_DeactivateTransportChannel(t *testing.T) {
|
||||
c := client()
|
||||
deleteData, status, err := c.DeactivateTransportChannel(channelId)
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("%d %v", status, err)
|
||||
}
|
||||
|
||||
if deleteData.DectivatedAt.String() == "" {
|
||||
t.Errorf("%v", err)
|
||||
}
|
||||
|
||||
t.Logf("%v", deleteData.ChannelID)
|
||||
}
|
||||
|
||||
/*func TestMgClient_Messages(t *testing.T) {
|
||||
func TestMgClient_Messages(t *testing.T) {
|
||||
c := client()
|
||||
t.Logf("%v", ext)
|
||||
|
||||
snd := SendData{
|
||||
SendMessage{
|
||||
Message{
|
||||
ExternalID: "23e23e23",
|
||||
Channel: channelId,
|
||||
ExternalID: ext,
|
||||
Type: "text",
|
||||
Text: "hello!",
|
||||
},
|
||||
time.Now(),
|
||||
},
|
||||
User{
|
||||
ExternalID: "8",
|
||||
Nickname: "@octopulus",
|
||||
ExternalID: "6",
|
||||
Nickname: "octopus",
|
||||
Firstname: "Joe",
|
||||
},
|
||||
channelId,
|
||||
@ -124,4 +123,73 @@ func TestMgClient_DeactivateTransportChannel(t *testing.T) {
|
||||
if data.Time.String() == "" {
|
||||
t.Errorf("%v", err)
|
||||
}
|
||||
}*/
|
||||
|
||||
t.Logf("Message %v is sent", data.MessageID)
|
||||
}
|
||||
|
||||
func TestMgClient_UpdateMessages(t *testing.T) {
|
||||
c := client()
|
||||
t.Logf("%v", ext)
|
||||
|
||||
snd := UpdateData{
|
||||
UpdateMessage{
|
||||
Message{
|
||||
ExternalID: ext,
|
||||
Type: "text",
|
||||
Text: "hello hello!",
|
||||
},
|
||||
time.Now(),
|
||||
},
|
||||
channelId,
|
||||
}
|
||||
|
||||
data, status, err := c.UpdateMessages(snd)
|
||||
|
||||
if status != http.StatusOK {
|
||||
t.Errorf("%v", err)
|
||||
}
|
||||
|
||||
if data.Time.String() == "" {
|
||||
t.Errorf("%v", err)
|
||||
}
|
||||
|
||||
t.Logf("Message %v updated", data.MessageID)
|
||||
}
|
||||
|
||||
func TestMgClient_DeleteMessage(t *testing.T) {
|
||||
c := client()
|
||||
t.Logf("%v", ext)
|
||||
|
||||
snd := DeleteData{
|
||||
Message{
|
||||
ExternalID: ext,
|
||||
},
|
||||
channelId,
|
||||
}
|
||||
|
||||
data, status, err := c.DeleteMessage(snd)
|
||||
if status != http.StatusOK {
|
||||
t.Errorf("%v", err)
|
||||
}
|
||||
|
||||
if data.Time.String() == "" {
|
||||
t.Errorf("%v", err)
|
||||
}
|
||||
|
||||
t.Logf("Message %v updated", data.MessageID)
|
||||
}
|
||||
|
||||
func TestMgClient_DeactivateTransportChannel(t *testing.T) {
|
||||
c := client()
|
||||
deleteData, status, err := c.DeactivateTransportChannel(channelId)
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("%d %v", status, err)
|
||||
}
|
||||
|
||||
if deleteData.DectivatedAt.String() == "" {
|
||||
t.Errorf("%v", err)
|
||||
}
|
||||
|
||||
t.Logf("Deactivate selected channel: %v", deleteData.ChannelID)
|
||||
}
|
||||
|
@ -59,13 +59,11 @@ func (c *MgClient) PutRequest(url string, parameters []byte) ([]byte, int, error
|
||||
}
|
||||
|
||||
// DeleteRequest implements DELETE Request
|
||||
func (c *MgClient) DeleteRequest(url string) ([]byte, int, error) {
|
||||
var buf []byte
|
||||
|
||||
func (c *MgClient) DeleteRequest(url string, parameters []byte) ([]byte, int, error) {
|
||||
return makeRequest(
|
||||
"DELETE",
|
||||
fmt.Sprintf("%s%s%s", c.URL, prefix, url),
|
||||
bytes.NewBuffer(buf),
|
||||
bytes.NewBuffer(parameters),
|
||||
c,
|
||||
)
|
||||
}
|
||||
|
22
v1/types.go
22
v1/types.go
@ -39,7 +39,7 @@ type DeleteResponse struct {
|
||||
|
||||
// User struct
|
||||
type User struct {
|
||||
ExternalID string `url:"external_id"`
|
||||
ExternalID string `url:"external_id" json:"external_id"`
|
||||
Nickname string `url:"nickname"`
|
||||
Firstname string `url:"first_name,omitempty"`
|
||||
Lastname string `url:"last_name,omitempty"`
|
||||
@ -52,9 +52,8 @@ type User struct {
|
||||
|
||||
// Message struct
|
||||
type Message struct {
|
||||
ExternalID string `url:"external_id"`
|
||||
Channel uint64 `url:"channel"`
|
||||
Type string `url:"type"`
|
||||
ExternalID string `url:"external_id" json:"external_id"`
|
||||
Type string `url:"type,omitempty"`
|
||||
Text string `url:"text,omitempty"`
|
||||
}
|
||||
|
||||
@ -70,14 +69,27 @@ type UpdateMessage struct {
|
||||
EditedAt time.Time `url:"edited_at,omitempty"`
|
||||
}
|
||||
|
||||
// SendData struct
|
||||
type SendData struct {
|
||||
Message SendMessage `url:"message"`
|
||||
User User `url:"user"`
|
||||
Channel uint64 `url:"channel"`
|
||||
}
|
||||
|
||||
// UpdateData struct
|
||||
type UpdateData struct {
|
||||
Message UpdateMessage `url:"message"`
|
||||
Channel uint64 `url:"channel"`
|
||||
}
|
||||
|
||||
// DeleteData struct
|
||||
type DeleteData struct {
|
||||
Message Message `url:"message"`
|
||||
Channel uint64 `url:"channel"`
|
||||
}
|
||||
|
||||
// MessagesResponse message event response
|
||||
type MessagesResponse struct {
|
||||
MessageID string `json:"message_id"`
|
||||
MessageID int `json:"message_id"`
|
||||
Time time.Time `json:"time"`
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user