aliyun

package
v0.0.0-...-7becda7 Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2017 License: Apache-2.0 Imports: 14 Imported by: 0

README

开发阿里云短信服务注意事项

名词解释

以下几个变量名称在调用 SendOneSendMulti 函数时使用)

recnum: 用户接收的手机号

signname: 签名名称

templatecode: 模板CODE

paramstring: 参数字符串

准备工作

直达帮助页面

  1. 新建短信签名

    用户需要先新建短信签名, 阿里云审核通过后会得到一个签名名称, 此签名名称即为signname

  2. 新建模板

    • 用户需要先新建模板, 阿里云审核通过后会得到一个模板CODE, 此模板CODE即为templatecode

    • 用户在创建模板的时候,会在模板中添加变量(一个或多个),如下为一个例子:

        尊敬的用户,您的${device_id}(${devicename})设备已离线
      

      上面的device_iddevicename既是模板变量,用户在使用此SDK时,需要把这两个变量转换为参数字符串paramstring,此paramstring是一个json对象,如下所示:

        {"device_id":"T0000001","devicename":"测试设备"}
      

      以上字符串即为 paramstring.

      下面提供一个golang方式转换方法:

        A. 为每一个模板CODE建立一个模板参数结构体,实现一个String()方法
      
        	type Alarm_Offline_SMS_22120102 struct {
        		DeviceId   string `json:"device_id"`
        		DeviceName string `json:"devicename"`
        	}
      
        	func (this Alarm_Offline_SMS_22120102) String() string {
        		body, err := json.Marshal(this)
        		if err != nil {
        			return ""
        		}
        		return string(body)
        	}
      
        B. 在设置paramstring参数时,直接使用String()方法产生:
      
        	o := new(Alarm_Offline_SMS_22120102)
        	o.DeviceId = "T0000001"
        	o.DeviceName = "测试设备"
        	paramstring := o.String()
      
        	// 这里paramstring将会是符合要求的结果:{"device_id":"T0000001","devicename":"测试设备"}
        	fmt.Println(paramstring)
      
        C. 例子sample.go,可以改写成如下形式:
      
        	package main
      
        	import (
        		"encoding/json"
        		"fmt"
        		"os"
      
        		"github.com/GiterLab/aliyun-sms-go-sdk/sms"
        	)
      
        	// modify it to yours
        	const (
        		ENDPOINT  = "https://sms.aliyuncs.com/"
        		ACCESSID  = "your_accessid"
        		ACCESSKEY = "your_accesskey"
        	)
      
        	type Register_SMS_48825019 struct {
        		CompanyName string `json:"company"`
        	}
      
        	func (this *Register_SMS_48825019) String() string {
        		body, err := json.Marshal(this)
        		if err != nil {
        			return ""
        		}
        		return string(body)
        	}
      
        	func main() {
        		sms.HttpDebugEnable = true
        		c := sms.New(ACCESSID, ACCESSKEY)
      
        		// create a paramstring object
        		o := new(Register_SMS_48825019)
        		o.CompanyName = "duoxieyun"
      
        		// send to one person
        		e, err := c.SendOne("1375821****", "谢先斌", "SMS_48825019", o.String())
        		if err != nil {
        			fmt.Println("send sms failed", err, e.Error())
        			os.Exit(0)
        		}
        		// send to more than one person
        		e, err = c.SendMulti([]string{"1375821****", "1835718****"}, "谢先斌", "SMS_48825019", o.String())
        		if err != nil {
        			fmt.Println("send sms failed", err, e.Error())
        			os.Exit(0)
        		}
        		fmt.Println("send sms succeed", e.GetRequestId())
        	}
      

Documentation

Index

Constants

This section is empty.

Variables

View Source
var HttpDebugEnable bool = false

Functions

This section is empty.

Types

type ErrorMessage

type ErrorMessage struct {
	HttpCode  int     `json"-"`
	Model     *string `json:"Model,omitempty"`
	RequestId *string `json:"RequestId,omitempty"`
	Message   *string `json:"Message,omitempty"`
	Code      *string `json:"Code,omitempty"`
}

短信服务器返回的错误信息

func (*ErrorMessage) Error

func (e *ErrorMessage) Error() string

func (*ErrorMessage) GetCode

func (e *ErrorMessage) GetCode() string

func (*ErrorMessage) GetHttpCode

func (e *ErrorMessage) GetHttpCode() int

func (*ErrorMessage) GetMessage

func (e *ErrorMessage) GetMessage() string

func (*ErrorMessage) GetModel

func (e *ErrorMessage) GetModel() string

func (*ErrorMessage) GetRequestId

func (e *ErrorMessage) GetRequestId() string

func (*ErrorMessage) SetHttpCode

func (e *ErrorMessage) SetHttpCode(code int)

type SMSClient

type SMSClient struct {
	// SMS服务的地址,默认为(https://sms.aliyuncs.com
	EndPoint string
	// 访问SMS服务的accessid,通过官方网站申请或通过管理员获取
	AccessId string
	// 访问SMS服务的accesskey,通过官方网站申请或通过管理员获取
	AccessKey string
	// 连接池中每个连接的Socket超时,单位为秒,可以为int或float。默认值为50
	SocketTimeout int

	// 其他参数
	Param SmsParam
	// contains filtered or unexported fields
}

func New

func New(accessid, accesskey string) (c *SMSClient)

创建一个短信发送客户端

func (*SMSClient) SendMulti

func (c *SMSClient) SendMulti(recnum []string, signname, templatecode, paramstring string) (e *ErrorMessage, err error)

发送给多个手机号, 最多100个

func (*SMSClient) SendOne

func (c *SMSClient) SendOne(recnum, signname, templatecode, paramstring string) (e *ErrorMessage, err error)

发送给一个手机号

func (*SMSClient) SetAccessId

func (c *SMSClient) SetAccessId(accessid string)

设置短信服务的accessid,通过官方网站申请或通过管理员获取

func (*SMSClient) SetAccessKey

func (c *SMSClient) SetAccessKey(accesskey string)

设置短信服务的accesskey,通过官方网站申请或通过管理员获取

func (*SMSClient) SetEndPoint

func (c *SMSClient) SetEndPoint(end_point string)

设置短信服务器

func (*SMSClient) SetSocketTimeout

func (c *SMSClient) SetSocketTimeout(sockettimeout int)

设置短信服务的Socket超时,单位为秒,可以为int或float。默认值为50

type SmsParam

type SmsParam struct {
	Action           string
	SignName         string
	TemplateCode     string
	RecNum           string
	ParamString      string
	Format           string
	Version          string
	AccessKeyId      string
	SignatureMethod  string
	Timestamp        string
	SignatureVersion string
	SignatureNonce   string
	RegionId         string
}

func (*SmsParam) GetAccessKeyId

func (this *SmsParam) GetAccessKeyId() string

func (*SmsParam) GetAction

func (this *SmsParam) GetAction() string

func (*SmsParam) GetFormat

func (this *SmsParam) GetFormat() string

func (*SmsParam) GetParamString

func (this *SmsParam) GetParamString() string

func (*SmsParam) GetRecNum

func (this *SmsParam) GetRecNum() string

func (*SmsParam) GetRegionId

func (this *SmsParam) GetRegionId() string

func (*SmsParam) GetSignName

func (this *SmsParam) GetSignName() string

func (*SmsParam) GetSignatureMethod

func (this *SmsParam) GetSignatureMethod() string

func (*SmsParam) GetSignatureNonce

func (this *SmsParam) GetSignatureNonce() string

func (*SmsParam) GetSignatureVersion

func (this *SmsParam) GetSignatureVersion() string

func (*SmsParam) GetTemplateCode

func (this *SmsParam) GetTemplateCode() string

func (*SmsParam) GetTimestamp

func (this *SmsParam) GetTimestamp() string

func (*SmsParam) GetVersion

func (this *SmsParam) GetVersion() string

func (*SmsParam) SetAccessKeyId

func (this *SmsParam) SetAccessKeyId(accesskeyid string)

func (*SmsParam) SetAction

func (this *SmsParam) SetAction(action string)

func (*SmsParam) SetFormat

func (this *SmsParam) SetFormat(format string)

func (*SmsParam) SetParamString

func (this *SmsParam) SetParamString(paramstring string)

func (*SmsParam) SetRecNum

func (this *SmsParam) SetRecNum(recnum string)

func (*SmsParam) SetRegionId

func (this *SmsParam) SetRegionId(regionid string)

func (*SmsParam) SetSignName

func (this *SmsParam) SetSignName(signname string)

func (*SmsParam) SetSignatureMethod

func (this *SmsParam) SetSignatureMethod(signaturemethod string)

func (*SmsParam) SetSignatureNonce

func (this *SmsParam) SetSignatureNonce(signaturenonce string)

func (*SmsParam) SetSignatureVersion

func (this *SmsParam) SetSignatureVersion(signatureversion string)

func (*SmsParam) SetTemplateCode

func (this *SmsParam) SetTemplateCode(templatecode string)

func (*SmsParam) SetTimestamp

func (this *SmsParam) SetTimestamp(timestamp string)

func (*SmsParam) SetVersion

func (this *SmsParam) SetVersion(version string)

Jump to

Keyboard shortcuts

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