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.
60 lines
1.0 KiB
Go
60 lines
1.0 KiB
Go
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
|
|
}
|