From 74f6e62d2a3b144acdfbd23e87dd4190378a894d Mon Sep 17 00:00:00 2001 From: cutedragon <79246970+cutedrag0n@users.noreply.github.com> Date: Thu, 9 May 2024 00:56:42 +0800 Subject: [PATCH] feat: add UniSMS Sender (#46) --- basic.go | 3 +++ go.mod | 1 + go.sum | 2 ++ unisms.go | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 78 insertions(+) create mode 100644 unisms.go diff --git a/basic.go b/basic.go index 9635faf..bb3b511 100644 --- a/basic.go +++ b/basic.go @@ -36,6 +36,7 @@ const ( MockSms = "Mock SMS" Netgsm = "Netgsm SMS" OsonSms = "OSON SMS" + UniSms = "Uni SMS" ) type SmsClient interface { @@ -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) } diff --git a/go.mod b/go.mod index 69b3de8..f1f8e63 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index c2aa251..be0b7d6 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/unisms.go b/unisms.go new file mode 100644 index 0000000..c6c3a54 --- /dev/null +++ b/unisms.go @@ -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 +}