infobip

package module
v0.0.0-...-0631a20 Latest Latest
Warning

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

Go to latest
Published: Oct 3, 2018 License: MIT Imports: 5 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 (
	//SingleMessagePath for sending a single message
	SingleMessagePath = "sms/1/text/single"

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

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"}
)

Functions

This section is empty.

Types

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
	HTTPClient HTTPInterface
}

Client manages requests to Infobip

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) (r Response, err error)

AdvancedMessage sends messages to the recipients

func (Client) SingleMessage

func (c Client) SingleMessage(m Message) (r Response, err error)

SingleMessage sends one message to one recipient

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"`
}

Message contains the body request

func (Message) Validate

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

Validate validates the body request values

type Response

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

Response body response

type ResponseMessage

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

ResponseMessage ...

type ResponseStatus

type ResponseStatus 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"`
}

ResponseStatus ...

Jump to

Keyboard shortcuts

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