1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| package main
import ( "encoding/json" "fmt" "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common" "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile" sms "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/sms/v20210111" "math/rand" "strings" "time" )
func SendPhoneMessage(phoneNumber string) {
secretID := "" secretKey := "" smsSdkAppID := "" signName := "" templateID := ""
credential := common.NewCredential( secretID, secretKey, ) cpf := profile.NewClientProfile() cpf.HttpProfile.ReqMethod = "POST" cpf.HttpProfile.Endpoint = "sms.tencentcloudapi.com" client, _ := sms.NewClient(credential, "ap-beijing", cpf) request := sms.NewSendSmsRequest() request.SmsSdkAppId = common.StringPtr(smsSdkAppID) request.SignName = common.StringPtr(signName) request.TemplateId = common.StringPtr(templateID) request.TemplateParamSet = common.StringPtrs([]string{generateCode(6)}) request.PhoneNumberSet = common.StringPtrs([]string{phoneNumber})
responseObject, _ := client.SendSms(request) responseCharArray, _ := json.Marshal(responseObject.Response) responseString := string(responseCharArray) fmt.Println(responseString) }
func generateCode(length int) string { num := [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", num[rand.Intn(len(num))]) } return sb.String() }
|