valuefirst

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2022 License: MIT Imports: 19 Imported by: 0

README

valuefirst

Go Reference Go Report Card

Valuefirst is a library written in Golang to connect to the ValueFirst API.

How to Test

To run the integration tests, we need to do the following:

  1. Make a new copy of .env.example to .env by running this command below:
cat .env.example > .env
  1. Fill the new .env with the parameter that we already prepared.
  2. Run the tests by running this command:
go test -v -race -tags=integration -covermode=atomic ./...

Documentation

Index

Constants

View Source
const (
	URLToken               = "/api/messages/token"
	URLActionGenerateToken = URLToken + "?action=generate"
	URLActionEnableToken   = URLToken + "?action=enable"
	URLActionDisableToken  = URLToken + "?action=disable"
	URLActionDeleteToken   = URLToken + "?action=delete"
	URLActionSendSMS       = "/servlet/psms.JsonEservice"
)

List of URLs used in this package.

View Source
const (
	// QueryParamToken is the key for adding the token to the query params of URL.
	QueryParamToken = "token"

	// DefaultPayloadVersion is the default version of Payload.
	DefaultPayloadVersion = "1.2"

	// DefaultUDHText is the default UDH used for the text message.
	// UDH is used for sending binary messages.
	// For text message the value should be 0.
	DefaultUDHText = "0"

	// DefaultCodingText is the default coding used for the text message.
	// Extended type of messages.
	// For text message the value should be 1.
	DefaultCodingText = "1"

	// DefaultPropertyText is the default property used for the text message.
	// Unique property of message.
	// Default value is 0.
	// For sending Flash SMS the value should be 1.
	DefaultPropertyText = "0"
	// DefaultPropertyFlash is the default property used for the flash message.
	DefaultPropertyFlash = "1"
	// ChannelTypeWhatsapp is the channel type for Whatsapp messages.
	ChannelTypeWhatsapp = "4"
	// 	DefaultTemplateSeparator is the default separator used for TEMPLATEINFO in ValueFirst.
	DefaultTemplateSeparator = "~"
)
View Source
const (
	LimitEncodingSingleDigit = 16
	LimitEncodingLower       = 32
	LimitEncodingUpper       = 128
)

These constants represents the lower and upper limit of the characters that don't have to be encoded.

View Source
const (
	// DefaultTimeout sets the default timeout of the HTTP client.
	DefaultTimeout = 30 * time.Second

	// BaseURL sets the base URL of the API.
	BaseURL = "https://api.myvfirst.com/psms"
)

Variables

View Source
var (
	ErrEmptyUsername     = errors.New("empty username")
	ErrEmptyPassword     = errors.New("empty password")
	ErrDecodeVarIsNotPtr = errors.New("the decode variable is not a pointer")
	ErrNilRequest        = errors.New("nil request")
)

List of errors in the valuefirst package.

Functions

func Encode added in v1.0.5

func Encode(msg string) (res string)

Encode encodes the message using the step 2 encoding of ValueFirst documentation.

func EncodeHTML added in v1.0.5

func EncodeHTML(msg string) (res string)

EncodeHTML encodes the message using the step 1 encoding of ValueFirst documentation. This encoding is used for HTML special characters.

Types

type Client

type Client interface {
	manager.TokenClient
	// SendSMS sends SMS messages to a list of addresses.
	SendSMS(ctx context.Context, req *RequestSendSMS) (resp *ResponseMessage, err error)
}

Client is the interface for the ValueFirst API client.

func New

func New(opts ...FnOption) (c Client, err error)

New initialize the ValueFirst API client.

type ErrorCode added in v1.0.8

type ErrorCode string

ErrorCode is the error code from the ValueFirst.

const (
	ErrCodeGeneralSuccess       ErrorCode = "0"
	ErrCodeGeneralNotConformDTD ErrorCode = "65535"

	ErrCodeMessageInvalidSenderID ErrorCode = "28680"
	ErrCodeMessageInvalidMessage  ErrorCode = "28681"

	ErrCodeStatusRequestSuccess ErrorCode = "8448"

	ErrCodeSchedulerSuccess ErrorCode = "13568"
)

List of error codes in the ValueFirst.

func (ErrorCode) Integer added in v1.0.8

func (e ErrorCode) Integer() (int, error)

Integer represents the integer type of ErrorCode.

func (ErrorCode) String added in v1.0.9

func (e ErrorCode) String() string

String returns the string representation of the ErrorCode.

type ErrorList

type ErrorList []error

ErrorList is a list of errors.

func (*ErrorList) Append

func (e *ErrorList) Append(err error)

Append appends error to the ErrorList.

func (*ErrorList) Error

func (e *ErrorList) Error() (res string)

Error implements the error interface.

type FnOption

type FnOption func(o *Option)

FnOption is a function that sets the option.

func WithBaseURL

func WithBaseURL(s string) FnOption

WithBaseURL sets the base URL of the API.

func WithBasicAuth

func WithBasicAuth(user string, password string) FnOption

WithBasicAuth sets the basic auth credentials.

func WithClient

func WithClient(c heimdall.Doer) FnOption

WithClient sets the HTTP client.

func WithCustomIPs

func WithCustomIPs(ips ...string) FnOption

WithCustomIPs sets the custom IPs used for hitting the API.

func WithHystrixOptions

func WithHystrixOptions(opts ...hystrix.Option) FnOption

WithHystrixOptions sets the hystrix options.

func WithTimeout

func WithTimeout(t time.Duration) FnOption

WithTimeout sets the timeout of the HTTP client.

func WithTokenManager

func WithTokenManager(tm manager.TokenManager) FnOption

WithTokenManager sets the token manager.

func WithTokenManagerOptions added in v1.0.10

func WithTokenManagerOptions(opts ...manager.FnOption) FnOption

WithTokenManagerOptions sets the token manager options.

type MessageType added in v1.0.5

type MessageType string

MessageType is the type of the message for Whatsapp.

const (
	// MessageTypePlain is the plain type of Whatsapp message.
	MessageTypePlain MessageType = "1"
	// MessageTypePlainTwoWay is the plain type of Whatsapp message with two-way communication.
	MessageTypePlainTwoWay MessageType = "2"
	// MessageTypeRich is the rich type of Whatsapp message.
	MessageTypeRich MessageType = "3"
	// MessageTypeRichTwoWay is the rich type of Whatsapp message with two-way communication.
	MessageTypeRichTwoWay MessageType = "4"
	// MessageTypeBusinessCardSharingTwoWay is the business card sharing type of Whatsapp message with two-way communication.
	MessageTypeBusinessCardSharingTwoWay MessageType = "5"
	// MessageTypeLocationSharingTwoWay is the location sharing type of Whatsapp message with two-way communication.
	MessageTypeLocationSharingTwoWay MessageType = "6"
	// MessageTypeListTwoWay is the list type of Whatsapp message with two-way communication.
	MessageTypeListTwoWay MessageType = "7"
	// MessageTypeReplyButtonTwoWay is the reply button type of Whatsapp message with two-way communication.
	MessageTypeReplyButtonTwoWay MessageType = "8"
)

func (MessageType) String added in v1.0.5

func (m MessageType) String() string

String returns the string representation of the MessageType.

type Option

type Option struct {
	BaseURL             string
	BasicAuth           OptionBasicAuth
	Timeout             time.Duration
	Client              heimdall.Doer
	HystrixOptions      []hystrix.Option
	CustomIPs           []string
	TokenManager        manager.TokenManager
	TokenManagerOptions []manager.FnOption
	// contains filtered or unexported fields
}

Option is a config for Value.

func (*Option) Assign

func (o *Option) Assign(opts ...FnOption) *Option

Assign assigns the Option using the functional options.

func (*Option) Clone

func (o *Option) Clone() *Option

Clone clones the Option. Clone only makes a shallow copy of the Option struct.

func (*Option) Default

func (o *Option) Default() *Option

Default sets the config default value.

func (*Option) Validate

func (o *Option) Validate() (err error)

Validate validates the config variables to ensure smooth integration.

type OptionBasicAuth

type OptionBasicAuth struct {
	User     string
	Password string
}

OptionBasicAuth is a config for basic authorization.

type RequestSendSMS

type RequestSendSMS struct {
	Version string `json:"@VER"`
	User    struct {
		Username      string `json:"@USERNAME,omitempty"`
		Password      string `json:"@PASSWORD,omitempty"`
		ChannelType   string `json:"@CH_TYPE,omitempty"`
		UnixTimestamp string `json:"@UNIXTIMESTAMP,omitempty"`
	} `json:"USER"`
	DLR RequestSendSMSDLR      `json:"DLR"`
	SMS RequestSendSMSMessages `json:"SMS"`
}

RequestSendSMS is the request of SendSMS.

func (*RequestSendSMS) AddMessage added in v1.0.5

AddMessage adds a message to the RequestSendSMS.

func (*RequestSendSMS) Default

func (r *RequestSendSMS) Default() *RequestSendSMS

Default sets the default values for the RequestSendSMS.

func (*RequestSendSMS) SetChannelType added in v1.0.5

func (r *RequestSendSMS) SetChannelType(chType string) *RequestSendSMS

SetChannelType sets the channel type of the RequestSendSMS.

func (*RequestSendSMS) SetDLRURL added in v1.0.5

func (r *RequestSendSMS) SetDLRURL(urlStr string) *RequestSendSMS

SetDLRURL sets the DLR URL of the RequestSendSMS.

func (*RequestSendSMS) SetPassword added in v1.0.5

func (r *RequestSendSMS) SetPassword(password string) *RequestSendSMS

SetPassword sets the password of the RequestSendSMS.

func (*RequestSendSMS) SetTypeWhatsapp added in v1.0.5

func (r *RequestSendSMS) SetTypeWhatsapp() *RequestSendSMS

SetTypeWhatsapp sets the type of RequestSendSMS to Whatsapp.

func (*RequestSendSMS) SetUnixTimestamp added in v1.0.5

func (r *RequestSendSMS) SetUnixTimestamp(unixTimestamp int64) *RequestSendSMS

SetUnixTimestamp sets the UnixTimestamp of the RequestSendSMS.

func (*RequestSendSMS) SetUsername added in v1.0.5

func (r *RequestSendSMS) SetUsername(username string) *RequestSendSMS

SetUsername sets the username of the RequestSendSMS.

func (*RequestSendSMS) SetVersion added in v1.0.5

func (r *RequestSendSMS) SetVersion(version string) *RequestSendSMS

SetVersion sets the version of the RequestSendSMS.

type RequestSendSMSDLR

type RequestSendSMSDLR struct {
	URL string `json:"@URL"`
}

RequestSendSMSDLR is the DLR part of the RequestSendSMS.

type RequestSendSMSMessage

type RequestSendSMSMessage struct {
	// UDH is used for sending binary messages. For text message the value should be 0.
	UDH string `json:"@UDH"`

	// Extended type of messages. For text message the value should be 1.
	Coding string `json:"@CODING"`

	// This field describe the message text to be sent to receiver.
	// SMS can contain up to 160 characters in Message Text.
	// API allows user to send Message text of more than 160 characters.
	// Credits will be deducted in the multiple of 160 characters according to the length of SMS.
	Text string `json:"@TEXT,omitempty"`

	// It is now possible to schedule a message.
	// To schedule message to go at a later time,
	// user can specify “SEND_ON” date as attribute of SMS tag.
	// Only absolute date is supported.
	// The value should be given in “YYYY-MM-DD HH:MM:SS TIMEZONE” format.
	// Time zone is difference w.r.t. to GMT.
	// Example: "2022-09-21 14:20:00 GMT+07:00".
	// For GMT Timezone, we can use both, either GMT+00:00 or GMT-00:00.
	// Please refer Scheduling Support for more information on this feature.
	SendOn string `json:"@SEND_ON,omitempty"`

	// New Parameter TEMPLATEINFO has been added for above functionality
	// which will contain the template id and variables value to be replaced in template text.
	// Template info parameter will have ~ separated values.
	//
	// If both TEXT and TEMPLATEINFO is given then priority will be given to Text.
	//
	// New error code i.e. INVALID_TEMPLATEINFO = 28694; has been created for error if occurred any,
	// related to TEMPLATEINFO parameter, which include like invalid templateid is provided,
	// variables count mismatch than the template Text variables count,
	// template text not found for the given template id.
	TemplateInfo string `json:"@TEMPLATEINFO,omitempty"`

	// Unique property of message. Default value is 0. For sending Flash SMS the value should be 1.
	Property string `json:"@PROPERTY"`

	// Unique ID of message. The client sends this value.
	// In future communication, server sends this value back to the client.
	// This value is used in future to check status of the message.
	ID string `json:"@ID"`

	// Describe the Sender as well as Receiver address.
	Address RequestSendSMSMessageAddresses `json:"ADDRESS"`

	// 	Embed Whatsapp Request
	RequestSendWhatsappMessage
}

RequestSendSMSMessage is the message part of the RequestSendSMS.

func (*RequestSendSMSMessage) AddAddress added in v1.0.5

func (*RequestSendSMSMessage) Default

Default sets the default values for the RequestSendSMSMessage.

func (*RequestSendSMSMessage) SetBURLInfo added in v1.0.5

func (r *RequestSendSMSMessage) SetBURLInfo(bURLInfo string) *RequestSendSMSMessage

SetBURLInfo sets the dynamic URL information of Whatsapp message.

func (*RequestSendSMSMessage) SetCaption added in v1.0.5

func (r *RequestSendSMSMessage) SetCaption(caption string) *RequestSendSMSMessage

SetCaption sets the caption of Whatsapp message.

func (*RequestSendSMSMessage) SetCoding added in v1.0.5

func (r *RequestSendSMSMessage) SetCoding(coding string) *RequestSendSMSMessage

SetCoding sets the coding of the message.

func (*RequestSendSMSMessage) SetContentType added in v1.0.5

func (r *RequestSendSMSMessage) SetContentType(contentType string) *RequestSendSMSMessage

SetContentType sets the content type of Whatsapp message.

func (*RequestSendSMSMessage) SetID added in v1.0.5

SetID sets the ID of the message.

func (*RequestSendSMSMessage) SetMediaData added in v1.0.5

func (r *RequestSendSMSMessage) SetMediaData(mediaData string) *RequestSendSMSMessage

SetMediaData sets the media data of Whatsapp message.

func (*RequestSendSMSMessage) SetMessageType added in v1.0.5

func (r *RequestSendSMSMessage) SetMessageType(msgType MessageType) *RequestSendSMSMessage

SetMessageType sets the message type of Whatsapp message.

func (*RequestSendSMSMessage) SetProperty added in v1.0.5

func (r *RequestSendSMSMessage) SetProperty(property string) *RequestSendSMSMessage

SetProperty sets the property of the message.

func (*RequestSendSMSMessage) SetSendOn added in v1.0.5

func (r *RequestSendSMSMessage) SetSendOn(sendOn string) *RequestSendSMSMessage

SetSendOn sets the scheduled sent time of the message.

func (*RequestSendSMSMessage) SetTemplate added in v1.0.5

func (r *RequestSendSMSMessage) SetTemplate(templateID string, parameters ...string) *RequestSendSMSMessage

SetTemplate sets the template of the RequestSendSMSMessage.

func (*RequestSendSMSMessage) SetText added in v1.0.5

SetText sets the text of the message.

func (*RequestSendSMSMessage) SetType added in v1.0.5

SetType sets the type of Whatsapp message.

func (*RequestSendSMSMessage) SetUDH added in v1.0.5

SetUDH sets the UDH of the message.

type RequestSendSMSMessageAddress

type RequestSendSMSMessageAddress struct {
	// The Sender of the message.
	// This field should conform to Sender Phone Number guidelines.
	From string `json:"@FROM"`

	// Person receiving the SMS, should confirm to Receiver Phone Number guidelines.
	To string `json:"@TO"`

	// Unique Sequence ID.
	// Must be an integer and must be unique to each SMS.
	// While checking message status, you must send this value.
	Sequence string `json:"@SEQ"`

	// A text that identify message.
	// This is an optional parameter.
	Tag string `json:"@TAG"`
}

RequestSendSMSMessageAddress is the address part of the RequestSendSMSMessage.

func (*RequestSendSMSMessageAddress) Default

Default sets the default values for the RequestSendSMSMessageAddress.

func (*RequestSendSMSMessageAddress) Normalize added in v1.0.3

Normalize normalizes the RequestSendSMSMessageAddress.

func (*RequestSendSMSMessageAddress) SetFrom added in v1.0.5

SetFrom sets the sender address of the message.

func (*RequestSendSMSMessageAddress) SetSequence added in v1.0.5

SetSequence sets the sequence address of the message.

func (*RequestSendSMSMessageAddress) SetTag added in v1.0.5

SetTag sets the tag address of the message.

func (*RequestSendSMSMessageAddress) SetTo added in v1.0.5

SetTo sets the destination address of the message.

type RequestSendSMSMessageAddresses

type RequestSendSMSMessageAddresses []RequestSendSMSMessageAddress

RequestSendSMSMessageAddresses is a collection of RequestSendSMSMessageAddress.

func (RequestSendSMSMessageAddresses) Default

Default sets the default values for each RequestSendSMSMessageAddress.

type RequestSendSMSMessages

type RequestSendSMSMessages []RequestSendSMSMessage

RequestSendSMSMessages is a collection of RequestSendSMSMessage.

func (RequestSendSMSMessages) Default

Default sets the default values for each RequestSendSMSMessage.

type RequestSendWhatsappMessage added in v1.0.5

type RequestSendWhatsappMessage struct {
	Caption     string `json:"@CAPTION,omitempty"`
	ContentType string `json:"@CONTENTTYPE,omitempty"`
	Type        string `json:"@TYPE,omitempty"`
	MessageType string `json:"@MSGTYPE,omitempty"`
	MediaData   string `json:"@MEDIADATA,omitempty"`
	BURLInfo    string `json:"@B_URLINFO,omitempty"`
}

RequestSendWhatsappMessage is the Whatsapp message part of the RequestSendSMSMessage.

type ResponseMessage

type ResponseMessage struct {
	MessageAck ResponseMessageAck `json:"MESSAGEACK"`
}

ResponseMessage is the message response of SendSMS.

func (*ResponseMessage) GetError

func (r *ResponseMessage) GetError() (err error)

GetError returns the error if any.

type ResponseMessageAck

type ResponseMessageAck struct {
	GUID interface{} `json:"GUID"`

	// "Err": {
	//            "Desc": "The Specified message does not conform to DTD",
	//            "Code": 65535
	//        }
	Error *ResponseMessageAckError `json:"Err,omitempty"`
}

ResponseMessageAck is the message part of the ResponseMessage.

func (*ResponseMessageAck) GetError

func (r *ResponseMessageAck) GetError() (err error)

GetError returns the error if any.

func (*ResponseMessageAck) GetMessageGUID

func (r *ResponseMessageAck) GetMessageGUID() (res *ResponseMessageAckGUID, ok bool)

GetMessageGUID return the GUID of the ResponseMessageAck.

func (*ResponseMessageAck) GetMessageGUIDs

func (r *ResponseMessageAck) GetMessageGUIDs() (res *ResponseMessageAckGUIDs, ok bool)

GetMessageGUIDs return the GUIDs of the ResponseMessageAck.

func (*ResponseMessageAck) UnmarshalJSON

func (r *ResponseMessageAck) UnmarshalJSON(data []byte) (err error)

UnmarshalJSON implements the json.Unmarshaler interface.

type ResponseMessageAckError

type ResponseMessageAckError struct {
	Description string         `json:"Desc"`
	Code        newtype.String `json:"Code"`
}

ResponseMessageAckError is the error part of the ResponseMessageAck.

func (*ResponseMessageAckError) Error

func (r *ResponseMessageAckError) Error() string

Error implements the error interface.

func (*ResponseMessageAckError) GetCode added in v1.0.9

func (r *ResponseMessageAckError) GetCode() ErrorCode

GetCode returns the Code of the ResponseMessageAckError.

type ResponseMessageAckGUID

type ResponseMessageAckGUID struct {
	// A globally unique message ID that is generated for each <SMS> tag.
	// Note that, in future to check the status of the message you must save this code.
	GUID string `json:"GUID"`

	// The date and time when the transaction was completed.
	SubmitDate string `json:"SUBMITDATE"`

	// (In case of any error)
	// To conserve bandwidth utilization ValueFirst JSON API only sends
	// Sequence information of messages that has either some error or were rejected
	// because of some error. If there are no errors in a particular message,
	// you shall not receive any confirmation of each address SEQ.
	// For instance, in the above example in message ID 1 (of client)
	// the TO number "My company" was rejected as non-numeric.
	// The second message does not have any error,
	// and hence there was no error information for the second part.
	// SEQ: The Sequence ID (provided by client) that has error.
	// CODE: Reason why the message wasn’t accepted.
	// The table shown next describes these error conditions.
	Error interface{} `json:"ERROR,omitempty"`

	// Unique SMS ID sent by the customer. For each message a unique GUID is generated.
	// The Server sends the SMS ID so that
	// the client application can map the GUID to the SMS ID provided by them.
	ID newtype.String `json:"ID"`
}

ResponseMessageAckGUID is the GUID part of the ResponseMessageAck.

func (*ResponseMessageAckGUID) GetError

func (r *ResponseMessageAckGUID) GetError() (err error)

GetError returns the Error of the ResponseMessageAckGUID.

func (*ResponseMessageAckGUID) GetID added in v1.0.9

func (r *ResponseMessageAckGUID) GetID() string

GetID returns the ID string representation of the ResponseMessageAckGUID.

func (*ResponseMessageAckGUID) GetMessageError

func (r *ResponseMessageAckGUID) GetMessageError() (res *ResponseMessageAckGUIDError, ok bool)

GetMessageError return the Error of the ResponseMessageAckGUID.

func (*ResponseMessageAckGUID) GetMessageErrors

func (r *ResponseMessageAckGUID) GetMessageErrors() (res *ResponseMessageAckGUIDErrors, ok bool)

GetMessageErrors return the Errors of the ResponseMessageAckGUID.

func (*ResponseMessageAckGUID) UnmarshalJSON

func (r *ResponseMessageAckGUID) UnmarshalJSON(data []byte) (err error)

UnmarshalJSON implements the json.Unmarshaler interface.

type ResponseMessageAckGUIDError

type ResponseMessageAckGUIDError struct {
	Code     newtype.String `json:"CODE"`
	Sequence newtype.String `json:"SEQ"`
}

ResponseMessageAckGUIDError is the error part of the ResponseMessageAckGUID.

"ERROR": {
		"CODE": 28675,
		"SEQ": 1
		// OR
		"SEQ": "1"
},

func (*ResponseMessageAckGUIDError) Error

func (r *ResponseMessageAckGUIDError) Error() (res string)

Error implements the error interface.

func (*ResponseMessageAckGUIDError) GetCode added in v1.0.9

GetCode returns the Code of the ResponseMessageAckGUIDError.

func (*ResponseMessageAckGUIDError) GetSequence added in v1.0.9

func (r *ResponseMessageAckGUIDError) GetSequence() string

GetSequence returns the Sequence string representation of the ResponseMessageAckGUIDError.

type ResponseMessageAckGUIDErrors

type ResponseMessageAckGUIDErrors []ResponseMessageAckGUIDError

ResponseMessageAckGUIDErrors is the collection of the ResponseMessageAckGUIDError.

func (ResponseMessageAckGUIDErrors) Error

func (r ResponseMessageAckGUIDErrors) Error() (res string)

Error implements the error interface.

type ResponseMessageAckGUIDs

type ResponseMessageAckGUIDs []ResponseMessageAckGUID

ResponseMessageAckGUIDs is a collection of ResponseMessageAckGUID.

func (ResponseMessageAckGUIDs) GetError

func (r ResponseMessageAckGUIDs) GetError() (err error)

GetError returns the error if any.

type UnknownError

type UnknownError struct {
	Message interface{} `json:"message"`
}

UnknownError is an error that is not defined by the documentation from the ValueFirst.

func (UnknownError) Error

func (e UnknownError) Error() string

Error implements the error interface.

Directories

Path Synopsis
Package mocks is a generated GoMock package.
Package mocks is a generated GoMock package.

Jump to

Keyboard shortcuts

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