freshchat

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2022 License: MIT Imports: 15 Imported by: 0

README

Freshchat

Go Reference Go Report Card

Freshchat API client library using Go, https://developers.freshchat.com/api/#whatsapp.

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 (
	// DefaultBaseURLAustralia is the default base URL for the Freshchat API in Australia.
	DefaultBaseURLAustralia = "https://api.au.freshchat.com"
	// DefaultTimeout is the default timeout for the client.
	DefaultTimeout = 30 * time.Second
)
View Source
const (
	// DefaultProviderWhatsapp is the default provider for sending messages in the Freshchat.
	DefaultProviderWhatsapp = "whatsapp"
	// DefaultStorage is the default storage for sending messages.
	DefaultStorage = "none"
	// DefaultLanguagePolicy is the default language policy for sending messages.
	DefaultLanguagePolicy = "deterministic"
	// DefaultLanguageCode is the default language code for sending messages.
	DefaultLanguageCode = "id"
)
View Source
const (
	// TypePhoneSender is the type for phone sender.
	TypePhoneSender = "sender"
	// TypePhoneDestination is the type for phone destination.
	TypePhoneDestination = "destination"
)
View Source
const (
	// EndpointSendMessage is the endpoint for sending a message.
	EndpointSendMessage = "/v2/outbound-messages/whatsapp"
)
View Source
const (
	// HeaderBearerPrefix is the prefix for the Bearer token.
	HeaderBearerPrefix = "Bearer "
)

Variables

View Source
var (
	// ErrNilArguments is returned when the arguments are nil.
	ErrNilArguments = errors.New("nil arguments")
)

Functions

This section is empty.

Types

type Client added in v1.0.0

type Client interface {
	SendMessage(ctx context.Context, req *RequestWhatsappMessage) (res *ResponseFreshchat, err error)
}

Client is the client for sending messages in the Freshchat.

func NewClient added in v1.0.0

func NewClient(opts ...FnOption) Client

NewClient creates a new Freshchat client.

type FnOption added in v1.0.0

type FnOption func(o *Option)

FnOption is a functional option for the Freshchat API

func WithAPIToken added in v1.0.2

func WithAPIToken(apiToken string) FnOption

WithAPIToken sets the API token for the Freshchat API.

func WithBaseURL added in v1.0.0

func WithBaseURL(baseURL string) FnOption

WithBaseURL sets the base URL for the Freshchat API.

func WithClient added in v1.0.0

func WithClient(client heimdall.Doer) FnOption

WithClient sets the client for the Freshchat API.

func WithFromPhoneNumber added in v1.0.0

func WithFromPhoneNumber(fromPhoneNumber string) FnOption

WithFromPhoneNumber sets the from phone number for the Freshchat API.

func WithHystrixOptions added in v1.0.0

func WithHystrixOptions(options []hystrix.Option) FnOption

WithHystrixOptions sets the hystrix options for the Freshchat API.

func WithNameSpace added in v1.0.0

func WithNameSpace(nameSpace string) FnOption

WithNameSpace sets the name space for the Freshchat API.

func WithTimeout added in v1.0.0

func WithTimeout(timeout time.Duration) FnOption

WithTimeout sets the timeout for the Freshchat API.

type Option added in v1.0.0

type Option struct {
	BaseURL         string
	NameSpace       string
	APIToken        string
	FromPhoneNumber string
	Timeout         time.Duration
	Client          heimdall.Doer
	HystrixOptions  []hystrix.Option
	// contains filtered or unexported fields
}

Option is an option for the Freshchat API

func (*Option) Assign added in v1.0.0

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

Assign assigns the functional options to the option.

func (*Option) Clone added in v1.0.0

func (o *Option) Clone() *Option

Clone clones the option and returns a new one.

func (*Option) Default added in v1.0.0

func (o *Option) Default() *Option

Default returns option with default values.

type RequestData added in v1.0.0

type RequestData struct {
	MessageTemplate RequestMessageTemplate `json:"message_template"`
}

RequestData is a request for specifying the message data.

func (*RequestData) Default added in v1.0.0

func (r *RequestData) Default(o *Option) *RequestData

Default is a default for RequestData.

type RequestFrom added in v1.0.0

type RequestFrom struct {
	PhoneNumber string `json:"phone_number"`
}

RequestFrom is a request for specifying the sender number.

func (*RequestFrom) Default added in v1.0.0

func (r *RequestFrom) Default(o *Option, typePhone string) *RequestFrom

Default is a default for RequestFrom.

type RequestLanguage added in v1.0.0

type RequestLanguage struct {
	Policy string `json:"policy"`
	Code   string `json:"code"`
}

RequestLanguage is a request for specifying the language.

func (*RequestLanguage) Default added in v1.0.0

func (r *RequestLanguage) Default(o *Option) *RequestLanguage

Default is a default for RequestLanguage.

type RequestMessageTemplate added in v1.0.0

type RequestMessageTemplate struct {
	Storage          string                  `json:"storage"`
	TemplateName     string                  `json:"template_name"`
	Namespace        string                  `json:"namespace"`
	Language         RequestLanguage         `json:"language"`
	RichTemplateData RequestRichTemplateData `json:"rich_template_data"`
}

RequestMessageTemplate is a request for specifying the message template.

func (*RequestMessageTemplate) Default added in v1.0.0

Default is a default for RequestMessageTemplate.

type RequestRichTemplateData added in v1.0.0

type RequestRichTemplateData struct {
	Header *RequestRichTmplHeader `json:"header,omitempty"`
	Body   RequestRichTmplBody    `json:"body"`
}

RequestRichTemplateData is a request for specifying the rich template data.

type RequestRichTmplBody added in v1.0.0

type RequestRichTmplBody struct {
	Params []RequestRichTmplParams `json:"params"`
}

RequestRichTmplBody is a request for specifying the rich template body.

type RequestRichTmplHeader added in v1.0.0

type RequestRichTmplHeader struct {
	Type     string                  `json:"type,omitempty"`
	MediaURL string                  `json:"media_url,omitempty"`
	Params   []RequestRichTmplParams `json:"params,omitempty"`
}

RequestRichTmplHeader is a request for specifying the rich template header.

type RequestRichTmplParams added in v1.0.0

type RequestRichTmplParams struct {
	Data string `json:"data"`
}

RequestRichTmplParams is a request for specifying the rich template params data.

type RequestWhatsappMessage added in v1.0.0

type RequestWhatsappMessage struct {
	From     RequestFrom    `json:"from"`
	Provider string         `json:"provider"`
	To       []*RequestFrom `json:"to"`
	Data     RequestData    `json:"data"`

	// Response Attributes Below
	MessageID     string `json:"message_id,omitempty"`
	RequestID     string `json:"request_id,omitempty"`
	Status        string `json:"status,omitempty"`
	FailureCode   string `json:"failure_code,omitempty"`
	FailureReason string `json:"failure_reason,omitempty"`
	CreatedOn     int    `json:"created_on,omitempty"`
}

RequestWhatsappMessage is a request for sending a message to a user.

func (*RequestWhatsappMessage) AddDestination added in v1.0.0

func (r *RequestWhatsappMessage) AddDestination(phoneNumber string) *RequestWhatsappMessage

AddDestination adds the destination for the message.

func (*RequestWhatsappMessage) Default added in v1.0.0

Default is a default for RequestWhatsappMessage.

func (*RequestWhatsappMessage) SetDestinations added in v1.0.0

func (r *RequestWhatsappMessage) SetDestinations(phoneNumbers []string) *RequestWhatsappMessage

SetDestinations sets the destinations for the message.

func (*RequestWhatsappMessage) SetFrom added in v1.0.0

SetFrom sets the from/sender for the message.

func (*RequestWhatsappMessage) SetHeaderMediaURL added in v1.0.0

func (r *RequestWhatsappMessage) SetHeaderMediaURL(url string) *RequestWhatsappMessage

SetHeaderMediaURL sets the header media URL for the message.

func (*RequestWhatsappMessage) SetHeaderParams added in v1.0.0

func (r *RequestWhatsappMessage) SetHeaderParams(params []string) *RequestWhatsappMessage

SetHeaderParams sets the header params for the message.

func (*RequestWhatsappMessage) SetHeaderType added in v1.0.0

func (r *RequestWhatsappMessage) SetHeaderType(headerType string) *RequestWhatsappMessage

SetHeaderType sets the header type for the message.

func (*RequestWhatsappMessage) SetLanguageCode added in v1.0.0

func (r *RequestWhatsappMessage) SetLanguageCode(code string) *RequestWhatsappMessage

SetLanguageCode sets the language code for the message.

func (*RequestWhatsappMessage) SetLanguagePolicy added in v1.0.0

func (r *RequestWhatsappMessage) SetLanguagePolicy(policy string) *RequestWhatsappMessage

SetLanguagePolicy sets the language policy for the message.

func (*RequestWhatsappMessage) SetNamespace added in v1.0.0

SetNamespace sets the namespace for the message.

func (*RequestWhatsappMessage) SetParams added in v1.0.0

func (r *RequestWhatsappMessage) SetParams(params []string) *RequestWhatsappMessage

SetParams sets the params for the message.

func (*RequestWhatsappMessage) SetProvider added in v1.0.0

func (r *RequestWhatsappMessage) SetProvider(provider string) *RequestWhatsappMessage

SetProvider sets the provider for the message.

func (*RequestWhatsappMessage) SetStorage added in v1.0.0

func (r *RequestWhatsappMessage) SetStorage(storage string) *RequestWhatsappMessage

SetStorage sets the storage for the message.

func (*RequestWhatsappMessage) SetTemplateName added in v1.0.0

func (r *RequestWhatsappMessage) SetTemplateName(tmpl string) *RequestWhatsappMessage

SetTemplateName sets the template name for the message.

type ResponseFailed added in v1.0.0

type ResponseFailed struct {
	Success      bool   `json:"success"`
	ErrorCode    int    `json:"error_code"`
	ErrorMessage string `json:"error_message"`
}

ResponseFailed is the failed response from Freshchat.

func (*ResponseFailed) Error added in v1.0.0

func (r *ResponseFailed) Error() string

Error implements the error interface.

type ResponseFreshchat added in v1.0.0

type ResponseFreshchat struct {
	Success        *ResponseSuccess `json:"-"`
	Failed         *ResponseFailed  `json:"-"`
	HTTPStatusCode int              `json:"-"`
	RawData        string           `json:"-"`
}

ResponseFreshchat is the response from Freshchat.

type ResponseSuccess added in v1.0.0

type ResponseSuccess struct {
	RequestID          string              `json:"request_id"`
	RequestProcessTime string              `json:"request_process_time"`
	Link               ResponseSuccessLink `json:"link"`
}

ResponseSuccess is the success response from Freshchat.

type ResponseSuccessLink struct {
	Href string `json:"href"`
	Rel  string `json:"rel"`
	Type string `json:"type"`
}

ResponseSuccessLink is the success link response from Freshchat.

Jump to

Keyboard shortcuts

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