alidayu

package module
v0.0.0-...-8667217 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2016 License: MIT Imports: 14 Imported by: 1

README

go-alidayu

Overview

  • 基于最新阿里大鱼API
  • 支持短信验证码、语音双呼、文本转语音通知、语音通知及短信发送记录查询
  • 提供了简单轻量化的并发队列
  • 方便自行封装
  • 支持回调跟踪

Installation

go get -u github.com/WindomZ/go-alidayu

Usage

package main
import . "github.com/WindomZ/go-alidayu"

func init() {
	InitAlidayu(true, "12345678", "88888888888888888888888888888888") // 初始化服务, 配置Key和Secret
}

func main() {
	// 构建短信
	msg := NewMessageSms("身份验证").SetTel("13088888888").
		SetContent(
			"SMS_1234567",
			map[string]string{
				"code":    "1",
				"product": "2",
			},
		)
	// 异步发送短信
    if err := SendMessageQueue(msg, func(msg interface{}, cnt int, err error) {
    }); err != nil {
    	panic(err)
    }
    // Or 同步发送短信
    if err := SendMessageDirect(msg); err != nil {
    	panic(err)
    }
	`...do other things...`
}

Documentation

可以先基于 *_test.go 进行了解 (需要在tests/init.go中配置正确的API调用参数)

完善中...

TODO

  • 简洁方便的文档
  • 语音双呼、文本转语音通知、语音通知的测试用例
  • 提供封装实例方案
  • 更多阿里大鱼反馈支持

LICENSE

MIT(http://opensource.org/licenses/mit-license.php)

Documentation

Index

Constants

View Source
const (
	URL_PRODUCTION string = "https://eco.taobao.com/router/rest"      // 正式环境 (或者: http://gw.api.taobao.com/router/rest)
	URL_SANDBOX    string = "http://gw.api.tbsandbox.com/router/rest" // 沙箱环境 (或者: http://gw.api.tbsandbox.com/router/rest)
)
View Source
const (
	DEFAULT_COURIER_SIZE  int = 1  // 默认线程数
	DEFAULT_CAPACITY_SIZE int = 30 // 默认线程承载数量
)
View Source
const (
	MESSAGE_METHOD_DOUBLE    string = "alibaba.aliqin.fc.voice.num.doublecall" // 多方通话
	MESSAGE_METHOD_TTS              = "alibaba.aliqin.fc.tts.num.singlecall"   // 文本转语音通知
	MESSAGE_METHOD_VOICE            = "alibaba.aliqin.fc.voice.num.singlecall" // 发送语音
	MESSAGE_METHOD_SMS              = "alibaba.aliqin.fc.sms.num.send"         // 短信发送
	MESSAGE_METHOD_SMS_QUERY        = "alibaba.aliqin.fc.sms.num.query"        // 短信发送记录查询
)
View Source
const (
	MESSAGE_FORMAT_JSON string = "json" // 短信JSON参数,推荐
	MESSAGE_FORMAT_XML         = "xml"  // 短信XML参数
)
View Source
const (
	MESSAGE_SIGN_METHOD_HMAC string = "hmac" // 签名方式-HMAC
	MESSAGE_SIGN_METHOD_MD5         = "md5"  // 签名方式-MD5
)

Variables

View Source
var (
	AppURL    string = URL_SANDBOX // alidayu的api地址
	AppKey    string               // alidayu的key
	AppSecret string               // alidayu的secret
)
View Source
var (
	ErrNoAppKey        error = errors.New("goalidayu: Key and Secret must not be null!")
	ErrServiceClosed         = errors.New("goalidayu: Service is closed!")
	ErrServiceOverflow       = errors.New("goalidayu: Service is overflow!")
	ErrPostboxBusy           = errors.New("goalidayu: Postbox is busy")
	ErrCourierBusy           = errors.New("goalidayu: Courier is busy")
)
View Source
var (
	ErrMessageNil        error = errors.New("goalidayu: Message must not be null!")
	ErrMessageMethod           = errors.New("goalidayu: Invalid message method")
	ErrMessageFormat           = errors.New("goalidayu: Invalid message format")
	ErrMessageSignMethod       = errors.New("goalidayu: Invalid message sign method")
)

Functions

func CloseAlidayu

func CloseAlidayu(waitSeconds ...int) error

CloseAlidayu 关闭服务 waitSeconds: 最大的等待时间

func InitAlidayu

func InitAlidayu(prod bool, key, secret string)

InitAlidayu 初始化服务 prod: 是否应用于正式环境 key: alidayu账号分配给应用的AppKey secret: alidayu账号分配给应用的AppSecret

func IsIdle

func IsIdle() bool

IsIdle 服务是否闲置(一般用于测试)

func NewAlidayuResponseError

func NewAlidayuResponseError(response string) error

func SendMessage

func SendMessage(msg IMessage, f ...MessageErrorFunc) error

SendMessage 默认发送信息 基于SetAlidayuQueue的设置,与SendMessageQueue相同

func SendMessageDirect

func SendMessageDirect(msg IMessage) error

SendMessageDirect 同步发送信息 直接发送信息,尝试一次返回结果

func SendMessageQueue

func SendMessageQueue(msg IMessage, f ...MessageErrorFunc) error

SendMessageQueue 异步发送信息 异步发送信息,基于SetAlidayuQueue的设置队列处理

func SetAlidayuQueue

func SetAlidayuQueue(thread_count, queue_capacity int)

SetAlidayuQueue 设置服务队列 thread_count: 同时处理线程数 queue_capacity: 每个线程的承载数量能力

Types

type Courier

type Courier struct {
	// contains filtered or unexported fields
}

func NewCourier

func NewCourier() *Courier

func (*Courier) IsIdle

func (s *Courier) IsIdle() bool

func (*Courier) SendEnvelope

func (s *Courier) SendEnvelope(e *Envelope)

type Envelope

type Envelope struct {
	Message   IMessage
	ErrorFunc MessageErrorFunc
}

func NewEnvelope

func NewEnvelope(msg IMessage, f MessageErrorFunc) *Envelope

func (*Envelope) Error

func (e *Envelope) Error() error

func (*Envelope) FailToSend

func (e *Envelope) FailToSend(err error)

func (*Envelope) Increase

func (e *Envelope) Increase() int

func (*Envelope) SuccessToSend

func (e *Envelope) SuccessToSend()

type IMessage

type IMessage interface {
	Error() error
	Increase() int
}

type Message

type Message struct {
	Method       string `json:"method"`
	AppKey       string `json:"app_key"`
	Session      string `json:"session"`
	Timestamp    string `json:"timestamp"`
	Format       string `json:"format"`
	Version      string `json:"v"`
	PartnerId    string `json:"partner_id"`
	TargetAppKey string `json:"target_app_key"`
	Simplify     bool   `json:"simplify"`
	SignMethod   string `json:"sign_method"`
	Sign         string `json:"sign"`
	Err          error  `json:"-"`
	TryCount     int    `json:"-"`
}

func NewMessage

func NewMessage(method string) *Message

func (*Message) Error

func (m *Message) Error() error

func (*Message) Increase

func (m *Message) Increase() int

type MessageDouble

type MessageDouble struct {
	Message       `json:""`
	CallerTel     string `json:"caller_num"`
	ShowCallerTel string `json:"caller_show_num"`
	CalledTel     string `json:"called_num"`
	ShowCalledTel string `json:"called_show_num"`
}

MessageDouble 多方通话

func NewMessageDouble

func NewMessageDouble() *MessageDouble

NewMessageDouble 创建多方通话

func (*MessageDouble) SetCalledTel

func (s *MessageDouble) SetCalledTel(tel string, show string) *MessageDouble

SetCalledTel 设置多方通话的接收方电话号码

func (*MessageDouble) SetCallerTel

func (s *MessageDouble) SetCallerTel(tel string, show string) *MessageDouble

SetCallerTel 设置多方通话的发起方电话号码

type MessageErrorFunc

type MessageErrorFunc func(interface{}, int, error)

type MessageQuery

type MessageQuery struct {
	Message     `json:""`
	BizId       string `json:"biz_id"`
	Tel         string `json:"rec_num"`
	Date        string `json:"query_date"`
	CurrentPage int64  `json:"current_page"`
	PageSize    int64  `json:"page_size"`
}

MessageQuery 短信发送记录查询

func NewMessageQuery

func NewMessageQuery() *MessageQuery

NewMessageQuery 创建短信发送记录查询

func (*MessageQuery) SetLast

func (s *MessageQuery) SetLast() *MessageQuery

SetLast 设置只获取短信发送记录查询的最后一条发送状态

func (*MessageQuery) SetPage

func (s *MessageQuery) SetPage(current, size int64) *MessageQuery

SetPage 设置短信发送记录查询的分页参数

func (*MessageQuery) SetTel

func (s *MessageQuery) SetTel(tel string) *MessageQuery

SetTel 设置短信发送记录查询的手机号码

type MessageSms

type MessageSms struct {
	Message      `json:""`
	Type         string `json:"sms_type"`
	FreeSignName string `json:"sms_free_sign_name"`
	Param        string `json:"sms_param"`
	Tel          string `json:"rec_num"`
	TemplateCode string `json:"sms_template_code"`
}

MessageSms 短信发送

func NewMessageSms

func NewMessageSms(signName string) *MessageSms

NewMessageSms 创建短信发送

func (*MessageSms) SetContent

func (s *MessageSms) SetContent(code string, param map[string]string) *MessageSms

SetContent 设置短信发送信息模板code及参数param

func (*MessageSms) SetTel

func (s *MessageSms) SetTel(tel ...string) *MessageSms

SetTel 设置短信接收方的电话号码

type MessageTTS

type MessageTTS struct {
	Message       `json:""`
	Param         string `json:"tts_param"`
	CalledTel     string `json:"called_num"`
	ShowCalledTel string `json:"called_show_num"`
	TTSCode       string `json:"tts_code"`
}

MessageTTS 文本转语音通知

func NewMessageTTS

func NewMessageTTS() *MessageTTS

NewMessageTTS 创建文本转语音通知

func (*MessageTTS) SetCalledTel

func (s *MessageTTS) SetCalledTel(tel string, show string) *MessageTTS

SetCalledTel 设置文本转语音通知的接收方电话号码

func (*MessageTTS) SetContent

func (s *MessageTTS) SetContent(code string, param map[string]string) *MessageTTS

SetContent 设置文本转语音通知的内容模板code和参数param

type MessageVoice

type MessageVoice struct {
	Message       `json:""`
	CalledTel     string `json:"called_num"`
	ShowCalledTel string `json:"called_show_num"`
	VoiceCode     string `json:"voice_code"`
}

MessageVoice 发送语音

func NewMessageVoice

func NewMessageVoice() *MessageVoice

NewMessageVoice 创建发送语音

func (*MessageVoice) SetCalledTel

func (s *MessageVoice) SetCalledTel(tel string, show string) *MessageVoice

SetCalledTel 设置发送语音接收方的电话号码

type Postbox

type Postbox struct {
	Couriers []*Courier
	Size     int
	// contains filtered or unexported fields
}

func NewPostbox

func NewPostbox(thread_count, queue_capacity int) *Postbox

func (*Postbox) Close

func (s *Postbox) Close() error

func (*Postbox) IsFull

func (s *Postbox) IsFull() bool

func (*Postbox) IsIdle

func (s *Postbox) IsIdle() bool

func (*Postbox) Start

func (s *Postbox) Start() *Postbox

func (*Postbox) StuffMessage

func (s *Postbox) StuffMessage(msg IMessage, f ...MessageErrorFunc) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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