当前位置:网站首页>go基于阿里云实现发送短信
go基于阿里云实现发送短信
2022-06-12 11:08:00 【.番茄炒蛋】
简介
代码
package main
import (
"fmt"
"github.com/alibabacloud-go/darabonba-openapi/client"
dysmsapi20170525 "github.com/alibabacloud-go/dysmsapi-20170525/v2/client"
"math/rand"
"strings"
"time"
)
func main() {
accessKeyId := "你的accessKeyId"
accessKeySecret := "你的accessKeySecret "
endpoint := "dysmsapi.aliyuncs.com"
c := &client.Config{
AccessKeyId: &accessKeyId, AccessKeySecret: &accessKeySecret, Endpoint: &endpoint}
newClient, err := dysmsapi20170525.NewClient(c)
if err != nil {
panic(err)
}
phoneNumber := "要发送的手机号"
templateCode := "SMS_154950909"
signName := "阿里云短信测试"
code := "{\"code\":" + GenerateSmsCode(6) + "}"
request := &dysmsapi20170525.SendSmsRequest{
PhoneNumbers: &phoneNumber,
TemplateCode: &templateCode,
SignName: &signName,
TemplateParam: &code,
}
sms, err := newClient.SendSms(request)
if err != nil {
panic(err)
}
fmt.Println(sms)
}
// GenerateSmsCode 生成验证码;length代表验证码的长度
func GenerateSmsCode(length int) string {
numberic := [10]byte{
0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
rand.Seed(time.Now().Unix())
var sb strings.Builder
for i := 0; i < length; i++ {
fmt.Fprintf(&sb, "%d", numberic[rand.Intn(len(numberic))])
}
return sb.String()
}
返回的sms
{
"headers": {
"access-control-allow-headers": "X-Requested-With, X-Sequence, _aop_secret, _aop_signature, x-acs-action, x-acs-version, x-acs-date, Content-Type",
"access-control-allow-methods": "POST, GET, OPTIONS, PUT, DELETE",
"access-control-allow-origin": "*",
"access-control-max-age": "172800",
"connection": "keep-alive",
"content-length": "110",
"content-type": "application/json;charset=utf-8",
"date": "Sun, 12 Jun 2022 02:15:49 GMT",
"x-acs-request-id": "6BC0AE17-B50A-5B0F-B5BF-10AEDC602909",
"x-acs-trace-id": "fc4f2bf6ec8a9f9541103cfa0395c47a"
},
"body": {
"BizId": "361607455000149340^0",
"Code": "OK",
"Message": "OK",
"RequestId": "6BC0AE17-B50A-5B0F-B5BF-10AEDC602909"
}
}
边栏推荐
- XML Parsing Error: mismatched tag. Expected
- Malicious code analysis practice - lab03-02 DLL analysis
- AcWing 41. Stack containing min function (monotone stack)
- Module 8 job
- ^34作用域面试题
- SharDingJDBC-5.1.0按月水平分表+读写分离,自动创表、自动刷新节点表
- AcWing 1921. 重新排列奶牛(环图)
- 【CF1392D】D. Omkar and Bed Wars(环形与后效性dp)
- Fiddler automatically saves the result of the specified request to a file
- 2021-03-24
猜你喜欢

Amélioration de la 3dsc par HSC

The solution of Lenovo notebook ThinkPad t440 WiFi dropping all the time

AcWing 132. 小组队列(队列模拟题)

Leetcdoe 2037. 使每位学生都有座位的最少移动次数(可以,一次过)

DS18B20数字温度计 (一) 电气特性, 寄生供电模式和远距离接线

SOT23(Small Outline Transistor)

M-arch (fanwai 13) gd32l233 evaluation - some music

M-Arch(番外12)GD32L233评测-CAU加解密(捉弄下小编)

On 3dsc theory and application of 3D shape context feature

Bug easily ignored by recursion
随机推荐
^34作用域面试题
Common tools download address
Several methods of importing ThinkPHP
Failed to load resource: the server responded with a status of 413 (Request Entity Too Large)
Why check the @nonnull annotation at run time- Why @Nonnull annotation checked at runtime?
AcWing 131. The largest rectangle in the histogram (monotone stack classic application template)
Several solutions across domains
NLP data set download address for naturallanguageprocessing
AcWing 1921. 重新排列奶牛(环图)
InfoQ geek media's 15th anniversary solicitation |position:fixed virtual button cannot take effect after being triggered. Problem analysis and Solution Exploration
k58.第一章 基于kubeadm安装kubernetes v1.23 -- 集群部署
Malicious code analysis practice - use IDA pro to analyze lab05-01 dll
JS open common application market
AcWing 128. 编辑器(对顶栈 实现序列内部指定位置高效修改)
模块8作业
对网络库协程的思考——读brpc有感
DS18B20数字温度计 (一) 电气特性, 供电和接线方式
scanf返回值被忽略的原因及其解决方法
redis 總結
AcWing 41. 包含min函数的栈(单调栈)