-
Notifications
You must be signed in to change notification settings - Fork 33
/
user.go
117 lines (100 loc) · 2.64 KB
/
user.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
package viber
import "encoding/json"
// User struct as part of UserDetails
type User struct {
ID string `json:"id"`
Name string `json:"name"`
Avatar string `json:"avatar"`
Country string `json:"country"`
Language string `json:"language"`
PrimaryDeviceOs string `json:"primary_device_os"`
APIVersion int `json:"api_version"`
ViberVersion string `json:"viber_version"`
Mcc int `json:"mcc"`
Mnc int `json:"mnc"`
DeviceType string `json:"device_type"`
}
// UserDetails for Viber user
type UserDetails struct {
Status int `json:"status"`
StatusMessage string `json:"status_message"`
MessageToken int64 `json:"message_token"`
User `json:"user"`
}
// Online status struct
type online struct {
Status int `json:"status"`
StatusMessage string `json:"status_message"`
Users []UserOnline `json:"users"`
}
// UserOnline response struct
type UserOnline struct {
ID string `json:"id"`
OnlineStatus int `json:"online_status"`
OnlineStatusMessage string `json:"online_status_message"`
LastOnline int64 `json:"last_online,omitempty"`
}
// UserDetails of user id
func (v *Viber) UserDetails(id string) (UserDetails, error) {
/*
b := []byte(`{
"status": 0,
"status_message": "ok",
"message_token": 4912661846655238145,
"user": {
"id": "01234567890A=",
"name": "John McClane",
"avatar": "http://avatar.example.com",
"country": "UK",
"language": "en",
"primary_device_os": "android 7.1",
"api_version": 1,
"viber_version": "6.5.0",
"mcc": 1,
"mnc": 1
}
}`)
var u UserDetails
err := json.Unmarshal(b, &u)
return u, err
*/
var u UserDetails
s := struct {
ID string `json:"id"`
}{
ID: id,
}
b, err := v.PostData("https://chatapi.viber.com/pa/get_user_details", s)
if err != nil {
return u, err
}
if err := json.Unmarshal(b, &u); err != nil {
return u, err
}
// viber error returned
if u.Status != 0 {
return u, Error{Status: u.Status, StatusMessage: u.StatusMessage}
}
return u, err
}
// UserOnline status
func (v *Viber) UserOnline(ids []string) ([]UserOnline, error) {
var uo online
req := struct {
IDs []string `json:"ids"`
}{
IDs: ids,
}
b, err := v.PostData("https://chatapi.viber.com/pa/get_online", req)
if err != nil {
return []UserOnline{}, err
}
if err := json.Unmarshal(b, &uo); err != nil {
return []UserOnline{}, err
}
// viber error
if uo.Status != 0 {
return []UserOnline{}, Error{Status: uo.Status, StatusMessage: uo.StatusMessage}
}
return uo.Users, nil
}