当前位置:网站首页>go基于腾讯云实现发送短信
go基于腾讯云实现发送短信
2022-06-12 11:08:00 【.番茄炒蛋】
简介
代码
package main
import (
"encoding/json"
"fmt"
"math/rand"
"strings"
"time"
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors"
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile"
sms "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/sms/v20210111" // 引入sms
)
func main() {
// 实例化一个认证对象,入参需要传入腾讯云账户密钥对secretId,secretKey.
credential := common.NewCredential(
"你的accessKeyId",
"你的accessKeySecret",
)
cpf := profile.NewClientProfile()
cpf.HttpProfile.ReqMethod = "POST"
cpf.HttpProfile.Endpoint = "sms.tencentcloudapi.com"
client, _ := sms.NewClient(credential, "ap-beijing", cpf)
/* 实例化一个请求对象,根据调用的接口和实际情况*/
request := sms.NewSendSmsRequest()
// 应用 ID 可前往 [短信控制台](https://console.cloud.tencent.com/smsv2/app-manage) 查看
request.SmsSdkAppId = common.StringPtr("")
// 短信签名内容: 使用 UTF-8 编码,必须填写已审核通过的签名
request.SignName = common.StringPtr("")
/* 模板 ID: 必须填写已审核通过的模板 ID */
request.TemplateId = common.StringPtr("")
/* 模板参数: 模板参数的个数需要与 TemplateId 对应模板的变量个数保持一致,若无模板参数,则设置为空*/
request.TemplateParamSet = common.StringPtrs([]string{
GenerateSmsCode(6)})
/* 下发手机号码,采用 E.164 标准,+[国家或地区码][手机号] * 示例如:+8613711112222, 其中前面有一个+号 ,86为国家码,13711112222为手机号,最多不要超过200个手机号*/
request.PhoneNumberSet = common.StringPtrs([]string{
"+86187********"})
// 通过client对象调用想要访问的接口,需要传入请求对象
response, err := client.SendSms(request)
// 处理异常
if _, ok := err.(*errors.TencentCloudSDKError); ok {
fmt.Printf("An API error has returned: %s", err)
return
}
// 非SDK异常,直接失败。实际代码中可以加入其他的处理。
if err != nil {
panic(err)
}
b, _ := json.Marshal(response.Response)
// 打印返回的json字符串
fmt.Printf("%s", b)
}
// 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()
}
返回的response:
{
"SendStatusSet": [
{
"SerialNo": "2433:124789050616550020834470867",
"PhoneNumber": "+86187********",
"Fee": 1,
"SessionContext": "",
"Code": "Ok",
"Message": "send success",
"IsoCode": "CN"
}
],
"RequestId": "ee59157c-b11d-444d-a17c-4aa070f21414"
}
边栏推荐
- Index query efficiency of MySQL
- k58.第一章 基于kubeadm安装kubernetes v1.23 -- 集群部署
- Flex layout
- selenium使用代理IP
- Leetcode2154. 将找到的值乘以 2(二分查找)
- redis 總結
- MATLAB中stairs函数使用
- Common configuration commands for Cisco network device security management
- Malicious code analysis practice - use IDA pro to analyze lab05-01 dll
- Common port description
猜你喜欢

Index query efficiency of MySQL

The reason why scanf return value is ignored and its solution

程序分析与优化 - 6 循环优化

深度学习与CV教程(14) | 图像分割 (FCN,SegNet,U-Net,PSPNet,DeepLab,RefineNet)

Leetcode2154. 将找到的值乘以 2(二分查找)

AcWing 128. Editor (to effectively modify the specified position in the top stack)

How to upload the video on the computer to the mobile phone without network

k58.第一章 基于kubeadm安装kubernetes v1.23 -- 集群部署

k52.第一章 基于kubeadm安装kubernetes v1.22 -- 集群部署

A simple understanding of b+ tree
随机推荐
淺談調和形狀上下文特征HSC對3DSC的改進
k59.第二章 基于二进制包安装kubernetes v1.23 --集群部署
The solution of Lenovo notebook ThinkPad t440 WiFi unable to connect to the Internet
NFT数字藏品的可验证性和稀缺性
浅谈三维形状上下文特征3DSC理论及应用
Stream as a return value in WCF - who disposes of it- Stream as a return value in WCF - who disposes it?
The difference between meta universe chain games and traditional games
Fiddler automatically saves the result of the specified request to a file
Zabbix 监控之LLD
疫情居家办公体验 | 社区征文
Several solutions across domains
XML Parsing Error: mismatched tag. Expected
自然语言处理nlp 数据集下载地址
Malicious code analysis practice - lab03-02 DLL analysis
Malicious code analysis practice - lab03-01 Exe basic dynamic analysis
On 3dsc theory and application of 3D shape context feature
JS obtains the time period of this week and last week (one time period is from Monday to Sunday)
2022-06-11:注意本文件中,graph不是邻接矩阵的含义,而是一个二部图。 在长度为N的邻接矩阵matrix中,所有的点有N个,matrix[i][j]
Create vite source code analysis
AcWing 1921. 重新排列奶牛(环图)