ussdapp

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2022 License: Apache-2.0 Imports: 12 Imported by: 0

README

ussdapp

Golang library for creating ussd apps

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrFailedValidation = errors.New("validation failed")
	ErrMenuNotExist     = fmt.Errorf("menu does not exist")
	ErrMenuExist        = fmt.Errorf("menu is registered")
)

Functions

func ValidateAppMenus

func ValidateAppMenus(app *UssdApp) error

func ValidateMenu

func ValidateMenu(m Menu) error

Types

type Cacher

type Cacher interface {
	Set(ctx context.Context, key, value string) error
	Get(ctx context.Context, key string) (string, error)
	Delete(ctx context.Context, key, value string) error
	SetMap(ctx context.Context, key string, fields map[string]interface{}) error
	GetMap(ctx context.Context, key string) (map[string]string, error)
	DeleteMap(ctx context.Context, key string) error
	SetMapField(ctx context.Context, key string, values ...interface{}) error
	GetMapField(ctx context.Context, key, field string) (string, error)
	DeleteMapField(ctx context.Context, key string, fields ...string) error
}

Cacher represent a generic interface for a cache store

type Logger

type Logger interface {
	Errorf(format string, args ...interface{})
	Fatalf(format string, args ...interface{})
	Fatal(args ...interface{})
	Infof(format string, args ...interface{})
	Info(args ...interface{})
	Warnf(format string, args ...interface{})
	Debugf(format string, args ...interface{})
	Debug(args ...interface{})
}

Logger represent common interface for logging function

type Menu interface {
	MenuName() string
	PreviousMenu() string
	NextMenu() string
	ShortCut() string
	HasArbitraryInput() bool
	GenerateMenuFn(context.Context, UssdPayload, Menu) (*SessionResponse, error)
	ParseResponse(key string, args ...interface{}) *SessionResponse
}

Menu is an interface to be used for reading menu options. Prevents writes to the underlying menu data

func NewMenu

func NewMenu(opt *MenuOptions) Menu

NewMenu will create a new menu instance

type MenuOptions struct {
	MenuName          string
	PreviousMenu      string
	NextMenu          string
	ShortCut          string
	HasArbitraryInput bool
	MenuItems         map[string]string
	MenuContent       map[string]string
	GenerateMenuFn    generateMenuFn
}

MenuOptions contains data for a USSD menu.

type Options

type Options struct {
	AppName         string
	Cache           *redis.Client
	Logger          Logger
	TableName       string
	DefaultLanguage string
	SessionDuration time.Duration
}

Options contains data required for ussd app

type SessionResponse

type SessionResponse struct {
	Response      string
	Failed        bool
	StatusMessage string
	MenuName      string
}

func FailedResponse

func FailedResponse(sessionResponse *SessionResponse) *SessionResponse

FailedResponse is a helper that will return failed *SessionResponse i.e the SessionResponse.Failed will be true;

func SuccessfulResponse

func SuccessfulResponse(sessionResponse *SessionResponse) *SessionResponse

SuccessfulResponse is a helper that will return successful *SessionResponse i.e the SessionResponse.Failed will be fa,se;

type UssdApp

type UssdApp struct {
	// contains filtered or unexported fields
}

func NewUssdApp

func NewUssdApp(ctx context.Context, opt *Options) (*UssdApp, error)

func (*UssdApp) AddMenu

func (app *UssdApp) AddMenu(m Menu) error

func (*UssdApp) Cache

func (app *UssdApp) Cache() *redis.Client

func (*UssdApp) GetCurrentMenu

func (app *UssdApp) GetCurrentMenu(ctx context.Context, payload UssdPayload) (Menu, error)

GetCurrentMenu will get the current menu

If no menu is found, it will default to home menu

To set the home menu, use the helper SetHomeMenu

func (*UssdApp) GetLanguage

func (app *UssdApp) GetLanguage(ctx context.Context, payload UssdPayload) string

GetLanguage will get the preferred language for the ussd session

func (*UssdApp) GetMenuNames

func (app *UssdApp) GetMenuNames() []string

GetMenuNames will return all menu names registered as a slice of strings

func (*UssdApp) GetNextMenu

func (app *UssdApp) GetNextMenu(payload UssdPayload, currentMenu string) (Menu, error)

GetNextMenu will attempt to get the highest matching menu to be saved or/and rendered

func (*UssdApp) GetPreviousPayload

func (app *UssdApp) GetPreviousPayload(ctx context.Context, payload UssdPayload) (UssdPayload, error)

GetPreviousPayload will get the payload for the ussd requesr

func (*UssdApp) GetSessionKey

func (app *UssdApp) GetSessionKey(_ context.Context, payload UssdPayload) string

GetSessionKey will get the session key that is used to save the user data in cache

func (*UssdApp) GetShortCutMenu

func (app *UssdApp) GetShortCutMenu(ctx context.Context, payload UssdPayload) string

GetShortCutMenu is a helper to find the first menu registered with the given shortcut

A shortcut in this case is the ussd string data that comes during first session

The method should only be called for new sessions as ongoing session cannot be deemed as shortcut

func (*UssdApp) IsNewSession

func (app *UssdApp) IsNewSession(ctx context.Context, payload UssdPayload) (bool, error)

IsNewSession will check if incoming ussd session is new

If session is new, it will be saved and automatically be cleared after session duration

func (*UssdApp) PreviousMenuInvalid

func (app *UssdApp) PreviousMenuInvalid(ctx context.Context, payload UssdPayload, helper string) (*SessionResponse, error)

PreviousMenuInvalid will return render the previous menu content prefixed by the error string

This helper is usually called after the user has input wrong details and you want to return the same menu but a the helper string appended on the top of the menu. The helper is mearnt to guide the user on what went wrong

func (*UssdApp) SaveCurrentMenu

func (app *UssdApp) SaveCurrentMenu(ctx context.Context, payload UssdPayload, menuName string) (Menu, error)

SaveCurrentMenu will save the menu as current in cache.

It will be used for the next incoming request to determine the right menu to render

Will fail of the menu does not exist

func (*UssdApp) SaveLanguage

func (app *UssdApp) SaveLanguage(ctx context.Context, payload UssdPayload, language string) error

SaveLanguage will save user language for the ussd session

func (*UssdApp) SetHomeMenu

func (app *UssdApp) SetHomeMenu(menuName string)

SetHomeMenu will set default home menu for the ussd app

func (*UssdApp) SetUserInCache

func (app *UssdApp) SetUserInCache(ctx context.Context, payload UssdPayload, user map[string]interface{}) error

SetUserInCache is a helper to save user details in cache

func (*UssdApp) UserExistInCache

func (app *UssdApp) UserExistInCache(ctx context.Context, payload UssdPayload) (bool, error)

UserExistInCache is a helper to check if user is already saved in cache

type UssdPayload

type UssdPayload interface {
	SessionId() string
	ServiceCode() string
	Msisdn() string
	UssdParams() string
	UssdCurrentParam() string
	IsShortCut() bool
	ValidationFailed() bool
	Time() string
	JSON() ([]byte, error)
}

UssdPayload interface has getters for getting original session data for the ussd request. It is an interface to prevents inadvertent modification of ussd data down the request chain

func UssdPayloadFromJSON

func UssdPayloadFromJSON(jsonData []byte) (UssdPayload, error)

UssdPayloadFromRequest will read the json byte and return an interface for reading data

func UssdPayloadFromRequest

func UssdPayloadFromRequest(r *http.Request) UssdPayload

UssdPayloadFromRequest will read request params or body and return an interface for reading data

Jump to

Keyboard shortcuts

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