ippanel

package module
v2.0.2 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2022 License: BSD-3-Clause Imports: 10 Imported by: 0

README

IPPANEL SMS api SDK

Build Status GoDoc

Installation

If you are using go modules, just install it with go mod install or go build ., Otherwise you can use go get ./...

go get github.com/ippanel/go-rest-sdk/v2

Examples

After installing ippanel sdk with above methods, you can import it in your project like this:

import "github.com/ippanel/go-rest-sdk/v2"

For using sdk, after importing, you have to create a client instance that gives you available methods on API

// you api key that generated from panel
apiKey := "api-key"

// create client instance
sms := ippanel.New(apiKey)

...
Credit check
// return float64 type credit amount
credit, err := sms.GetCredit()
if err != nil {
    t.Error("error occurred ", err)
}
Send one to many

For sending sms, obviously you need originator number, recipients and message.

MessageId, err := sms.Send("+9810001", []string{"98912xxxxxxx"}, "ippanel is awesome")
if err != nil {
    t.Error("error occurred ", err)
}

If send is successful, a unique tracking code returned and you can track your message status with that.

Get message summery
MessageId := "message-tracking-code"

message, err := sms.GetMessage(MessageId)
if err != nil {
    t.Error("error occurred ", err)
}

fmt.Println(message.Status) // get message status
fmt.Println(message.Cost)   // get message cost
fmt.Println(message.Payack) // get message payback
Get message delivery statuses
MessageId := "message-tracking-code"
// pagination params for defining fetch size and offset
paginationParams := ippanel.ListParams{Page: 0, Limit: 10}

statuses, paginationInfo, err := sms.FetchStatuses(MessageId, paginationParams)
if err != nil {
    t.Error("error occurred ", err)
}

// you can loop in messages statuses list
for _, status := range statuses {
    fmt.Printf("Recipient: %s, Status: %s", status.Recipient, status.Status)
}

fmt.Println("Total: ", paginationInfo.Total)
Inbox fetch

fetch inbox messages

paginationParams := ippanel.ListParams{Page: 0, Limit: 10}

messages, paginationInfo, err := sms.FetchInbox(paginationParams)
if err != nil {
    t.Error("error occurred ", err)
}
Pattern create

For sending messages with predefined pattern(e.g. verification codes, ...), you hav to create a pattern. a pattern at least have a parameter. parameters defined with %param_name%.

pattern, err := sms.CreatePattern("%name% is awesome", false)
if err != nil {
    t.Error("error occurred ", err)
}
Send with pattern
patternValues := map[string]string{
    "name": "IPPANEL",
}

MessageId, err := sms.SendPattern(
    "t2cfmnyo0c",   // pattern code
    "+9810001",     // originator
    "98912xxxxxxx", // recipient
    patternValues,  // pattern values
)
if err != nil {
    t.Error("error occurred ", err)
}
Error checking
_, err := sms.Send("9810001", []string{"98912xxxxx"}, "ippanel is awesome")
if err != nil {
    // check that error is type of ippanel standard error
    if e, ok := err.(Error); ok {
        // after casting, you have access to error code and error message
        switch e.Code {
        // its special type of error, occurred when POST form data validation failed
        case ErrUnprocessableEntity:
            // for accessing field level errors you have to cast it to FieldErrors type
            fieldErrors := e.Message.(FieldErrs)
            for field, fieldError := range fieldErrors {
                t.Log(field, fieldError)
            }
        // in other error types, e.Message is string
        default:
            errMsg := e.Message.(string)
            t.Log(errMsg)
        }
    }
}

Documentation

Overview

Package ippanel is an official library for working with ippanel sms api. brief documentation for ippanel sms api provided at http://docs.ippanel.com

Index

Constants

View Source
const (
	// ClientVersion is used in User-Agent request header to provide server with API level.
	ClientVersion = "2.0.0"

	// Endpoint points you to Ippanel REST API.
	Endpoint = "https://api2.ippanel.com/api/v1"
)

Variables

View Source
var (
	ErrUnexpectedResponse = errors.New("The Ippanel API is currently unavailable")
	ErrStatusUnauthorized = errors.New("You api key is not valid")
)

Functions

func ParseErrors

func ParseErrors(res *BaseResponse) error

ParseErrors ...

Types

type BaseResponse

type BaseResponse struct {
	Status       string          `json:"status"`
	Code         ResponseCode    `json:"code"`
	Data         json.RawMessage `json:"data"`
	Meta         *PaginationInfo `json:"meta"`
	ErrorMessage string          `json:"error_message"`
}

BaseResponse base response model

type Error

type Error struct {
	Code    ResponseCode
	Message interface{}
}

Error general service error

func (Error) Error

func (e Error) Error() string

Error implement error interface

type FieldErrs

type FieldErrs map[string][]string

FieldErrs input field level errors

type InboxMessage

type InboxMessage struct {
	To        string    `json:"to"`
	Message   string    `json:"message"`
	From      string    `json:"from"`
	CreatedAt time.Time `json:"created_at"`
	Type      string    `json:"type"`
}

InboxMessage inbox message

type Ippanel

type Ippanel struct {
	Apikey  string
	Client  *http.Client
	BaseURL *url.URL
}

Ippanel ...

func New

func New(apikey string) *Ippanel

New create new ippanel sms instance

func (*Ippanel) CreatePattern

func (sms *Ippanel) CreatePattern(pattern string, description string, variables map[string]string, delimiter string, isShare bool) (string, error)

CreatePattern create new pattern

func (*Ippanel) FetchInbox

func (sms *Ippanel) FetchInbox(pp ListParams) ([]InboxMessage, *PaginationInfo, error)

FetchInbox fetch inbox messages list

func (*Ippanel) FetchStatuses

func (sms *Ippanel) FetchStatuses(MessageId int64, pp ListParams) ([]MessageRecipient, *PaginationInfo, error)

FetchStatuses get message recipients statuses

func (*Ippanel) GetCredit

func (sms *Ippanel) GetCredit() (float64, error)

GetCredit get credit for user

func (*Ippanel) GetMessage

func (sms *Ippanel) GetMessage(MessageId int64) (*Message, error)

GetMessage get a message by message_id

func (*Ippanel) Send

func (sms *Ippanel) Send(sender string, recipients []string, message string, summary string) (int64, error)

Send send a message

func (*Ippanel) SendPattern

func (sms *Ippanel) SendPattern(patternCode string, originator string, recipient string, values map[string]string) (int64, error)

SendPattern send a message with pattern

type ListParams

type ListParams struct {
	Limit int64 `json:"limit"`
	Page  int64 `json:"page"`
}

ListParams ...

type Message

type Message struct {
	MessageId            int64               `json:"message_id"`
	Number               string              `json:"number"`
	Message              string              `json:"message"`
	State                string              `json:"state"`
	Type                 string              `json:"type"`
	ConfirmState         MessageConfirmState `json:"valid"`
	CreatedAt            time.Time           `json:"time"`
	SentAt               time.Time           `json:"time_sent"`
	RecipientCount       int64               `json:"recipient_count"`
	ValidRecipientsCount int64               `json:"exit_count"`
	Part                 int64               `json:"part"`
	Cost                 float64             `json:"cost"`
	ReturnCost           float64             `json:"return_cost"`
	Summary              string              `json:"summary"`
}

Message message model

type MessageConfirmState

type MessageConfirmState string

MessageConfirmState message confirm state

const (
	// MessageConfirmeStatePending pending
	MessageConfirmeStatePending MessageConfirmState = "notconfirm"
	// MessageConfirmeStateConfirmed confirmed
	MessageConfirmeStateConfirmed MessageConfirmState = "approve"
	// MessageConfirmeStateRejected rejected
	MessageConfirmeStateRejected MessageConfirmState = "reject"
)

type MessageRecipient

type MessageRecipient struct {
	Recipient string `json:"recipient"`
	Status    int    `json:"status"`
}

MessageRecipient message recipient status

type PaginationInfo

type PaginationInfo struct {
	Total int64   `json:"total"`
	Limit int64   `json:"limit"`
	Page  int64   `json:"page"`
	Pages int64   `json:"pages"`
	Prev  *string `json:"prev"`
	Next  *string `json:"next"`
}

PaginationInfo ...

type Pattern

type Pattern struct {
	Code    string        `json:"code"`
	Status  PatternStatus `json:"status"`
	Message string        `json:"message"`
	IsShare bool          `json:"is_share"`
}

Pattern pattern

type PatternResType

type PatternResType struct {
	Code string `json:"code"`
}

type PatternStatus

type PatternStatus string

PatternStatus ...

const (
	// PatternStatusActive active
	PatternStatusActive PatternStatus = "active"
	// PatternStatusInactive inactive
	PatternStatusInactive PatternStatus = "inactive"
	// PatternStatusPending pending
	PatternStatusPending PatternStatus = "pending"
)

type PatternVariable

type PatternVariable struct {
	Name string `json:"name"`
	Type string `json:"type"`
}

type ResponseCode

type ResponseCode int

ResponseCode api response code error type

const (
	ErrForbidden           ResponseCode = 403
	ErrNotFound            ResponseCode = 404
	ErrUnprocessableEntity ResponseCode = 422
	ErrInternalServer      ResponseCode = 500
)

Jump to

Keyboard shortcuts

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