/
plivo.go
97 lines (85 loc) · 2.17 KB
/
plivo.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
// Package plivo is to send SMS tools.
package plivo
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"net/url"
"path"
"strings"
"time"
"unicode/utf8"
)
// PlivoAPI URL.
const PlivoAPI = "https://api.plivo.com/v1/Account"
// Fixed http too many open files.
var httpClient = &http.Client{Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
Dial: (&net.Dialer{
Timeout: 0,
KeepAlive: 0,
}).Dial,
TLSHandshakeTimeout: 10 * time.Second,
},
}
// Message struct
type Message struct {
dst string
src string
text string
account *Account
}
// NewMessage new a Message.
func NewMessage(dst, src, text string, account *Account) *Message {
return &Message{dst: dst, src: src, text: text, account: account}
}
func (m Message) toMap() map[string]string {
var result = make(map[string]string)
result["dst"] = m.dst
result["src"] = m.src
result["text"] = m.text
return result
}
// Send to send SMS.
func (m Message) Send() {
jsonData, _ := json.Marshal(m.toMap())
req, _ := http.NewRequest("POST", m.URL("/Message/"), bytes.NewReader(jsonData))
req.URL.User = url.UserPassword(m.account.User, m.account.Password)
req.Header = http.Header{"Content-Type": {"application/json"}}
if resp, err := httpClient.Do(req); err == nil {
if body, err := ioutil.ReadAll(resp.Body); err == nil {
defer resp.Body.Close()
log.Printf("%s\n%s\n", m.String(), body)
}
}
}
// String to print string.
func (m Message) String() string {
return fmt.Sprintf(`<dst: "%s" src: "%s" text: "%s" Len: %d>`, m.dst, m.src, m.text, m.Len())
}
// Len to show length of Message text.
func (m Message) Len() int {
return utf8.RuneCountInString(m.text)
}
// URL to render api full URL.
func (m Message) URL(urlpath string) string {
URLPath, _ := url.ParseRequestURI(PlivoAPI)
URLPath.Path = path.Join(URLPath.Path, m.account.User, urlpath)
if strings.LastIndex(urlpath, "/") >= 0 {
return fmt.Sprintf("%s/", URLPath.String())
}
return URLPath.String()
}
// Account struct
type Account struct {
User string
Password string
}
// MakeBulkDst to format bulk dst numbers.
func MakeBulkDst(dst, sep string) string {
return strings.Replace(dst, sep, "<", -1)
}