Skip to content

Commit

Permalink
feat: add smsbao sms (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
gtn1024 authored Sep 26, 2022
1 parent 4f1ed60 commit 1462aa9
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
3 changes: 3 additions & 0 deletions basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const (
Huyi = "Huyi SMS"
HuaweiCloud = "Huawei Cloud SMS"
Twilio = "Twilio SMS"
SmsBao = "SmsBao SMS"
MockSms = "Mock SMS"
)

Expand All @@ -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:
Expand Down
91 changes: 91 additions & 0 deletions smsbao.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 1462aa9

Please sign in to comment.