亚马逊短信发送

//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)
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值