Documentation ¶
Index ¶
- Variables
- func ValidateAppMenus(app *UssdApp) error
- func ValidateMenu(m Menu) error
- type Cacher
- type Logger
- type Menu
- type MenuOptions
- type Options
- type SessionResponse
- type UssdApp
- func (app *UssdApp) AddMenu(m Menu) error
- func (app *UssdApp) Cache() *redis.Client
- func (app *UssdApp) GetCurrentMenu(ctx context.Context, payload UssdPayload) (Menu, error)
- func (app *UssdApp) GetLanguage(ctx context.Context, payload UssdPayload) string
- func (app *UssdApp) GetMenuNames() []string
- func (app *UssdApp) GetNextMenu(payload UssdPayload, currentMenu string) (Menu, error)
- func (app *UssdApp) GetPreviousPayload(ctx context.Context, payload UssdPayload) (UssdPayload, error)
- func (app *UssdApp) GetSessionKey(_ context.Context, payload UssdPayload) string
- func (app *UssdApp) GetShortCutMenu(ctx context.Context, payload UssdPayload) string
- func (app *UssdApp) IsNewSession(ctx context.Context, payload UssdPayload) (bool, error)
- func (app *UssdApp) PreviousMenuInvalid(ctx context.Context, payload UssdPayload, helper string) (*SessionResponse, error)
- func (app *UssdApp) SaveCurrentMenu(ctx context.Context, payload UssdPayload, menuName string) (Menu, error)
- func (app *UssdApp) SaveLanguage(ctx context.Context, payload UssdPayload, language string) error
- func (app *UssdApp) SetHomeMenu(menuName string)
- func (app *UssdApp) SetUserInCache(ctx context.Context, payload UssdPayload, user map[string]interface{}) error
- func (app *UssdApp) UserExistInCache(ctx context.Context, payload UssdPayload) (bool, error)
- type UssdPayload
Constants ¶
This section is empty.
Variables ¶
Functions ¶
func ValidateAppMenus ¶
func ValidateMenu ¶
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 ¶
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
type MenuOptions ¶
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 ¶
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 (*UssdApp) GetCurrentMenu ¶
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 ¶
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 ¶
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 ¶
SaveLanguage will save user language for the ussd session
func (*UssdApp) SetHomeMenu ¶
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 ¶
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