Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:Add QuanmSMS provider #42

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const (
MockSms = "Mock SMS"
Netgsm = "Netgsm SMS"
OsonSms = "OSON SMS"
QuanmSms = "Quanm SMS"
)

type SmsClient interface {
Expand Down Expand Up @@ -79,6 +80,8 @@ func NewSmsClient(provider string, accessId string, accessKey string, sign strin
return NewMocker(accessId, accessKey, sign, template, other)
case OsonSms:
return GetOsonClient(accessId, accessKey, sign, template)
case QuanmSms:
return GetQuanmSMSClient(accessId, accessKey, template)
default:
return nil, fmt.Errorf("unsupported provider: %s", provider)
}
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
)

require (
gitee.com/chengdu-quanming-network/quanmsms-go v0.1.0 // indirect
github.com/cenkalti/backoff/v4 v4.1.2 // indirect
github.com/golang/mock v1.6.0 // indirect
github.com/google/go-querystring v1.1.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ cloud.google.com/go/storage v1.6.0/go.mod h1:N7U0C8pVQ/+NIKOBQyamJIeKQKkZ+mxpohl
cloud.google.com/go/storage v1.8.0/go.mod h1:Wv1Oy7z6Yz3DshWRJFhqM/UCfaWIRTdp0RXyy7KQOVs=
cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9ullr3+Kg0=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
gitee.com/chengdu-quanming-network/quanmsms-go v0.1.0 h1:2Ot4+ZGS5ykBhJKDoQ5noSID2Ls2YHnXEl5qRsEHlHk=
gitee.com/chengdu-quanming-network/quanmsms-go v0.1.0/go.mod h1:oc0vKKw79jIOADGIzTjsyFV/624toSY81ptfpihmH18=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/DataDog/datadog-go v3.2.0+incompatible/go.mod h1:LButxg5PwREeZtORoXG3tL4fMGNddJ+vMq1mwgfaqoQ=
Expand Down
106 changes: 106 additions & 0 deletions quanmsms.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// 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"
"strconv"

"gitee.com/chengdu-quanming-network/quanmsms-go"
)

// console: https://dev.quanmwl.com/console
// The use of SDK is to ensure the use of the latest standards

type QuanmSMSClient struct {
openid string
apikey string
templateid string
}

func GetQuanmSMSClient(openid string, apikey string, templateid string) (*QuanmSMSClient, error) {
return &QuanmSMSClient{
openid: openid,
apikey: apikey,
templateid: templateid,
}, nil
}

func (c *QuanmSMSClient) SendMessage(param map[string]string, targetPhoneNumber ...string) error {
code, ok := param["code"]
if !ok {
return fmt.Errorf("missing parameter: code")
}

if len(targetPhoneNumber) == 0 {
return fmt.Errorf("missing parameter: targetPhoneNumber")
}

smsSDK := quanmsms.NewSmsSDK(c.openid, c.apikey, "https")
template_args := map[string]interface{}{"code": code}
for _, tel := range targetPhoneNumber {
templateidInt64, intErr := strconv.Atoi(c.templateid)
if intErr != nil {
return fmt.Errorf("template id error")
}
resp, err := smsSDK.Send(tel, int8(templateidInt64), template_args)
if err != nil {
return fmt.Errorf("other error:" + err.Error())
}
switch string(resp.State) {
case "201":
// 表单信息有误或触发限发机制
return fmt.Errorf("refusal to send due to security reasons")
case "202":
// 账户重复
return fmt.Errorf("account repeat")
case "203":
// 服务器异常或限流
return fmt.Errorf("server error,Please try again later")
case "205":
// 请求不安全
return fmt.Errorf("illegal request")
case "207":
// 配额不足
return fmt.Errorf("insufficient balance")
case "208":
// 验签失败
return fmt.Errorf("verification failed")
case "209":
// 账户被禁用接口
return fmt.Errorf("insufficient permissions")
case "210":
// 账户被冻结
return fmt.Errorf("account frozen")
case "211":
// 请求参数超过上限
return fmt.Errorf("parameter too long")
case "212":
// 权限不足或使用了他人模板
return fmt.Errorf("insufficient permissions or using someone else's template")
case "213":
// 调用状态异常
return fmt.Errorf("status error")
case "215":
// 内容受限
return fmt.Errorf("content restricted")
case "216":
// 内容违法
return fmt.Errorf("content violation")
}
}

return nil
}
Loading