-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sms):add alibaba cloud dysmsapi
- Loading branch information
Showing
9 changed files
with
162 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package sms | ||
|
||
import ( | ||
"context" | ||
"log" | ||
|
||
"github.com/404tk/cloudtoolkit/pkg/schema" | ||
"github.com/aliyun/alibaba-cloud-sdk-go/services/dysmsapi" | ||
) | ||
|
||
type SmsProvider struct { | ||
Client *dysmsapi.Client | ||
} | ||
|
||
func (d *SmsProvider) GetResource(ctx context.Context) (schema.Sms, error) { | ||
res := schema.Sms{} | ||
select { | ||
case <-ctx.Done(): | ||
return res, nil | ||
default: | ||
log.Println("[*] List SMS resource ...") | ||
} | ||
var err error | ||
res.Signs, err = listSmsSign(d.Client) | ||
if err != nil { | ||
log.Println("[-] List SMS failed.") | ||
return res, err | ||
} | ||
res.Templates, err = listSmsTemplate(d.Client) | ||
res.DailySize, err = querySendStatistics(d.Client) | ||
|
||
return res, err | ||
} | ||
|
||
var status = map[string]string{ | ||
"AUDIT_STATE_INIT": "审核中", | ||
"AUDIT_STATE_PASS": "审核通过", | ||
"AUDIT_STATE_NOT_PASS": "审核未通过", | ||
"AUDIT_STATE_CANCEL": "取消审核", | ||
} | ||
|
||
func listSmsSign(client *dysmsapi.Client) ([]schema.SmsSign, error) { | ||
signs := []schema.SmsSign{} | ||
request := dysmsapi.CreateQuerySmsSignListRequest() | ||
request.Scheme = "https" | ||
response, err := client.QuerySmsSignList(request) | ||
if err != nil { | ||
return signs, err | ||
} | ||
for _, sign := range response.SmsSignList { | ||
s, _ := status[sign.AuditStatus] | ||
signs = append(signs, schema.SmsSign{ | ||
Name: sign.SignName, | ||
Type: sign.BusinessType, | ||
Status: s, | ||
}) | ||
} | ||
return signs, nil | ||
} | ||
|
||
func listSmsTemplate(client *dysmsapi.Client) ([]schema.SmsTemplate, error) { | ||
temps := []schema.SmsTemplate{} | ||
request := dysmsapi.CreateQuerySmsTemplateListRequest() | ||
request.Scheme = "https" | ||
response, err := client.QuerySmsTemplateList(request) | ||
if err != nil { | ||
return temps, err | ||
} | ||
for _, temp := range response.SmsTemplateList { | ||
s, _ := status[temp.AuditStatus] | ||
temps = append(temps, schema.SmsTemplate{ | ||
Name: temp.TemplateName, | ||
Status: s, | ||
Content: temp.TemplateContent, | ||
}) | ||
} | ||
return temps, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package sms | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"time" | ||
|
||
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests" | ||
"github.com/aliyun/alibaba-cloud-sdk-go/services/dysmsapi" | ||
) | ||
|
||
func querySendStatistics(client *dysmsapi.Client) (int64, error) { | ||
date := time.Now().UTC().Format("20060102") | ||
request := dysmsapi.CreateQuerySendStatisticsRequest() | ||
request.Scheme = "https" | ||
request.IsGlobe = requests.NewInteger(1) | ||
request.StartDate = date | ||
request.EndDate = date | ||
request.PageIndex = requests.NewInteger(1) | ||
request.PageSize = requests.NewInteger(10) | ||
response, err := client.QuerySendStatistics(request) | ||
if err != nil { | ||
return 0, err | ||
} | ||
return response.Data.TotalSize, nil | ||
} | ||
|
||
func querySendDetails(client *dysmsapi.Client, phone string) { | ||
request := dysmsapi.CreateQuerySendDetailsRequest() | ||
request.Scheme = "https" | ||
request.SendDate = time.Now().UTC().Format("20060102") | ||
request.PageSize = requests.NewInteger(10) | ||
request.CurrentPage = requests.NewInteger(1) | ||
request.PhoneNumber = phone | ||
response, err := client.QuerySendDetails(request) | ||
if err != nil { | ||
log.Println(err) | ||
return | ||
} | ||
fmt.Printf("\n%-10s\t%-90s\n", "SendDate", "Content") | ||
fmt.Printf("%-10s\t%-90s\n", "--------", "-------") | ||
for _, detail := range response.SmsSendDetailDTOs.SmsSendDetailDTO { | ||
fmt.Printf("%-10s\t%-90s\n\n", detail.SendDate, detail.Content) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters