Skip to content

Commit

Permalink
feat: add UniSMS Sender (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
cutedrag0n authored May 8, 2024
1 parent 9ff519a commit 74f6e62
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 0 deletions.
3 changes: 3 additions & 0 deletions basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const (
MockSms = "Mock SMS"
Netgsm = "Netgsm SMS"
OsonSms = "OSON SMS"
UniSms = "Uni SMS"
)

type SmsClient interface {
Expand Down Expand Up @@ -82,6 +83,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 UniSms:
return GetUnismsClient(accessId, accessKey, sign, 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 (
github.com/apistd/uni-go-sdk v0.0.2 // 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 @@ -53,6 +53,8 @@ github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk5
github.com/aliyun/alibaba-cloud-sdk-go v1.62.545 h1:0LfzeUr4quwrrrTHn1kfLA0FBdsChCMs8eK2EzOwXVQ=
github.com/aliyun/alibaba-cloud-sdk-go v1.62.545/go.mod h1:Api2AkmMgGaSUAhmk76oaFObkoeCPc/bKAqcyplPODs=
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
github.com/apistd/uni-go-sdk v0.0.2 h1:7kqETCOz/rz8AQU55XGzxDFGoFeMgeZL5fGwvxKBZrc=
github.com/apistd/uni-go-sdk v0.0.2/go.mod h1:eIqYos4IbHgE/rB75r05ypNLahooEMJCrbjXq322b74=
github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o=
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY=
github.com/armon/go-metrics v0.3.9/go.mod h1:4O98XIr/9W0sxpJ8UaYkvjk10Iff7SnFrb4QAOwNTFc=
Expand Down
72 changes: 72 additions & 0 deletions unisms.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright 2024 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 (
"errors"
"fmt"
"strings"

unisms "github.com/apistd/uni-go-sdk/sms"
)

type UnismsClient struct {
core *unisms.UniSMSClient
sign string
template string
}

func GetUnismsClient(accessId string, accessKey string, signature string, templateId string) (*UnismsClient, error) {
client := unisms.NewClient(accessId, accessKey)

// Check the correctness of the accessId and accessKey
msg := unisms.BuildMessage()
msg.SetTo("test")
msg.SetTemplateId("pub_verif_register") // free template
_, err := client.Send(msg)
if strings.Contains(err.Error(), "[104111] InvalidAccessKeyId") {
return nil, err
}

unismsClient := &UnismsClient{
core: client,
sign: signature,
template: templateId,
}

return unismsClient, nil
}

func (c *UnismsClient) SendMessage(param map[string]string, targetPhoneNumber ...string) error {
if len(targetPhoneNumber) == 0 {
return fmt.Errorf("missing parameter: targetPhoneNumber")
}

msg := unisms.BuildMessage()
msg.SetTo(targetPhoneNumber...)
msg.SetSignature(c.sign)
msg.SetTemplateId(c.template)

resp, err := c.core.Send(msg)
if err != nil {
return err
}

if resp.Code != "0" {
return errors.New(resp.Message)
}

return nil
}

0 comments on commit 74f6e62

Please sign in to comment.