diff --git a/basic.go b/basic.go index 1dae2b1..f28344d 100644 --- a/basic.go +++ b/basic.go @@ -23,6 +23,7 @@ const ( Huyi = "Huyi SMS" HuaweiCloud = "Huawei Cloud SMS" Twilio = "Twilio SMS" + SmsBao = "SmsBao SMS" MockSms = "Mock SMS" ) @@ -44,6 +45,8 @@ func NewSmsClient(provider string, accessId string, accessKey string, sign strin return GetHuaweiClient(accessId, accessKey, sign, template, other) case Twilio: return GetTwilioClient(accessId, accessKey, template) + case SmsBao: + return GetSmsbaoClient(accessId, accessKey, sign, template, other) case MockSms: return NewMocker(accessId, accessKey, sign, template, other) default: diff --git a/smsbao.go b/smsbao.go new file mode 100644 index 0000000..0d079f6 --- /dev/null +++ b/smsbao.go @@ -0,0 +1,91 @@ +// Copyright 2022 The Casdoor Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package go_sms_sender + +import ( + "fmt" + "io/ioutil" + "net/http" + "net/url" +) + +type SmsBaoClient struct { + username string + apikey string + sign string + template string + goodsid string +} + +func GetSmsbaoClient(username string, apikey string, sign string, template string, other []string) (*SmsBaoClient, error) { + var goodsid string + if len(other) < 1 { + goodsid = "" + } else { + goodsid = other[0] + } + return &SmsBaoClient{ + username: username, + apikey: apikey, + sign: sign, + template: template, + goodsid: goodsid, + }, nil +} + +func (c *SmsBaoClient) SendMessage(param map[string]string, targetPhoneNumber ...string) error { + code, ok := param["code"] + if !ok { + return fmt.Errorf("missing parameter: msg code") + } + + if len(targetPhoneNumber) < 1 { + return fmt.Errorf("missing parameter: targetPhoneNumber") + } + + smsContent := url.QueryEscape("【" + c.sign + "】" + fmt.Sprintf(c.template, code)) + for _, mobile := range targetPhoneNumber { + // https://api.smsbao.com/sms?u=USERNAME&p=PASSWORD&g=GOODSID&m=PHONE&c=CONTENT + url := fmt.Sprintf("https://api.smsbao.com/sms?u=%s&p=%s&g=%s&m=%s&c=%s", c.username, c.apikey, c.goodsid, mobile, smsContent) + + client := &http.Client{} + req, _ := http.NewRequest("GET", url, nil) + resp, err := client.Do(req) + if err != nil { + return err + } + defer resp.Body.Close() + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return err + } + switch string(body) { + case "30": + return fmt.Errorf("password error") + case "40": + return fmt.Errorf("account not exist") + case "41": + return fmt.Errorf("overdue account") + case "43": + return fmt.Errorf("IP address limit") + case "50": + return fmt.Errorf("content contain forbidden words") + case "51": + return fmt.Errorf("phone number incorrect") + } + } + + return nil +}