gosms

package module
v1.0.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 8, 2023 License: Apache-2.0 Imports: 14 Imported by: 0

README

readme

由于没有注意到ali官方发布到新版的golang sdk for SMS service 快实现完成完成了才发现, 索性把它实现完成,只是没有补充足够的测试. 相比官方sdk, 个人认为实现要比官方版简洁,调用优雅, 可扩展性强于官方版.

description: golang version short message sender underline with ali sms service

Prerequisite: install golang 1.19 or later

Installation

Use go get to install SDK

go get -u github.com/guoapeng/gosms

Quick Examples

Before you begin, you need to sign up for an Alibaba Cloud account and retrieve your Credentials.

Create sender and send short message
package main

import ("github.com/guoapeng/gosms"
  "log"
)


func main() {

    //replace the accessKeyId, AccessSecret and SignName only you know
    sender := gosms.NewSender("your_accessKeyId", "your_AccessSecret", "your_SignName")
    
    msgJSON := `{code:"1234"}`           
    // replace 13288888888 with valid phone number recognizable by aliyun SMS service
    // replace SMS_9999999 with message template id defined by you in aliyun's sms service
    if response, err := sender.Send("13288888888", msgJSON, "SMS_9999999"); err ==nil {  
        if "OK" == response.Code {
            log.Println("sent message successfully and get response ",response)
        } else {
            log.Println("sent message with issue ",response)
        }
    } else {
        log.Println("failed to send message with error", err)
    }
}

Documentation

Index

Constants

View Source
const (
	DELIMITER  = "&"
	SignPrefix = "GET&%2F&"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type AcsClient

type AcsClient struct {
	TargetServer     SmsServer
	SignatureBuilder SignatureBuilder
	HttpClient       *http.Client
}

func (*AcsClient) GetResponse

func (p *AcsClient) GetResponse(queryString *QueryString, body RequestBody) (*Response, error)

type Action

type Action struct {
	Action     string
	ApiVersion string
	RegionId   string
	Msg        Message
}

type AliSmsSender

type AliSmsSender struct {
	Client           AcsClient
	Profile          Profile
	SignNameProvider SignNameProvider
}

func (*AliSmsSender) Send

func (p *AliSmsSender) Send(phone, msgJSON, templateCode string) (*Response, error)

type Credential

type Credential struct {
	AccessKeyId  string
	AccessSecret string
}

type Message

type Message struct {
	PhoneNumbers  string
	TemplateParam string
	TemplateCode  string
	SignName      string
	OutId         string ////可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者
}

type Profile

type Profile struct {
	SignatureMethod  string
	SignatureVersion string
	Credential       Credential
}

type QueryString

type QueryString struct {
	MethodType       string
	AccessKeyId      string
	Action           string
	Format           string //Optional 没传默认为JSON,可选填值:XML
	OutId            string //外部流水扩展字段
	PhoneNumbers     string
	SignName         string
	RegionId         string
	SignatureMethod  string
	SignatureNonce   string //用于请求的防重放攻击,每次请求唯一
	SignatureVersion string
	TemplateCode     string
	TemplateParam    string
	Timestamp        string
	Version          string
}

func NewRequest

func NewRequest(profile Profile, signatureNonce string, action Action) *QueryString

func (*QueryString) String

func (p *QueryString) String() string

type RequestBody

type RequestBody struct {
	Reader *strings.Reader
}

type Response

type Response struct {
	Message string `json:"Message"`
	Code    string `json:"Code"`
}

SimpleMessage ...

type Sender

type Sender interface {
	Send(phone, msgJSON, templateCode string) (*Response, error)
}

func NewSender

func NewSender(accessKeyId, accessSecret, signName string) Sender

type SignNameProvider

type SignNameProvider struct {
	SignName string
}

type SignatureBuilder

type SignatureBuilder struct {
	Credential Credential
}

func (*SignatureBuilder) BuildSignature

func (p *SignatureBuilder) BuildSignature(signString string) string

type SmsServer

type SmsServer interface {
	GetUrl() (string, error)
}

func NewServer

func NewServer(protocal, host, port string) SmsServer

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL