phada

package module
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2025 License: MIT Imports: 10 Imported by: 0

README

Phada

Go Reference

Phada is a small utility for dealing with AfricasTalking's USSD input. If you've tried to build a USSD application with AT you may have come about the "asterix input problem" i.e. where the data is fed to your application as asterisk separated (ASV?) data.

Phada is a library to reduce the ceremony required to read user's current input, as a bonus you get a way to store the session data

Phada is Chichewa for Hopscotch.

USAGE

Install the module

$ go get github.com/nndi-oss/phada@v0.4.0

If you're using the standard net/http package then you can create the UssdRequestSession using the ParseUssdRequest(*http.Request) function. Otherwise you will have to fill the UssdRequestSession struct yourself if you're using a framework like Gin, Echo, etc..

import (
    "github.com/nndi-oss/phada"
)

var (
    sessionStore = phada.NewInMemorySessionStore()
)

func handler(w http.ResponseWriter, req *http.Request) {
    session, err := phada.ParseUssdRequest(req)
    if err != nil {
        log.Errorf("Failed to parse request to UssdRequestSession, %s", err)
    }
    err = sessionStore.PutHop(session) // store/persist the session
    if err != nil {
        // handle the error
    }
    session, err = sessionStore.Get(session.SessionID)
    if err != nil {
        log.Errorf("Failed to read UssdRequest from sessionStore, Got Error: %s", err)
    }
    // read the current hop/request text
    currentHopInput := session.ReadIn()
    if currentHopInput == "" {
        fmt.Printf("Failed to read input or input was empty")
    }
    // read the text from the first hop only
    fmt.Printf("Got data: %s during Hop 1", session.GetHopN(1))
    // read text for all the hops (basically the way AT sent it)
    fmt.Printf("Got data: %s", session.Text)
}

LICENSE

MIT License, see LICENSE.txt


Copyright (c) 2018 - 2022, NNDI

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type InMemorySessionStore

type InMemorySessionStore struct {
	SessionStore
	// contains filtered or unexported fields
}

InMemorySessionStore

func NewInMemorySessionStore

func NewInMemorySessionStore() *InMemorySessionStore

NewInMemorySessionStore

Creates an inmemory store that uses a concurrent map to store sessions

func (*InMemorySessionStore) Delete

func (m *InMemorySessionStore) Delete(sessionID string)

Delete

func (*InMemorySessionStore) Get

func (m *InMemorySessionStore) Get(sessionID string) (*UssdRequestSession, error)

Get

func (*InMemorySessionStore) PutHop

func (m *InMemorySessionStore) PutHop(ussdRequest *UssdRequestSession) error

PutHop

type RedisSessionStore

type RedisSessionStore struct {
	SessionStore
	// contains filtered or unexported fields
}

RedisSessionStore

func NewRedisSessionStore

func NewRedisSessionStore(redisClient *redis.Client) *RedisSessionStore

NewRedisSessionStore

Creates an inmemory store that uses a concurrent map to store sessions

func (*RedisSessionStore) Delete

func (m *RedisSessionStore) Delete(sessionID string)

Delete

func (*RedisSessionStore) Get

func (m *RedisSessionStore) Get(sessionID string) (*UssdRequestSession, error)

Get

func (*RedisSessionStore) PutHop

func (m *RedisSessionStore) PutHop(ussdRequest *UssdRequestSession) error

PutHop

type RistrettoSessionStore

type RistrettoSessionStore struct {
	SessionStore
	// contains filtered or unexported fields
}

RistrettoSessionStore

func NewRistrettoSessionStore

func NewRistrettoSessionStore() *RistrettoSessionStore

NewRistrettoSessionStore

Creates an inmemory store that uses a concurrent map to store sessions

func (*RistrettoSessionStore) Delete

func (m *RistrettoSessionStore) Delete(sessionID string)

Delete

func (*RistrettoSessionStore) Get

func (m *RistrettoSessionStore) Get(sessionID string) (*UssdRequestSession, error)

Get

func (*RistrettoSessionStore) PutHop

func (m *RistrettoSessionStore) PutHop(ussdRequest *UssdRequestSession) error

PutHop

type SessionStore

type SessionStore interface {
	Get(sessionID string) (*UssdRequestSession, error)
	PutHop(*UssdRequestSession) error
	Delete(sessionID string)
}

/ SessionStore / / Interface for storing session data

type UssdRequestSession

type UssdRequestSession struct {
	PhoneNumber string `json:"phoneNumber"`
	SessionID   string `json:"sessionID"`
	Text        string `json:"text"`
	Channel     string `json:"-"` // Deprecated - no longer being used
	NetworkCode string `json:"networkCode"`
	ServiceCode string `json:"serviceCode"`
	// The State of the request
	State int `json:"-"`
	// contains filtered or unexported fields
}

/ UssdRequestSession / / go representation of the structure of an AfricasTalking USSD call

func ParseUssdRequest

func ParseUssdRequest(req *http.Request) (*UssdRequestSession, error)

ParseUssdRequestSession

Parse the Request data to a UssdRequestSession if the parameters are present in the body

func (*UssdRequestSession) CountHops

func (u *UssdRequestSession) CountHops() int

Count Hops

Count the number of hops (interactions) for the Ussd session the number of hops is based on the asterisk count so it's approximate

func (*UssdRequestSession) GetHopN

func (u *UssdRequestSession) GetHopN(n int) string

GetHopN

Get the data provided at the nth hop

func (*UssdRequestSession) ReadIn

func (u *UssdRequestSession) ReadIn() string

ReadIn

Reads the last input string recorded for this session

func (*UssdRequestSession) RecordHop

func (u *UssdRequestSession) RecordHop(text string)

func (*UssdRequestSession) RecordHopAndReadIn

func (u *UssdRequestSession) RecordHopAndReadIn(text string) string

RecordHopAndReadIn

Records input string for the ussd session and immediately returns the new input string

func (*UssdRequestSession) SetState

func (u *UssdRequestSession) SetState(state int)

SetState

Set the state for this Ussd session

func (*UssdRequestSession) ToJSON

func (u *UssdRequestSession) ToJSON() string

ToJSON

Convert the UssdRequestSession to JSON string or empty string on error

Jump to

Keyboard shortcuts

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