-
Notifications
You must be signed in to change notification settings - Fork 0
/
messages.go
127 lines (104 loc) · 2.67 KB
/
messages.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// messages
package prom
import (
"fmt"
"strconv"
"time"
)
const (
MessageStatusUnread = "unread"
MessageStatusRead = "read"
MessageStatusDeleted = "deleted"
)
type MessagesRequest struct {
Status string
DateFrom time.Time
DateTo time.Time
Limit int
LastId int
}
type Message struct {
Id int `json:"id"`
DateCreated string `json:"date_created"`
ClientFullName string `json:"client_full_name"`
Phone string `json:"phone"`
Message string `json:"message"`
Subject string `json:"subject"`
Status string `json:"status"`
ProductId int `json:"product_id"`
}
type MessagesResponse struct {
Messages []Message `json:"messages"`
Error string `json:"error"`
}
type MessageResponse struct {
Message Message `json:"message"`
Error string `json:"error"`
}
type SetMessageStatus struct {
Status string `json:"status"`
Ids []int `json:"ids"`
}
type SetMessageStatusResponse struct {
ProcessedIds []int `json:"processed_ids"`
Error string `json:"error"`
}
type MessageReply struct {
Id int `json:"id"`
Message string `json:"message"`
}
type MessageReplyResponse struct {
ProcessedIds []int `json:"processed_ids"`
Error string `json:"error"`
}
func (c *Client) GetMessages(request MessagesRequest) (messages []Message, err error) {
var (
result MessagesResponse
params = make(map[string]string)
)
if !request.DateFrom.IsZero() {
params["date_from"] = request.DateFrom.Format(RequestDateFormat)
}
if !request.DateTo.IsZero() {
params["date_to"] = request.DateTo.Format(RequestDateFormat)
}
if request.LastId > 0 {
params["last_id"] = strconv.Itoa(request.LastId)
}
if request.Limit > 0 {
params["limit"] = strconv.Itoa(request.LastId)
}
err = c.Get("/messages/list", params, &result)
messages = result.Messages
return
}
func (c *Client) GetMessage(id int) (message Message, err error) {
var result MessageResponse
err = c.Get("/messages/"+strconv.Itoa(id), nil, &result)
if err != nil {
err = fmt.Errorf("Error when request message: %s", err)
return
}
if len(result.Error) > 0 {
err = fmt.Errorf("Error when request message: %s", result.Error)
return
}
message = result.Message
return
}
func (c *Client) UpdateMessageStatus(status string, ids []int) (result SetMessageStatusResponse, err error) {
request := SetMessageStatus{
Status: status,
Ids: ids,
}
err = c.Post("/messages/set_status", request, &result)
return
}
func (c *Client) ReplyMessage(id int, message string) (result MessageReplyResponse, err error) {
request := MessageReply{
Id: id,
Message: message,
}
err = c.Post("/messages/reply", request, &result)
return
}