-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathvk_ok_service.go
238 lines (207 loc) · 6.73 KB
/
vk_ok_service.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package sendpulse_sdk_go
import (
"bytes"
"context"
"fmt"
"io"
"mime/multipart"
"net/http"
"os"
"path/filepath"
)
type VkOkService struct {
client *Client
}
func newVkOkService(cl *Client) *VkOkService {
return &VkOkService{client: cl}
}
type CreateVkOkSenderParams struct {
Name string
VkUrl string
OkUrl string
CoverLetter *os.File
}
func (service *VkOkService) CreateSender(ctx context.Context, params CreateVkOkSenderParams) (int, error) {
path := "/vk-ok/senders"
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
if params.CoverLetter != nil {
part, err := writer.CreateFormFile("cover_letter", filepath.Base(path))
if err != nil {
return 0, err
}
_, err = io.Copy(part, params.CoverLetter)
if err != nil {
return 0, err
}
}
_ = writer.WriteField("name", params.Name)
if params.VkUrl != "" {
_ = writer.WriteField("vk_url", params.VkUrl)
}
if params.OkUrl != "" {
_ = writer.WriteField("ok_url", params.OkUrl)
}
if err := writer.Close(); err != nil {
return 0, err
}
var respData struct {
ID int `json:"id"`
}
_, err := service.client.newFormDataRequest(ctx, path, body, writer.FormDataContentType(), &respData, true)
return respData.ID, err
}
type CreateVkOkTemplateParams struct {
Name string `json:"name"`
VkMessage string `json:"vk_message,omitempty"`
OkMessage string `json:"ok_message,omitempty"`
SenderID int `json:"sender_id"`
}
func (service *VkOkService) CreateTemplate(ctx context.Context, params CreateVkOkTemplateParams) (int, error) {
path := "/vk-ok/templates"
var respData struct {
Total int `json:"total"`
Data struct {
ID int `json:"id"`
} `json:"data"`
}
_, err := service.client.newRequest(ctx, http.MethodPost, path, params, &respData, true)
return respData.Data.ID, err
}
type VkOkTemplate struct {
ID int `json:"id"`
UserID int `json:"user_id"`
SenderID int `json:"sender_id"`
Name string `json:"name"`
VkMessage string `json:"vk_message"`
OkMessage string `json:"ok_message"`
Sender struct {
ID int `json:"id"`
UserID int `json:"user_id"`
Name string `json:"name"`
VkUrl string `json:"vk_url"`
OkUrl string `json:"ok_url"`
CreatedAt string `json:"created_at"`
UpdateAt string `json:"update_at"`
} `json:"sender"`
Status int `json:"status"`
StatusDetail struct {
ID int `json:"id"`
Name string `json:"name"`
} `json:"status_detail"`
}
func (service *VkOkService) GetTemplates(ctx context.Context) ([]*VkOkTemplate, error) {
path := "/vk-ok/templates"
var respData struct {
Total int `json:"total"`
Data []*VkOkTemplate `json:"data"`
}
_, err := service.client.newRequest(ctx, http.MethodGet, path, nil, &respData, true)
return respData.Data, err
}
func (service *VkOkService) GetTemplate(ctx context.Context, templateID int) (*VkOkTemplate, error) {
path := fmt.Sprintf("/vk-ok/templates/%d", templateID)
var respData struct {
Total int `json:"total"`
Data *VkOkTemplate `json:"data"`
}
_, err := service.client.newRequest(ctx, http.MethodGet, path, nil, &respData, true)
return respData.Data, err
}
type SendVkOkTemplateParams struct {
AddressBooks []int `json:"address_book"`
Recipients []struct {
Phone string `json:"phone"`
Variables map[string]any `json:"variables"`
} `json:"recipients"`
LifeTime int `json:"life_time"`
LifeType string `json:"life_type"`
Name string `json:"name"`
Routes map[string]bool `json:"routes"`
SendDate DateTime `json:"send_date"`
TemplateID int `json:"template_id"`
}
func (service *VkOkService) Send(ctx context.Context, params SendVkOkTemplateParams) (int, error) {
path := "/vk-ok/campaigns"
var respData struct {
Total int `json:"total"`
Data struct {
ID int `json:"id"`
}
}
_, err := service.client.newRequest(ctx, http.MethodPost, path, params, &respData, true)
return respData.Data.ID, err
}
type VkOkCurrency struct {
ID int `json:"id"`
Name string `json:"currency_name"`
Abbr string `json:"currency_abbr"`
Sign string `json:"currency_sign"`
}
type VkOkCampaignStatistics struct {
ID int `json:"id"`
UserID int `json:"user_id"`
Name string `json:"name"`
TotalPrice int `json:"total_price"`
PriceRate int `json:"price_rate"`
Currency VkOkCurrency `json:"currency"`
LifeTime int `json:"life_time"`
LifeType string `json:"life_type"`
SendDate string `json:"send_date"`
CreatedAt string `json:"created_at"`
Template VkOkTemplate `json:"template"`
Status int `json:"status"`
StatusDetail struct {
ID int `json:"id"`
Name string `json:"name"`
} `json:"status_detail"`
GroupStat []struct {
ID int `json:"id"`
UserID int `json:"user_id"`
Sent int `json:"sent"`
Delivered int `json:"delivered"`
NotDelivered int `json:"not_delivered"`
Opened int `json:"opened"`
} `json:"group_stat"`
}
func (service *VkOkService) GetCampaignsStatistics(ctx context.Context) ([]*VkOkCampaignStatistics, error) {
path := "/vk-ok/campaigns"
var respData struct {
Total int `json:"total"`
Data []*VkOkCampaignStatistics `json:"data"`
}
_, err := service.client.newRequest(ctx, http.MethodGet, path, nil, &respData, true)
return respData.Data, err
}
func (service *VkOkService) GetCampaignStatistics(ctx context.Context, campaignID int) (*VkOkCampaignStatistics, error) {
path := fmt.Sprintf("/vk-ok/campaigns/%d", campaignID)
var respData *VkOkCampaignStatistics
_, err := service.client.newRequest(ctx, http.MethodGet, path, nil, &respData, true)
return respData, err
}
type VkOkCampaignPhone struct {
ID int `json:"id"`
UserID int `json:"user_id"`
CampaignID int `json:"campaign_id"`
TemplateID int `json:"template_id"`
Phone int `json:"phone"`
PhoneCost int `json:"phone_cost"`
CurrencyID int `json:"currency_id"`
PriceRate int `json:"price_rate"`
Currency VkOkCurrency `json:"currency"`
CreatedAt string `json:"created_at"`
Status int `json:"status"`
StatusDetail struct {
ID int `json:"id"`
Name string `json:"name"`
} `json:"status_detail"`
}
func (service *VkOkService) GetCampaignPhones(ctx context.Context, campaignID int) ([]*VkOkCampaignPhone, error) {
path := fmt.Sprintf("/vk-ok/campaigns/%d/phones", campaignID)
var respData struct {
Total int `json:"total"`
Data []*VkOkCampaignPhone `json:"data"`
}
_, err := service.client.newRequest(ctx, http.MethodGet, path, nil, &respData, true)
return respData.Data, err
}