//sms.go
// Package sms provides a small wrapper around AWS SNS SMS support.
package common
import (
"fmt"
"github.com/astaxie/beego"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sns"
"github.com/aws/aws-sdk-go/service/sns/snsiface"
)
// Type of SMS delivery mode.
type Type string
const (
// Promotional are non-critical messages, such as marketing messages.
// Amazon SNS optimizes the message delivery to incur the lowest cost.
Promotional Type = "Promotional"
// Transactional messages are critical messages that support
// customer transactions, such as one-time passcodes for multi-factor authentication.
// Amazon SNS optimizes the message delivery to achieve the highest reliability.
Transactional = "Transactional"
)
// Defaults.
var (
DefaultMaxPrice = 0.01
DefaultType = Promotional
)
// SMS configures an SNS SMS client.
type SMS struct {
Service snsiface.SNSAPI // Service implementation
SenderID string // SenderID (optional)
Type Type // Type of SMS delivery mode
MaxPrice float64 // MaxPrice (defaults to $0.01)
}
// Send `message` to `number`.
func (s *SMS) Send(message, number string) error {
attrs := map[string]*sns.MessageAttributeValue{}
if s.SenderID != "" {
attrs["AWS.SNS.SMS.SenderID"] = &sns.MessageAttributeValue{
DataType: aws.String("String"),
StringValue: &s.SenderID,
}
}
maxPrice := s.MaxPrice
if maxPrice == 0 {
maxPrice = DefaultMaxPrice
}
attrs["AWS.SNS.SMS.MaxPrice"] = &sns.MessageAttributeValue{
DataType: aws.String("String"),
StringValue: aws.String(fmt.Sprintf("%0.5f", maxPrice)),
}
kind := s.Type
if kind == "" {
kind = DefaultType
}
attrs["AWS.SNS.SMS.SMSType"] = &sns.MessageAttributeValue{
DataType: aws.String("String"),
StringValue: aws.String(string(kind)),
}
params := &sns.PublishInput{
Message: &message,
PhoneNumber: &number,
MessageAttributes: attrs,
}
_, err := s.Service.Publish(params)
return err
}
// Send `message` to `number` using defaults.
func Send(message, number string) error {
service := sns.New(session.New(&aws.Config{
Region: aws.String("us-east-1"),
Credentials: credentials.NewStaticCredentials(beego.AppConfig.String("AWSAccessKeyId"), beego.AppConfig.String("AWSSecretKey"), ""),
}))
sms := &SMS{
Service: service,
Type: Transactional,
MaxPrice: 0.50,
}
//service := sns.New(session.New(aws.NewConfig()))
//sms := SMS{Service: service}
return sms.Send(message, number)
}
//sms_test.go
package common
import (
"fmt"
"github.com/aws/aws-sdk-go/aws/credentials"
"testing"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sns"
)
func TestSMS_Send(t *testing.T) {
service := sns.New(session.New(aws.NewConfig()), aws.NewConfig().WithRegion("us-east-1").WithCredentials(credentials.NewStaticCredentials("xxxxx", "xxxxx", "")))
//service := sns.New(session.New(), aws.NewConfig().WithRegion("us-west-2"))
sms := &SMS{
Service: service,
Type: Transactional,
MaxPrice: 0.50,
}
err := sms.Send("Hello from SMS.Send()", "+86xxxxx")
if err != nil {
fmt.Printf("sms send fail err:%v", err)
}
}
func TestSend(t *testing.T) {
err := Send("Hello from Send()", "+86xxxxx")
if err != nil {
fmt.Printf("sms send fail err:%v", err)
}
}
亚马逊短信发送
于 2019-04-30 10:03:28 首次发布