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 || 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 }