You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
63 lines
1.2 KiB
Go
63 lines
1.2 KiB
Go
3 years ago
|
package sms
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"io/ioutil"
|
||
|
"net/http"
|
||
|
"net/url"
|
||
|
"strconv"
|
||
|
"strings"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
type Mitake struct {
|
||
|
Username string
|
||
|
Password string
|
||
|
}
|
||
|
|
||
|
func NewSmsMitake(username, password string) *Mitake {
|
||
|
return &Mitake{
|
||
|
Username: username,
|
||
|
Password: password,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (mitake *Mitake) Send(mobile, msg string) (int, error) {
|
||
|
qs := url.Values{}
|
||
|
qs.Add("CharsetURL", "UTF8")
|
||
|
qs.Add("username", mitake.Username)
|
||
|
qs.Add("password", mitake.Password)
|
||
|
qs.Add("dstaddr", mobile)
|
||
|
qs.Add("smbody", url.QueryEscape(msg))
|
||
|
|
||
|
client := http.Client{Timeout: 2 * time.Second}
|
||
|
resp, err := client.Get(fmt.Sprintf("http://smsb2c.mitake.com.tw/b2c/mtk/SmSend?%s", qs.Encode()))
|
||
|
if err != nil {
|
||
|
return 0, err
|
||
|
}
|
||
|
|
||
|
bs, err := ioutil.ReadAll(resp.Body)
|
||
|
if err != nil {
|
||
|
return 0, err
|
||
|
}
|
||
|
defer resp.Body.Close()
|
||
|
|
||
|
if len(strings.Split(string(bs), "\n")) != 6 {
|
||
|
return 0, ErrResponse
|
||
|
}
|
||
|
|
||
|
qs, err = url.ParseQuery(strings.ReplaceAll(string(bs), "\n", "&"))
|
||
|
if err != nil {
|
||
|
return 0, err
|
||
|
}
|
||
|
|
||
|
credit, err := strconv.ParseInt(strings.ReplaceAll(qs.Get("AccountPoint"), "\r", ""), 10, 64)
|
||
|
if err != nil {
|
||
|
return 0, err
|
||
|
} else if credit < 0 {
|
||
|
return 0, ErrNotEnoughCredit
|
||
|
}
|
||
|
|
||
|
return int(credit), nil
|
||
|
}
|