/
conversations.go
119 lines (91 loc) · 2.89 KB
/
conversations.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
package pulsesms
import (
"encoding/json"
"fmt"
"strings"
)
// ConversationID is the internal ID of a group or one-on-one chat / thread
type conversationID = int
type PhoneNumber = string
type conversation struct {
ID conversationID `json:"id,omitempty"`
DeviceId DeviceID `json:"device_id,omitempty"`
FolderId int `json:"folder_id,omitempty"`
Read bool `json:"read,omitempty"`
Timestamp int64 `json:"timestamp,omitempty"`
Title string `json:"title,omitempty"`
Archive bool `json:"archive,omitempty"`
Mute bool `json:"mute,omitempty"`
PhoneNumbers string `json:"phone_numbers,omitempty"`
Snippet string `json:"snippet,omitempty"`
}
func (conv conversation) members() []PhoneNumber {
return strings.Split(conv.PhoneNumbers, " ")
}
func (c *Client) getConversation(convoID conversationID) (conversation, error) {
convo := conversation{}
endpoint := c.getUrl(EndpointConversation)
path := fmt.Sprintf("%s/%s", endpoint, fmt.Sprint(convoID))
resp, err := c.api.R().
SetQueryParam("account_id", fmt.Sprint(c.accountID)).
Get(path)
if err != nil {
fmt.Println(resp.Status())
return convo, err
}
err = json.Unmarshal(resp.Body(), &convo)
if err != nil {
return convo, fmt.Errorf("failed to unmarshall conversation: %v", err)
}
err = decryptConversation(c.crypto.cipher, &convo)
if err != nil {
return convo, fmt.Errorf("failed to decrypt conversation %v", err)
}
return convo, nil
}
func (c *Client) listConversations() ([]conversation, error) {
index := "index_public_unarchived"
endpoint := c.getUrl(EndpointConversations)
path := fmt.Sprintf("%s/%s", endpoint, index)
resp, err := c.api.R().
SetQueryParam("account_id", fmt.Sprint(c.accountID)).
SetQueryParam("limit", fmt.Sprint(75)).
Get(path)
if err != nil {
fmt.Println(resp.Status())
return nil, err
}
convos := []conversation{}
err = json.Unmarshal(resp.Body(), &convos)
if err != nil {
return nil, fmt.Errorf("failed to unmarshall conversations: %v", err)
}
result := []conversation{}
for _, conv := range convos {
err := decryptConversation(c.crypto.cipher, &conv)
if err != nil {
return nil, fmt.Errorf("failed to decrypt conversation %v", err)
}
result = append(result, conv)
}
return result, nil
}
func (c *Client) updateConversation(conversationID conversationID, snippet string, timestamp int64) error {
req := updateConversationRequest{
AccountID: c.accountID,
Read: false,
Timestamp: timestamp,
Snippet: snippet,
}
endpoint := c.getUrl(EndpointUpdateConversation)
endpoint = fmt.Sprintf("%s/%s", endpoint, fmt.Sprint(conversationID))
resp, err := c.api.R().
SetHeader("Content-Type", "application/json").
SetBody(req).
Post(endpoint)
fmt.Println(resp.Status())
if resp.StatusCode() > 200 || err != nil {
return err
}
return nil
}