-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzoho.go
308 lines (274 loc) · 7.2 KB
/
zoho.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package zoho
import (
"bytes"
_ "embed"
"encoding/json"
"errors"
"fmt"
"io"
"log"
"net/http"
"os"
"strings"
"time"
)
// https://api-console.zoho.eu/
// ZohoCRM.modules.ALL,ZohoSearch.securesearch.READ,ZohoCRM.coql.READ,ZohoCRM.send_mail.all.CREATE,ZohoCRM.settings.emails.READ
// curl -X POST -F grant_type=authorization_code -F client_id=1000.XXXXXXXXXX -F client_secret=XXXXX -F code=1000.XXXXXXXXXXXXXXXXXX https://accounts.zoho.eu/oauth/v2/token
// curl -X POST "https://accounts.zoho.eu/oauth/v2/token?refresh_token=1000.XXXXXXXXXXXXXX&client_id=1000.XXXXXXXXXXXXX&client_secret=XXXXXXXXXX&grant_type=refresh_token"
type Auth struct {
AccessToken string `json:"access_token"`
APIDomain string `json:"api_domain"`
TokenType string `json:"token_type"`
ExpiresIn int `json:"expires_in"`
Code string `json:"code"`
Details struct {
} `json:"details"`
Message string `json:"message"`
Status string `json:"status"`
}
type notes struct {
Data []note `json:"data"`
}
type note struct {
Title string `json:"Note_Title,omitempty"`
Content string `json:"Note_Content,omitempty"`
}
type updateContact struct {
Code string `json:"code"`
Details struct {
ModifiedTime time.Time `json:"Modified_Time"`
ModifiedBy struct {
Name string `json:"name"`
ID string `json:"id"`
} `json:"Modified_By"`
CreatedTime time.Time `json:"Created_Time"`
ID string `json:"id"`
CreatedBy struct {
Name string `json:"name"`
ID string `json:"id"`
} `json:"Created_By"`
ApprovalState string `json:"$approval_state"`
} `json:"details"`
Message string `json:"message"`
Status string `json:"status"`
}
type updateContactResult struct {
Data []updateContact `json:"data"`
}
type getContactResponse struct {
Data []GetContactItem `json:"data"`
Info struct {
PerPage int `json:"per_page"`
NextPageToken interface{} `json:"next_page_token"`
Count int `json:"count"`
Page int `json:"page"`
PreviousPageToken interface{} `json:"previous_page_token"`
PageTokenExpiry interface{} `json:"page_token_expiry"`
MoreRecords bool `json:"more_records"`
} `json:"info"`
}
type findContactResponse struct {
Data []struct {
ID string `json:"id"`
} `json:"data"`
}
type createNote struct {
Data []struct {
Message string `json:"message"`
Details struct {
CreatedBy struct {
ID string `json:"id"`
Name string `json:"name"`
} `json:"created_by"`
ID string `json:"id"`
ModifiedBy struct {
ID string `json:"id"`
Name string `json:"name"`
} `json:"modified_by"`
ModifiedTime time.Time `json:"modified_time"`
CreatedTime time.Time `json:"created_time"`
} `json:"details"`
Status string `json:"status"`
Code string `json:"code"`
} `json:"data"`
}
var auth Auth
func init() {
var err error
for i := 0; i < 10; i++ {
err := authenticate()
if err == nil {
break
}
log.Println(err)
time.Sleep(time.Duration(i+1) * 100 * time.Millisecond)
}
if err != nil {
log.Println(err)
}
go func() {
for {
time.Sleep(time.Duration(auth.ExpiresIn-60) * time.Second)
for i := 0; i < 10; i++ {
err := authenticate()
if err == nil {
break
}
log.Println(err)
time.Sleep(time.Duration(i+1) * 100 * time.Millisecond)
}
if err != nil {
log.Println(err)
}
}
}()
}
type Date struct {
time.Time
}
type Time struct {
time.Time
}
func (t Date) MarshalJSON() ([]byte, error) {
stamp := fmt.Sprintf("\"%s\"", t.Format("2006-01-02"))
return []byte(stamp), nil
}
func (t *Date) UnmarshalJSON(b []byte) (err error) {
date, err := time.Parse(`"2006-01-02"`, string(b))
if err != nil {
return err
}
t.Time = date
return
}
func (t Time) MarshalJSON() ([]byte, error) {
stamp := fmt.Sprintf("\"%s\"", t.Format("2006-01-02T15:04:05Z07:00"))
return []byte(stamp), nil
}
func (t *Time) UnmarshalJSON(b []byte) (err error) {
date, err := time.Parse(`"2006-01-02T15:04:05Z07:00"`, string(b))
if err != nil {
return err
}
t.Time = date
return
}
func authenticate() error {
r, err := http.Post("https://accounts.zoho.eu/oauth/v2/token?refresh_token="+os.Getenv("ZOHO_REFRESH_TOKEN")+"&client_id="+os.Getenv("ZOHO_CLIENT_ID")+"&client_secret="+os.Getenv("ZOHO_CLIENT_SECRET")+"&grant_type=refresh_token", "", nil)
if err != nil {
return err
}
defer r.Body.Close()
if r.StatusCode != http.StatusOK {
b, err := io.ReadAll(r.Body)
if err != nil {
return err
}
log.Println(string(b))
return errors.New(r.Status)
}
err = json.NewDecoder(r.Body).Decode(&auth)
if err != nil {
return err
}
if auth.Status == "error" {
return errors.New(auth.Message)
}
return nil
}
// Scope: ZohoCRM.modules.all and ZohoSearch.securesearch.READ
/*
func FindContact(email string) (string, error) {
req, err := http.NewRequest("GET", "https://www.zohoapis.eu/crm/v3/Contacts/search?email="+url.QueryEscape(email), nil)
if err != nil {
return "", err
}
req.Header.Set("Authorization", "Zoho-oauthtoken "+auth.AccessToken)
r, err := http.DefaultClient.Do(req)
if err != nil {
return "", err
}
defer r.Body.Close()
if r.StatusCode == http.StatusNoContent {
return "", nil
}
if r.StatusCode != http.StatusOK {
b, err := ioutil.ReadAll(r.Body)
if err != nil {
return "", err
}
log.Println(string(b))
return "", errors.New(r.Status)
}
var res getContactResponse
err = json.NewDecoder(r.Body).Decode(&res)
if err != nil {
return "", err
}
return res.Data[0].ID, nil
}*/
type selectQuery struct {
Query string `json:"select_query"`
}
func AddContactNote(id string, title string, content string) (string, error) {
b, err := json.Marshal(notes{Data: []note{{Title: title, Content: content}}})
if err != nil {
return "", err
}
req, err := http.NewRequest("POST", "https://www.zohoapis.eu/crm/v3/Contacts/"+id+"/Notes", bytes.NewReader(b))
if err != nil {
return "", err
}
req.Header.Set("Authorization", "Zoho-oauthtoken "+auth.AccessToken)
r, err := http.DefaultClient.Do(req)
if err != nil {
return "", err
}
defer r.Body.Close()
if r.StatusCode != http.StatusCreated {
b, err = io.ReadAll(r.Body)
if err != nil {
return "", err
}
log.Println(string(b))
return "", errors.New(r.Status)
}
var res createNote
err = json.NewDecoder(r.Body).Decode(&res)
if err != nil {
return "", err
}
return res.Data[0].Details.ID, nil
}
func clearContactField(id string, field string) error {
json := fmt.Sprintf(`{"data":[{"id":"%s","%s":null}]}`, id, field)
req, err := http.NewRequest("PUT", "https://www.zohoapis.eu/crm/v3/Contacts", strings.NewReader(json))
if err != nil {
return err
}
req.Header.Set("Authorization", "Zoho-oauthtoken "+auth.AccessToken)
r, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer r.Body.Close()
if r.StatusCode != http.StatusOK {
b, err := io.ReadAll(r.Body)
if err != nil {
return err
}
log.Println(string(b))
return errors.New(r.Status)
}
return nil
}
func CancelOpenHouse(contactId string) error {
return clearContactField(contactId, "Open_House")
}
func CancelImmersion(contactId string) error {
return clearContactField(contactId, "Immersion")
}
func CancelCodingCamp(contactId string) error {
return clearContactField(contactId, "Coding_Camp")
}