Add sms every8d, mitake, smsget

tags/v1.2.3 v1.2.3
Stanly 3 years ago
parent 9db4b3652f
commit 066f6829bd

@ -0,0 +1,10 @@
package sms
import (
"fmt"
)
var (
ErrResponse = fmt.Errorf("Resposne error.")
ErrNotEnoughCredit = fmt.Errorf("Not enough credit.")
)

@ -0,0 +1,59 @@
package sms
import (
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"strings"
"time"
)
type Every8D struct {
Username string
Password string
}
func NewSmsEvery8D(username, password string) *Every8D {
return &Every8D{
Username: username,
Password: password,
}
}
func (every8d *Every8D) Send(mobile, msg string) (int, error) {
qs := url.Values{}
qs.Add("UID", every8d.Username)
qs.Add("PWD", every8d.Password)
qs.Add("DEST", mobile)
qs.Add("MSG", url.QueryEscape(msg))
client := http.Client{Timeout: 2 * time.Second}
resp, err := client.Get(fmt.Sprintf("https://oms.every8d.com/API21/HTTP/sendSMS.ashx?%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()
data := strings.Split(string(bs), ",")
if len(data) != 5 {
return 0, ErrResponse
}
credit, err := strconv.ParseFloat(data[0], 64)
if err != nil {
return 0, err
}
if credit < 0 {
return 0, ErrNotEnoughCredit
}
return int(credit), nil
}

@ -0,0 +1,62 @@
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
}

@ -0,0 +1,64 @@
package sms
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"strings"
)
type SmsGet struct {
Username string
Password string
}
func NewSmsGet(username, password string) *SmsGet {
return &SmsGet{
Username: username,
Password: password,
}
}
func (smsget *SmsGet) Send(mobile, msg string) (int, error) {
qs := url.Values{}
qs.Add("method", "1")
qs.Add("username", smsget.Username)
qs.Add("password", smsget.Password)
qs.Add("sms_msg", url.QueryEscape(msg))
resp, err := http.Get(fmt.Sprintf("https://sms-get2.com/api_send.php?%s", qs.Encode()))
if err != nil {
return 0, err
}
defer resp.Body.Close()
bs, err := ioutil.ReadAll(resp.Body)
if err != nil {
return 0, err
}
var res struct {
Status bool `json:"stats"`
Code int `json:"error_code,string"`
Msg string `json:"error_msg"`
}
if err := json.Unmarshal(bs, &res); err != nil {
return 0, ErrResponse
}
if res.Status == false || res.Code != 0 {
if res.Code == 9 {
return 0, ErrNotEnoughCredit
}
return 0, ErrResponse
}
credit, err := strconv.Atoi(strings.Split(res.Msg, "|")[2])
if err != nil {
return 0, err
}
return credit, nil
}
Loading…
Cancel
Save