mitake

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

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

Go to latest
Published: Feb 24, 2017 License: MIT Imports: 13 Imported by: 0

README

go-mitake

GoDoc Go Report Card Build Status

go-mitake is a Go client library for accessing the Mitake SMS API (Taiwan mobile phone number only).

Installation

go get -u github.com/minchao/go-mitake

Usage

import "github.com/minchao/go-mitake"

Construct a new Mitake SMS client, then use to access the Mitake API. For example:

client := mitake.NewClient("USERNAME", "PASSWORD", nil)

// Retrieving your account balance
balance, err := client.QueryAccountPoint()

Send an SMS:

message := mitake.Message{
    Dstaddr: "0987654321",
    Smbody:  "Test SMS",
}

response, err := client.Send(message)

Send multiple SMS:

messages := []mitake.Message{
    {
        Dstaddr: "0987654321",
        Smbody:  "Test SMS",
    },
    // ...
}

response, err := client.SendBatch(messages)

Query the status of messages:

response, err := client.QueryMessageStatus([]string{"MESSAGE_ID1", "MESSAGE_ID2"})

Use webhook to receive the delivery receipts of the messages:

http.HandleFunc("/callback", func(w http.ResponseWriter, r *http.Request) {
    receipt, err := mitake.ParseMessageReceipt(r)
    if err != nil {
        // Handle error...
        return
    }
    // Process message receipt...
})
if err := http.ListenAndServe(":80", nil); err != nil {
    log.Printf("ListenAndServe error: %v", err)
}

License

See the LICENSE file for license rights and limitations (MIT).

Documentation

Index

Constants

View Source
const (
	StatusServiceError                  = StatusCode("*")
	StatusSMSTemporarilyUnavailable     = StatusCode("a")
	StatusSMSTemporarilyUnavailableB    = StatusCode("b")
	StatusUsernameRequired              = StatusCode("c")
	StatusPasswordRequired              = StatusCode("d")
	StatusUsernameOrPasswordError       = StatusCode("e")
	StatusAccountExpired                = StatusCode("f")
	StatusAccountDisabled               = StatusCode("h")
	StatusInvalidConnectionAddress      = StatusCode("k")
	StatusChangePasswordRequired        = StatusCode("m")
	StatusPasswordExpired               = StatusCode("n")
	StatusPermissionDenied              = StatusCode("p")
	StatusServiceTemporarilyUnavailable = StatusCode("r")
	StatusAccountingFailure             = StatusCode("s")
	StatusSMSExpired                    = StatusCode("t")
	StatusSMSBodyEmpty                  = StatusCode("u")
	StatusInvalidPhoneNumber            = StatusCode("v")
	StatusReservationForDelivery        = StatusCode("0")
	StatusCarrierAccepted               = StatusCode("1")
	StatusCarrierAccepted2              = StatusCode("2")
	StatusCarrierAccepted3              = StatusCode("3")
	StatusDelivered                     = StatusCode("4")
	StatusContentError                  = StatusCode("5")
	StatusPhoneNumberError              = StatusCode("6")
	StatusSMSDisable                    = StatusCode("7")
	StatusDeliveryTimeout               = StatusCode("8")
	StatusReservationCanceled           = StatusCode("9")
)

List of Mitake API status codes

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	BaseURL   *url.URL
	UserAgent string
	// contains filtered or unexported fields
}

A Client manages communication with the Mitake API.

func NewClient

func NewClient(username, password string, httpClient *http.Client) *Client

NewClient returns a new Mitake API client. The username and password are required for authentication. If a nil httpClient is provided, http.DefaultClient will be used.

func (*Client) Do

func (c *Client) Do(req *http.Request) (*http.Response, error)

Do sends an API request, and returns the API response. If the returned error is nil, the Response will contain a non-nil Body which the user is expected to close.

func (*Client) Get

func (c *Client) Get(url string) (*http.Response, error)

Get method make a GET HTTP request.

func (*Client) NewRequest

func (c *Client) NewRequest(method, urlStr string, body io.Reader) (*http.Request, error)

NewRequest creates an API request. A relative URL can be provided in urlStr, in which case it is resolved relative to the BaseURL of the Client. Relative URLs should always be specified without a preceding slash.

func (*Client) Post

func (c *Client) Post(url string, bodyType string, body io.Reader) (*http.Response, error)

Post method make a POST HTTP request.

func (*Client) QueryAccountPoint

func (c *Client) QueryAccountPoint() (int, error)

QueryAccountPoint retrieves your account balance.

func (*Client) QueryMessageStatus

func (c *Client) QueryMessageStatus(messageIds []string) (*MessageStatusResponse, error)

QueryMessageStatus fetch the status of specific messages.

func (*Client) Send

func (c *Client) Send(message Message) (*MessageResponse, error)

Send an SMS.

func (*Client) SendBatch

func (c *Client) SendBatch(messages []Message) (*MessageResponse, error)

SendBatch sends multiple SMS.

type Message

type Message struct {
	Dstaddr  string `json:"dstaddr"`  // Destination phone number
	Smbody   string `json:"smbody"`   // The text of the message you want to send
	Dlvtime  string `json:"dlvtime"`  // Optional, Delivery time
	Vldtime  string `json:"vldtime"`  // Optional
	Response string `json:"response"` // Optional, Callback URL to receive the delivery receipt of the message
}

Message represents an SMS object.

func (Message) ToINI

func (m Message) ToINI() string

ToINI returns the INI format string from the message fields.

type MessageReceipt

type MessageReceipt struct {
	Msgid      string     `json:"msgid"`
	Dstaddr    string     `json:"dstaddr"`
	Dlvtime    string     `json:"dlvtime"`
	Donetime   string     `json:"donetime"`
	Statuscode StatusCode `json:"statuscode"`
	Statusstr  string     `json:"statusstr"`
	StatusFlag string     `json:"StatusFlag"`
}

MessageReceipt represents a message delivery receipt.

func ParseMessageReceipt

func ParseMessageReceipt(r *http.Request) (*MessageReceipt, error)

ParseMessageReceipt parse an incoming Mitake callback request and return the MessageReceipt.

Example usage:

func Callback(w http.ResponseWriter, r *http.Request) {
	receipt, err := mitake.ParseMessageReceipt(r)
	if err != nil { ... }
	// Process message receipt
}

type MessageResponse

type MessageResponse struct {
	Results      []*MessageResult
	AccountPoint int
	INI          string `json:"-"`
}

type MessageResult

type MessageResult struct {
	Msgid      string     `json:"msgid"`
	Statuscode StatusCode `json:"statuscode"`
}

type MessageStatus

type MessageStatus struct {
	MessageResult
	StatusTime string `json:"statustime"`
}

type MessageStatusResponse

type MessageStatusResponse struct {
	Statuses []*MessageStatus
	INI      string `json:"-"`
}

type StatusCode

type StatusCode string

StatusCode of Mitake API.

func (StatusCode) String

func (c StatusCode) String() string

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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