-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcall.go
183 lines (165 loc) · 4.87 KB
/
call.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
package zoho
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
)
type getCalls struct {
Data []ContactCall `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 ContactCall struct {
CallType string `json:"Call_Type"`
CallResult string `json:"Call_Result"`
Owner struct {
Name string `json:"name"`
ID string `json:"id"`
Email string `json:"email"`
} `json:"Owner"`
CallStartTime Time `json:"Call_Start_Time"`
CallDurationInSeconds int `json:"Call_Duration_in_seconds"`
ID string `json:"id"`
Subject string `json:"Subject"`
}
func GetOpenCalls(contactID string) ([]ContactCall, error) {
req, err := http.NewRequest("GET", "https://www.zohoapis.eu/crm/v4/contacts/"+contactID+"/calls?sort_order=desc&fields=Subject,Call_Type,Call_Start_Time,Owner,Call_Result,Call_Status,Call_Duration_in_seconds", nil)
if err != nil {
return nil, err
}
req.Header.Set("Authorization", "Zoho-oauthtoken "+auth.AccessToken)
r, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer r.Body.Close()
if r.StatusCode == http.StatusNoContent {
return []ContactCall{}, nil
}
if r.StatusCode != http.StatusOK {
b, err := ioutil.ReadAll(r.Body)
if err != nil {
return nil, err
}
log.Println(string(b))
return nil, errors.New(r.Status)
}
var res getCalls
err = json.NewDecoder(r.Body).Decode(&res)
if err != nil {
return nil, err
}
return res.Data, nil
}
type calls struct {
Data []NewCall `json:"data"`
}
type Owner struct {
Name string `json:"name"`
ID string `json:"id"`
Email string `json:"email"`
}
type NameID struct {
Name string `json:"name"`
ID string `json:"id"`
}
type NewCall struct {
Subject string `json:"Subject"`
CallType string `json:"Call_Type"`
CallStartTime Time `json:"Call_Start_Time"`
CallDuration string `json:"Call_Duration"`
Owner Owner `json:"Owner"`
SeModule string `json:"$se_module"`
WhoID NameID `json:"Who_Id"` // Contact
/*
Description string `json:"Description"`
CallerID interface{} `json:"Caller_ID"`
CallAgenda string `json:"Call_Agenda"`
CallPurpose string `json:"Call_Purpose"`
CallStatus string `json:"Call_Status"`
Reminder interface{} `json:"Reminder"`
CallResult string `json:"Call_Result"`
//WhatID NameID `json:"What_Id"`
CallDurationInSeconds string `json:"Call_Duration_in_seconds"`
Tag []interface{} `json:"Tag"`
DialledNumber interface{} `json:"Dialled_Number"`*/
}
func CreateCall(call NewCall, contactId string) (string, error) {
call.Owner = Owner{ID: "477339000000309001", Email: "julie.dupoty@algosup.com"} // Julie
call.WhoID.ID = contactId
call.SeModule = "Activities"
b, err := json.Marshal(&calls{Data: []NewCall{call}})
if err != nil {
return "", err
}
req, err := http.NewRequest("POST", "https://www.zohoapis.eu/crm/v4/Calls", 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 = ioutil.ReadAll(r.Body)
if err != nil {
return "", err
}
log.Println(string(b))
return "", errors.New(r.Status)
}
var res updateContactResult
err = json.NewDecoder(r.Body).Decode(&res)
if err != nil {
return "", err
}
return res.Data[0].Details.ID, nil
}
func GetCalls(contactID string) ([]ContactCall, error) {
//"SELECT id,Who_Id FROM Calls WHERE Owner.id='477339000000309001'"
b, err := json.Marshal(&selectQuery{Query: fmt.Sprintf("SELECT Subject,Call_Type,Call_Start_Time,Owner,Call_Result,Call_Duration_in_seconds FROM Calls WHERE Who_Id.id='%s'", contactID)})
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", "https://www.zohoapis.eu/crm/v4/coql", bytes.NewReader(b))
if err != nil {
return nil, err
}
req.Header.Set("Authorization", "Zoho-oauthtoken "+auth.AccessToken)
req.Header.Set("Content-Type", "text/plain")
r, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer r.Body.Close()
if r.StatusCode == http.StatusNoContent {
return nil, nil
}
if r.StatusCode != http.StatusOK {
b, err = ioutil.ReadAll(r.Body)
if err != nil {
return nil, err
}
log.Println(string(b))
return nil, errors.New(r.Status)
}
var res getCalls
err = json.NewDecoder(r.Body).Decode(&res)
if err != nil {
return nil, err
}
return res.Data, nil
}