infobip

package module
v0.0.0-...-0c27a14 Latest Latest
Warning

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

Go to latest
Published: May 25, 2019 License: MIT Imports: 7 Imported by: 0

README

infobip

Infobip API client library in Go

Build Status GoDoc Go Report Card

Usage

To initiate a client, you should use the infobip.ClientWithBasicAuth func. This will returns a pointer to infobip.Client and allows to you use features from service.

Sending a single message

The func needs a infobip.Message struct. That struct consists of the following attributes:

Attribute Type Description
From string Represents a sender ID which can be alphanumeric or numeric
To string Message destination address
Text string Text of the message that will be sent

It has a func to validate the From and To attributes, according to Infobip docs, and it is used into all funcs that make a request to the service. The following code is a basic example of the validate func:

package main

import (
    "log"

    "github.com/levpay/infobip"
)

func main() {
    m := infobip.Message{
        From: "Company", // or company number
        To:   "41793026727",
        Text: "This is an example of the body of the SMS message",
    }
    err := m.Validate()
    if err != nil {
        log.Fatalf("Infobip message error: %v", err)
    }
}

Finally, the following code is a full example to send a single message to Infobip service:

package main

import (
    "fmt"
    "log"

    "github.com/levpay/infobip"
)

func main() {
    client := infobip.ClientWithBasicAuth("foo", "bar")
    r, err := client.SingleMessage(m) // "m" refers to the variable from the previous example
    if err != nil {
        log.Fatalf("Infobip error: %v", err)
    }
    fmt.Printf("Infobip response: %v", r)
}
Sending a advanced message

The func needs a infobip.BulkMessage struct. That struct consists of the following attributes:

Attribute Type Description
ID string The ID which uniquely identifies the request
Messages slice of Message Message values

And the infobip.Message struct consists in the following attributes:

Attribute Type Description
From string Represents a sender ID which can be alphanumeric or numeric
Destinations slice of Destination Destination values
Text string Text of the message that will be sent
Transliteration string Conversion of a message text from one script to another
LanguageCode string Code for language character set of a message text

And finally the infobip.Destination struct consists in the following attributes:

Attribute Type Description
ID string The ID that uniquely identifies the message sent
To string Message destination address

The following code is a basic example of the validate func:

package main

import (
    "log"

    "github.com/levpay/infobip"
)

func main() {
    m := infobip.BulkMessage{
        Messages: []infobip.Message{
            infobip.Message{
                From: "Company", // or company number
                Destinations: []infobip.Destination{
                    infobip.Destination{
                        To: "41793026727",
                    },
                },
                Text:            "This is an example of the body of the SMS message",
                Transliteration: "PORTUGUESE",
                LanguageCode:    "PT",
            },
        },
    }
    err := m.Validate()
    if err != nil {
        log.Fatalf("Infobip message error: %v", err)
    }
}

Finally, the following code is a full example to send an advanced message to Infobip service:

package main

import (
    "fmt"
    "log"

    "github.com/levpay/infobip"
)

func main() {
    client := infobip.ClientWithBasicAuth("foo", "bar")
    r, err := client.AdvancedMessage(m) // "m" refers to the variable from the previous example
    if err != nil {
        log.Fatalf("Infobip error: %v", err)
    }
    fmt.Printf("Infobip response: %v", r)
}

Documentation

Index

Constants

View Source
const (

	// BasePath ...
	BasePath = "https://api.infobip.com/"

	//SingleMessagePath for sending a single message
	SingleMessagePath = "sms/1/text/single"

	//AdvancedMessagePath for sending advanced messages
	AdvancedMessagePath = "sms/1/text/advanced"

	// AvailableNumberPath for searching number
	AvailableNumberPath = "numbers/1/numbers/available"

	// RentNumberPath for purchasing number
	RentNumberPath = "numbers/1/numbers"

	// UnrentNumberPath for purchasing number
	UnrentNumberPath = "numbers/1/numbers"

	// PurchasedNumberPath to get purchased number
	PurchasedNumberPath = "numbers/1/numbers"

	// SMSStatusPath for getting status
	SMSStatusPath = "sms/1/reports"
)
View Source
const (
	ForwardTypePull = "PULL"
	ForwardTypePost = "HTTP_FORWARD_POST"
	ForwardTypeGet  = "HTTP_FORWARD_GET"
)

Variables

View Source
var (
	// ErrForDestinationNonAlphanumeric ...
	ErrForDestinationNonAlphanumeric = Error{Err: "non-alphanumeric 'Destination' value must be between 3 and 14 numbers"}

	// ErrForFromNonAlphanumeric ...
	ErrForFromNonAlphanumeric = Error{Err: "non-alphanumeric 'From' value must be between 3 and 14 numbers"}

	// ErrForFromAlphanumeric ...
	ErrForFromAlphanumeric = Error{Err: "alphanumeric 'From' value must be between 3 and 13 characters"}

	// ErrForToNonAlphanumeric ...
	ErrForToNonAlphanumeric = Error{Err: "non-alphanumeric 'To' value must be between 3 and 14 numbers"}

	// ErrSMSStatusNotFound ...
	ErrSMSStatusNotFound = Error{Err: "SMS Status not found"}

	// ErrNoAuthentication ...
	ErrNoAuthentication = Error{Err: "Not auth format available"}
)

Functions

This section is empty.

Types

type Action

type Action struct {
	ActionKey    string `json:"actionKey,omitempty" url:"actionKey,omitempty"`
	Type         string `json:"type,omitempty" url:"type,omitempty"`
	ForwardURL   string `json:"forwardUrl,omitempty" url:"forwardUrl,omitempty"`
	CallbackData string `json:"callbackData,omitempty" url:"callbackData,omitempty"`
}

type BulkMessage

type BulkMessage struct {
	ID       string    `json:"bulkId,omitempty"`
	Messages []Message `json:"messages"`
}

BulkMessage contains the body request for multiple messages

func (BulkMessage) Validate

func (b BulkMessage) Validate() (err error)

Validate validates the entire message values

type Client

type Client struct {
	BaseURL    string
	Username   string
	Password   string
	ApiKey     string
	HTTPClient HTTPInterface
}

Client manages requests to Infobip

func ClientWithApiKey

func ClientWithApiKey(apiKey string) *Client

ClientWithApiKey returns a pointer to infobip.Client with Infobip funcs

func ClientWithBasicAuth

func ClientWithBasicAuth(username, password string) *Client

ClientWithBasicAuth returns a pointer to infobip.Client with Infobip funcs

func (Client) AdvancedMessage

func (c Client) AdvancedMessage(m BulkMessage) (*MessageResponse, error)

AdvancedMessage sends messages to the recipients

func (Client) CreateNumberAction

func (c Client) CreateNumberAction(actionData *Action, numberKey, configKey string) (*Action, error)

CreateNumberAction create a action for a specific number key

func (Client) CreateNumberConfig

func (c Client) CreateNumberConfig(numberKey string) (*ConfigResponse, error)

CreateNumberConfig create a config for a specific number key

func (Client) GetSMSStatus

func (c Client) GetSMSStatus(messageID string) (*MessageStatusWithID, error)

GetSMSStatus return sms status

func (Client) ListPurchasedNumber

func (c Client) ListPurchasedNumber(parmas NumberParmas) (*NumberResponse, error)

ListPurchasedNumber return a purchased number

func (Client) Rent

func (c Client) Rent(numberKey string) (*Number, error)

Rent return a newly purchased number

func (Client) SearchNumber

func (c Client) SearchNumber(parmas NumberParmas) (*NumberResponse, error)

SearchNumber return a list of available number

func (Client) SingleMessage

func (c Client) SingleMessage(m Message) (*MessageResponse, error)

SingleMessage sends one message to one recipient

func (Client) Unrent

func (c Client) Unrent(numberKey string) error

Unrent rent a number using numberKey

type ConfigResponse

type ConfigResponse struct {
	ConfigurationKey string `json:"configurationKey,omitempty" url:"configurationKey,omitempty"`
	IsActive         bool   `json:"isActive,omitempty" url:"isActive,omitempty"`
}

type Destination

type Destination struct {
	ID string `json:"messageId,omitempty"`
	To string `json:"to"`
}

Destination contains the recipient

type Error

type Error struct {
	Err string `json:"error,omitempty"`
}

Error for Infobip

func (Error) Error

func (e Error) Error() string

Error func to implements error interface

type HTTPInterface

type HTTPInterface interface {
	Do(req *http.Request) (*http.Response, error)
}

HTTPInterface helps Infobip tests

type Message

type Message struct {
	From            string        `json:"from,omitempty"`
	Destinations    []Destination `json:"destinations,omitempty"`
	To              string        `json:"to,omitempty"`
	Text            string        `json:"text"`
	Transliteration string        `json:"transliteration,omitempty"`
	LanguageCode    string        `json:"languageCode,omitempty"`
	NotifyURL       string        `json:"notifyUrl,omitempty"`
}

Message contains the body request

func (Message) Validate

func (m Message) Validate() (err error)

Validate validates the body request values

type MessageInfo

type MessageInfo struct {
	ID       string        `json:"messageId"`
	To       string        `json:"to"`
	Status   MessageStatus `json:"status"`
	SMSCount int           `json:"smsCount"`
}

MessageInfo ...

type MessageResponse

type MessageResponse struct {
	BulkID   string        `json:"bulkId,omitempty"`
	Messages []MessageInfo `json:"messages"`
}

MessageResponse body response

type MessageStatus

type MessageStatus struct {
	ID          int    `json:"id"`
	Action      string `json:"action,omitempty"`
	GroupID     int    `json:"groupId"`
	GroupName   string `json:"groupName"`
	Name        string `json:"name"`
	Description string `json:"description"`
}

MessageStatus ...

type MessageStatusResponse

type MessageStatusResponse struct {
	Results []MessageStatusWithID `json:"results"`
}

MessageStatusResponse ...

type MessageStatusWithID

type MessageStatusWithID struct {
	BulkID string        `json:"bulkId,omitempty"`
	ID     string        `json:"messageId"`
	To     string        `json:"to"`
	Status MessageStatus `json:"status"`
}

MessageStatusWithID ...

type Number

type Number struct {
	NumberKey    string      `json:"numberKey"`
	Number       string      `json:"number"`
	Country      string      `json:"country"`
	Type         string      `json:"type,omitempty"`
	Capabilities []string    `json:"capabilities"`
	Shared       bool        `json:"shared,omitempty"`
	Price        NumberPrice `json:"price,omitempty"`
}

type NumberParmas

type NumberParmas struct {
	Number       string `json:"number,omitempty" url:"number,omitempty"`
	Capabilities string `json:"capabilities,omitempty" url:"capabilities,omitempty"`
	Country      string `json:"country,omitempty" url:"country,omitempty"`
	Limit        int    `json:"limit,omitempty" url:"limit,omitempty"`
	Page         int    `json:"page,omitempty" url:"page,omitempty"`
}

type NumberPrice

type NumberPrice struct {
	PricePerMonth     float32 `json:"pricePerMonth,omitempty"`
	SetupPrice        float32 `json:"setupPrice,omitempty"`
	InitialMonthPrice float32 `json:"initialMonthPrice,omitempty"`
	Currency          string  `json:"currency,omitempty"`
}

type NumberResponse

type NumberResponse struct {
	NumberCount int      `json:"numberCount,omitempty"`
	Numbers     []Number `json:"numbers,omitempty"`
}

Jump to

Keyboard shortcuts

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