mirror of
https://github.com/hanzoai/go-sms-sender.git
synced 2026-10-30 18:36:05 +00:00
1 module(s) in this repo declared go < 1.22. Go 1.22 changed for-loop variable scoping to per-iteration, so raising the directive past 1.22 can change runtime behaviour. Each module here was scanned for loops whose loop variable is captured by a func literal / goroutine / defer, or has its address taken: zero such sites were found, so the bump is behaviour-preserving. Also fix 5 non-constant format strings (fmt.Errorf(msg) -> errors.New(msg)), which Go's printf vet check flags from language version 1.24 on. Verified per module with go build, go vet and go test (-mod=readonly). Co-authored-by: Hanzo Dev <[email protected]>
89 lines
2.1 KiB
Go
89 lines
2.1 KiB
Go
// Copyright 2021 The Hanzo IAM 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 (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/aliyun/alibaba-cloud-sdk-go/services/dysmsapi"
|
|
)
|
|
|
|
type AliyunClient struct {
|
|
template string
|
|
sign string
|
|
core *dysmsapi.Client
|
|
}
|
|
|
|
type AliyunResult struct {
|
|
RequestId string
|
|
Message string
|
|
}
|
|
|
|
func GetAliyunClient(accessId string, accessKey string, sign string, template string) (*AliyunClient, error) {
|
|
region := "cn-hangzhou"
|
|
client, err := dysmsapi.NewClientWithAccessKey(region, accessId, accessKey)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
aliyunClient := &AliyunClient{
|
|
template: template,
|
|
core: client,
|
|
sign: sign,
|
|
}
|
|
|
|
return aliyunClient, nil
|
|
}
|
|
|
|
func (c *AliyunClient) SendMessage(param map[string]string, targetPhoneNumber ...string) error {
|
|
requestParam, err := json.Marshal(param)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if len(targetPhoneNumber) == 0 {
|
|
return fmt.Errorf("missing parameter: targetPhoneNumber")
|
|
}
|
|
|
|
request := dysmsapi.CreateSendSmsRequest()
|
|
request.Scheme = "https"
|
|
request.PhoneNumbers = strings.Join(targetPhoneNumber, ",")
|
|
request.TemplateCode = c.template
|
|
request.TemplateParam = string(requestParam)
|
|
request.SignName = c.sign
|
|
|
|
response, err := c.core.SendSms(request)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if response.Code != "OK" {
|
|
aliyunResult := AliyunResult{}
|
|
err = json.Unmarshal(response.GetHttpContentBytes(), &aliyunResult)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if aliyunResult.Message != "" {
|
|
return errors.New(aliyunResult.Message)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|