GO项目调用阿里云短信发送验证码并查询示例
即拿即用,替换阿里云值
调用SendSms,传入手机号 15555555555,16666666666 验证码 54321
//ACCESS_KEY_ID 阿里云控制台获取
//ACCESS_KEY_SECRET 阿里云控制台获取
//signName := "短信签名名称,示例值阿里云"
//templateCode := "短信模板Code,示例值SMS_15305****"
package main
import (
"os"
dysmsapi "github.com/alibabacloud-go/dysmsapi-20170525/v2/client"
openapi "github.com/alibabacloud-go/darabonba-openapi/client"
console "github.com/alibabacloud-go/tea-console/client"
env "github.com/alibabacloud-go/darabonba-env/client"
util "github.com/alibabacloud-go/tea-utils/service"
time "github.com/alibabacloud-go/darabonba-time/client"
string_ "github.com/alibabacloud-go/darabonba-string/client"
"github.com/alibabacloud-go/tea/tea"
)
// 使用AK&SK初始化账号Client
func CreateClient (accessKeyId *string, accessKeySecret *string) (_result *dysmsapi.Client, _err error) {
config := &openapi.Config{}
config.AccessKeyId = accessKeyId
config.AccessKeySecret = accessKeySecret
_result = &dysmsapi.Client{}
_result, _err = dysmsapi.NewClient(config)
return _result, _err
}
//手机号 15555555555 验证码 54321
//ACCESS_KEY_ID 阿里云控制台获取
//ACCESS_KEY_SECRET 阿里云控制台获取
func SendSms(phoneNumbers, codeParam string) (_err error) {
client, _err := CreateClient(tea.String("ACCESS_KEY_ID"), tea.String("ACCESS_KEY_SECRET"))
if _err != nil {
return _err
}
// 1.发送短信
signName := "短信签名名称,示例值阿里云"
templateCode := "短信模板Code,示例值SMS_15305****"
// 模板参数
templateParam := "{\"code\":\"" + codeParam + "\"}"
sendReq := &dysmsapi.SendSmsRequest{
PhoneNumbers: &phoneNumbers,
SignName: &signName,
TemplateCode: &templateCode,
TemplateParam: &templateParam,
}
sendResp, _err := client.SendSms(sendReq)
if _err != nil {
return _err
}
code := sendResp.Body.Code
if !tea.BoolValue(util.EqualString(code, tea.String("OK"))) {
console.Log(tea.String("错误信息: " + tea.StringValue(sendResp.Body.Message)))
return _err
}
bizId := sendResp.Body.BizId
// 2. 等待 10 秒后查询结果
_err = util.Sleep(tea.Int(10000))
if _err != nil {
return _err
}
// 3.查询结果
phoneNums := string_.Split(&phoneNumbers, tea.String(","), tea.Int(-1))
for _, phoneNum := range phoneNums {
queryReq := &dysmsapi.QuerySendDetailsRequest{
PhoneNumber: util.AssertAsString(phoneNum),
BizId: bizId,
SendDate: time.Format(tea.String("yyyyMMdd")),
PageSize: tea.Int64(10),
CurrentPage: tea.Int64(1),
}
queryResp, _err := client.QuerySendDetails(queryReq)
if _err != nil {
return _err
}
dtos := queryResp.Body.SmsSendDetailDTOs.SmsSendDetailDTO
// 打印结果
for _, dto := range dtos {
phone, _ := MaskPhoneNumber(*dto.PhoneNum)
if tea.BoolValue(util.EqualString(tea.String(tea.ToString(tea.Int64Value(dto.SendStatus))), tea.String("3"))) {
console.Log(tea.String(tea.StringValue(&phone) + " 发送成功,接收时间: " + tea.StringValue(dto.ReceiveDate)))
} else if tea.BoolValue(util.EqualString(tea.String(tea.ToString(tea.Int64Value(dto.SendStatus))), tea.String("2"))) {
console.Log(tea.String(tea.StringValue(&phone) + " 发送失败"))
} else {
console.Log(tea.String(tea.StringValue(&phone) + " 正在发送中..."))
}
}
}
return _err
}
// MaskPhoneNumber 用于替换手机号中间四位为 ****
func MaskPhoneNumber(phoneNumber string) (string, error) {
// 使用正则表达式检查手机号格式是否正确
// 假设手机号为11位数字
matched, err := regexp.MatchString(`^\d{11}$`, phoneNumber)
if err != nil {
return "", err
}
if !matched {
return "", fmt.Errorf("invalid phone number format")
}
// 替换中间四位为 ****
maskedPhoneNumber := phoneNumber[:3] + "****" + phoneNumber[7:]
return maskedPhoneNumber, nil
}