-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathvoice_sender.go
80 lines (61 loc) · 1.73 KB
/
voice_sender.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
package luosimao
import (
"encoding/json"
"fmt"
"net/url"
"time"
"github.com/parnurzeal/gorequest"
)
type VoiceSender struct {
sendUrl string
statusUrl string
auth Authorization
Proxy string
}
func NewVoiceSender(auth Authorization, proto ProtocalType) *VoiceSender {
sender := new(VoiceSender)
switch proto {
case JSON:
{
sender.sendUrl = VoiceServerURL + "verify.json"
sender.statusUrl = VoiceServerURL + "status.json"
}
case XML:
{
sender.sendUrl = VoiceServerURL + "verify.xml"
sender.statusUrl = VoiceServerURL + "status.xml"
}
}
sender.auth = auth
return sender
}
func (p *VoiceSender) Send(req VoiceRequest, timeout int64) (response Response, err error) {
to := time.Duration(timeout)
request := gorequest.New().Timeout(to * time.Millisecond).Proxy(p.Proxy)
strCode := fmt.Sprintf("%04d", req.Code)
params := url.Values{}
params.Add("mobile", req.Mobile)
params.Add("code", strCode)
_, body, errs := request.Post(p.sendUrl).Set("Authorization", p.auth.BasicAuthorization()).Set("Content-Type", "application/x-www-form-urlencoded").Send(params.Encode()).End()
if err = errors_to_error(errs); err != nil {
return
}
if e := json.Unmarshal([]byte(body), &response); e != nil {
err = e
return
}
return
}
func (p *VoiceSender) Status(timeout int64) (status Status, err error) {
to := time.Duration(timeout)
request := gorequest.New().Timeout(to * time.Millisecond).Proxy(p.Proxy)
_, body, errs := request.Post(p.statusUrl).Set("Authorization", p.auth.BasicAuthorization()).Set("Content-Type", "application/x-www-form-urlencoded").End()
if err = errors_to_error(errs); err != nil {
return
}
if e := json.Unmarshal([]byte(body), &status); e != nil {
err = e
return
}
return
}