-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage.go
148 lines (126 loc) · 3.49 KB
/
message.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
package zgo
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
)
const (
SrcOA = iota
SrcUser
)
type GetConversationResp struct {
Data Conversation `json:"data"`
ErrorResp
}
type Conversation []Message
func (c Conversation) Profile() Profile {
if len(c) == 0 {
return Profile{}
}
switch c[0].Src {
case SrcOA:
return Profile{
UserID: c[0].ToID,
DisplayName: c[0].ToDisplayName,
}
case SrcUser:
return Profile{
UserID: c[0].FromID,
DisplayName: c[0].FromDisplayName,
}
}
return Profile{}
}
type Profile struct {
UserID string `json:"user_id"`
DisplayName string `json:"display_name"`
}
type Message struct {
Src int `json:"src" bson:"src"`
Time int64 `json:"time" bson:"time"`
SentTime string `json:"sent_time" bson:"sent_time"`
FromID string `json:"from_id" bson:"from_id"`
FromDisplayName string `json:"from_display_name" bson:"from_display_name"`
FromAvatar string `json:"from_avatar" bson:"from_avatar"`
ToID string `json:"to_id" bson:"to_id"`
ToDisplayName string `json:"to_display_name" bson:"to_display_name"`
ToAvatar string `json:"to_avatar" bson:"to_avatar"`
MessageID string `json:"message_id" bson:"message_id"`
Type string `json:"type" bson:"type"`
Message string `json:"message" bson:"message"`
Links []Link `json:"links,omitempty" bson:"links"`
Thumb string `json:"thumb" bson:"thumb"`
URL string `json:"url" bson:"url"`
Description string `json:"description" bson:"description"`
}
type Link struct {
Title string `json:"title" bson:"title"`
URL string `json:"url" bson:"url"`
Thumb string `json:"thumb" bson:"thumb"`
Description string `json:"description" bson:"description"`
}
type LinkDescription struct {
Caption string `json:"caption"`
Phone string `json:"phone"`
}
func (l Link) String() string {
var data LinkDescription
if err := json.Unmarshal([]byte(l.Description), &data); err != nil {
return l.URL
}
switch l.URL {
case "https://zalo.me":
return data.Caption
case "www.zaloapp.com":
return fmt.Sprintf("Danh thiếp: %s - %s", l.Title, data.Phone)
default:
return l.URL
}
}
type GetConversationReq struct {
UserID int64 `json:"user_id"`
Offset int `json:"offset"`
Count int `json:"count"`
}
func (z *zgo) GetConversation(ctx context.Context, accessToken string, reqData GetConversationReq) (GetConversationResp, error) {
u, err := url.Parse(getConversationURL)
if err != nil {
return GetConversationResp{}, err
}
bytes, err := json.Marshal(reqData)
if err != nil {
return GetConversationResp{}, err
}
params := url.Values{}
params.Set("data", string(bytes))
u.RawQuery = params.Encode()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
if err != nil {
return GetConversationResp{}, err
}
req.Header.Set("access_token", accessToken)
resp, err := z.httpClient.Do(req)
if err != nil {
return GetConversationResp{}, err
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != http.StatusOK {
return GetConversationResp{}, errors.New("request failed")
}
responseBody, err := io.ReadAll(resp.Body)
if err != nil {
return GetConversationResp{}, err
}
var data GetConversationResp
if err := json.Unmarshal(responseBody, &data); err != nil {
return GetConversationResp{}, err
}
if data.Error != 0 {
return data, fmt.Errorf("request failed: %+v", data)
}
return data, nil
}