phada

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2019 License: MIT Imports: 8 Imported by: 1

README

Phada

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

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 (
    "bitbucket.org/nndi/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)
    }
    /* 
    * // If you're not using the standard lib net/http server
    session := &UssdRequestSession {
        PhoneNumber: "...", // get from the request's form data
        SessionID: "...", // get from the request's form data
        Channel: "...", // get from the request's form data
        Text: "...", // get from the request's form data
    }
    */
    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, 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 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:"channel"`
	// The State of the request
	State int `json:"state"`
	// 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