-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstatus.go
208 lines (178 loc) · 5.85 KB
/
status.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
package tggateway
import (
"encoding/json"
"time"
)
const (
CODE_VALID string = "code_valid"
CODE_INVALID = "code_invalid"
CODE_MAX_ATTEMPTS_EXCEEDED = "code_max_attempts_exceeded"
CODE_EXPIRED = "expired"
MESSAGE_SENT = "sent"
MESSAGE_READ = "read"
MESSAGE_REVOKED = "revoked"
)
type requestStatusJson struct {
RequestId string `json:"request_id"`
PhoneNumber string `json:"phone_number"`
RequestCost float32 `json:"request_cost"`
RemainingBalance *float32 `json:"remaining_balance"`
DeliveryStatus *struct {
Status string `json:"status"`
UpdatedAt int `json:"updated_at"`
} `json:"delivery_status"`
VerificationStatus *struct {
Status string `json:"status"`
UpdatedAt int `json:"updated_at"`
CodeEntered *string `json:"code_entered"`
} `json:"verification_status"`
Payload *string `json:"payload"`
}
type deliveryStatus struct {
status string
updatedAt int
}
type verificationStatus struct {
status string
updatedAt int
codeEntered *string
}
type RequestStatus struct {
rawJson []byte
requestId string
phoneNumber string
requestCost float32
remainingBalance *float32
deliveryStatus *deliveryStatus
verificationStatus *verificationStatus
payload *string
}
func (r *RequestStatus) UnmarshalJSON(data []byte) error {
var requestStatusJson requestStatusJson
if err := json.Unmarshal(data, &requestStatusJson); err != nil {
return err
}
r.rawJson = data
r.requestId = requestStatusJson.RequestId
r.phoneNumber = requestStatusJson.PhoneNumber
r.requestCost = requestStatusJson.RequestCost
r.remainingBalance = requestStatusJson.RemainingBalance
if requestStatusJson.DeliveryStatus != nil {
r.deliveryStatus = &deliveryStatus{
status: requestStatusJson.DeliveryStatus.Status,
updatedAt: requestStatusJson.DeliveryStatus.UpdatedAt,
}
}
if requestStatusJson.VerificationStatus != nil {
r.verificationStatus = &verificationStatus{
status: requestStatusJson.VerificationStatus.Status,
updatedAt: requestStatusJson.VerificationStatus.UpdatedAt,
codeEntered: requestStatusJson.VerificationStatus.CodeEntered,
}
}
r.payload = requestStatusJson.Payload
return nil
}
// Raw JSON data returned by the API.
func (r RequestStatus) RawJson() []byte {
return r.rawJson
}
// Unique identifier of the verification request.
func (r RequestStatus) RequestId() string {
return r.requestId
}
// The phone number to which the verification code was sent, in the E.164 format.
func (r RequestStatus) PhoneNumber() string {
return r.phoneNumber
}
// Total request cost incurred by either checkSendAbility or sendVerificationMessage.
func (r RequestStatus) RequestCost() float32 {
return r.requestCost
}
// Remaining balance in credits. Returned only in response to a request that incurs a charge.
func (r RequestStatus) RemainingBalance() *float32 {
return r.remainingBalance
}
// The current status of the message.
func (r RequestStatus) DeliveryStatus() *string {
if r.deliveryStatus != nil {
return &r.deliveryStatus.status
}
return nil
}
// The timestamp when the status was last updated.
func (r RequestStatus) DeliveryUpdatedAt() *int {
if r.deliveryStatus != nil {
return &r.deliveryStatus.updatedAt
}
return nil
}
// Delivery status updated at in UTC format.
func (r RequestStatus) DeliveryUpdatedAtUTC() *time.Time {
if r.deliveryStatus != nil {
t := unixToTime(r.deliveryStatus.updatedAt)
return &t
}
return nil
}
// True if the message has been sent to the recipient's device(s).
func (r RequestStatus) IsMessageSent() bool {
return r.deliveryStatus != nil && r.deliveryStatus.status == MESSAGE_SENT
}
// True if the message has been read by the recipient.
func (r RequestStatus) IsMessageRead() bool {
return r.deliveryStatus != nil && r.deliveryStatus.status == MESSAGE_READ
}
// True if the message has been revoked.
func (r RequestStatus) IsMessageRevoked() bool {
return r.deliveryStatus != nil && r.deliveryStatus.status == MESSAGE_REVOKED
}
// The current status of the verification process.
func (r RequestStatus) VerificationStatus() *string {
if r.verificationStatus != nil {
return &r.verificationStatus.status
}
return nil
}
// The timestamp for this particular status. Represents the time when the status was last updated.
func (r RequestStatus) VerificationUpdatedAt() *int {
if r.verificationStatus != nil {
return &r.verificationStatus.updatedAt
}
return nil
}
// Verification status updated at in UTC format.
func (r RequestStatus) VerificationUpdatedAtUTC() *time.Time {
if r.verificationStatus != nil {
t := unixToTime(r.verificationStatus.updatedAt)
return &t
}
return nil
}
// The code entered by the user.
func (r RequestStatus) VerificationCodeEntered() *string {
if r.verificationStatus != nil {
return r.verificationStatus.codeEntered
}
return nil
}
// True if the code entered by the user is correct.
func (r RequestStatus) IsCodeValid() bool {
return r.verificationStatus != nil && r.verificationStatus.status == CODE_VALID
}
// True if the code entered by the user is incorrect.
func (r RequestStatus) IsCodeInvalid() bool {
return r.verificationStatus != nil && r.verificationStatus.status == CODE_INVALID
}
// True if the maximum number of attempts to enter the code has been exceeded.
func (r RequestStatus) IsCodeMaxAttemptsExceeded() bool {
return r.verificationStatus != nil && r.verificationStatus.status == CODE_MAX_ATTEMPTS_EXCEEDED
}
// True if the code has expired and can no longer be used for verification.
func (r RequestStatus) IsCodeExpired() bool {
return r.verificationStatus != nil && r.verificationStatus.status == CODE_EXPIRED
}
// Custom payload if it was provided in the request, 0-256 bytes.
func (r RequestStatus) Payload() *string {
return r.payload
}