初始版本,工具基础类
This commit is contained in:
102
examples/sms_example.go
Normal file
102
examples/sms_example.go
Normal file
@@ -0,0 +1,102 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/go-common/config"
|
||||
"github.com/go-common/sms"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// 加载配置
|
||||
cfg, err := config.LoadFromFile("./config/example.json")
|
||||
if err != nil {
|
||||
log.Fatal("Failed to load config:", err)
|
||||
}
|
||||
|
||||
// 创建短信发送器
|
||||
smsConfig := cfg.GetSMS()
|
||||
if smsConfig == nil {
|
||||
log.Fatal("SMS config is nil")
|
||||
}
|
||||
|
||||
smsClient, err := sms.NewSMS(smsConfig)
|
||||
if err != nil {
|
||||
log.Fatal("Failed to create SMS client:", err)
|
||||
}
|
||||
|
||||
// 示例1:发送原始请求(推荐,最灵活)
|
||||
fmt.Println("=== Example 1: Send Raw SMS Request ===")
|
||||
// 外部构建完整的请求参数
|
||||
params := map[string]string{
|
||||
"PhoneNumbers": "13800138000",
|
||||
"SignName": smsConfig.SignName,
|
||||
"TemplateCode": smsConfig.TemplateCode,
|
||||
"TemplateParam": `{"code":"123456","expire":"5"}`,
|
||||
}
|
||||
|
||||
resp, err := smsClient.SendRaw(params)
|
||||
if err != nil {
|
||||
log.Printf("Failed to send raw SMS: %v", err)
|
||||
} else {
|
||||
fmt.Printf("Raw SMS sent successfully, RequestID: %s\n", resp.RequestID)
|
||||
}
|
||||
|
||||
// 示例2:发送简单短信(使用配置中的模板代码)
|
||||
fmt.Println("\n=== Example 2: Send Simple SMS ===")
|
||||
templateParam := map[string]string{
|
||||
"code": "123456",
|
||||
"expire": "5",
|
||||
}
|
||||
|
||||
resp2, err := smsClient.SendSimple(
|
||||
[]string{"13800138000"},
|
||||
templateParam,
|
||||
)
|
||||
if err != nil {
|
||||
log.Printf("Failed to send SMS: %v", err)
|
||||
} else {
|
||||
fmt.Printf("SMS sent successfully, RequestID: %s\n", resp2.RequestID)
|
||||
}
|
||||
|
||||
// 示例3:使用指定模板发送短信
|
||||
fmt.Println("\n=== Example 3: Send SMS with Template ===")
|
||||
templateParam3 := map[string]string{
|
||||
"code": "654321",
|
||||
"expire": "10",
|
||||
}
|
||||
|
||||
resp3, err := smsClient.SendWithTemplate(
|
||||
[]string{"13800138000"},
|
||||
"SMS_123456789", // 使用指定的模板代码
|
||||
templateParam3,
|
||||
)
|
||||
if err != nil {
|
||||
log.Printf("Failed to send SMS: %v", err)
|
||||
} else {
|
||||
fmt.Printf("SMS sent successfully, RequestID: %s\n", resp3.RequestID)
|
||||
}
|
||||
|
||||
// 示例4:使用JSON字符串作为模板参数
|
||||
fmt.Println("\n=== Example 4: Send SMS with JSON String Template Param ===")
|
||||
req := &sms.SendRequest{
|
||||
PhoneNumbers: []string{"13800138000"},
|
||||
TemplateCode: smsConfig.TemplateCode,
|
||||
TemplateParam: `{"code":"888888","expire":"15"}`, // 直接使用JSON字符串
|
||||
}
|
||||
|
||||
resp4, err := smsClient.Send(req)
|
||||
if err != nil {
|
||||
log.Printf("Failed to send SMS: %v", err)
|
||||
} else {
|
||||
fmt.Printf("SMS sent successfully, RequestID: %s\n", resp4.RequestID)
|
||||
}
|
||||
|
||||
fmt.Println("\nNote: Make sure your Aliyun SMS service is configured correctly:")
|
||||
fmt.Println("1. AccessKey ID and Secret are valid")
|
||||
fmt.Println("2. Sign name is approved")
|
||||
fmt.Println("3. Template code is approved")
|
||||
fmt.Println("4. Template parameters match the template definition")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user