mitake

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2019 License: MIT Imports: 13 Imported by: 2

README

go-mitake

GoDoc Build Status Go Report Card codecov

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:  "Message ...",
}

response, err := client.Send(message)

Send multiple SMS:

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

response, err := client.SendBatch(messages)

Send an long SMS:

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

response, err := client.SendLongMessage(message)

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...
})
// The callback URL port number must be standard 80 (HTTP) or 443 (HTTPS).
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
	LongMessageBaseURL *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) CancelMessageStatus

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

CancelMessageStatus cancel the specific messages.

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.

func (*Client) SendLongMessage

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

SendLongMessage sends a long SMS.

func (*Client) SendLongMessageBatch

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

SendLongMessageBatch sends multiple long message SMS.

type Message

type Message struct {
	ID       string `json:"id"`       // Default ID of the message
	Dstaddr  string `json:"dstaddr"`  // Destination phone number
	Destname string `json:"destname"` // Destination receiver name
	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
	ClientID string `json:"clientid"` // Optional, an unique identifier from client to identify SMS message
}

Message represents an SMS object.

func (Message) ToINI

func (m Message) ToINI() string

ToINI returns the INI format string from the message fields.

func (Message) ToLongMessage

func (m Message) ToLongMessage() string

ToLongMessage returns the format string for Long SMS.

type MessageReceipt

type MessageReceipt struct {
	Msgid        string     `json:"msgid"`
	Dstaddr      string     `json:"dstaddr"`
	Dlvtime      string     `json:"dlvtime"`
	Donetime     string     `json:"donetime"`
	Statuscode   string     `json:"statuscode"`
	Statusstring StatusCode `json:"statusstring"`
	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:"-"`
}

MessageResponse represents response of send SMS.

type MessageResult

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

MessageResult represents result of send SMS.

type MessageStatus

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

MessageStatus represents status of message.

type MessageStatusResponse

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

MessageStatusResponse represents response of query message status.

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