Documentation ¶
Overview ¶
Package dt offers a collection of commonly used datatypes. These datatypes are shared and used by Abot's core and shared packages as well as plugins themselves.
Index ¶
- Variables
- func GetAddress(dest *Address, db *sqlx.DB, id uint64) error
- type Address
- type Admin
- type AtomicSet
- type AuthMethod
- type Card
- type CardParams
- type City
- type Contactable
- type EventRequest
- type FlexIDType
- type HTTPRoute
- type HandlerMap
- type Location
- type Memory
- type Msg
- type Phone
- type Product
- type ProductSel
- type ProductSels
- type Purchase
- type PurchaseConfig
- type RouteHandler
- type State
- type StateMachine
- func (sm *StateMachine) GetDBConn() *sqlx.DB
- func (sm *StateMachine) GetMemory(in *Msg, k string) Memory
- func (sm *StateMachine) HasMemory(in *Msg, k string) bool
- func (sm *StateMachine) LoadState(in *Msg)
- func (sm *StateMachine) Next(in *Msg) (response string)
- func (sm *StateMachine) OnInput(in *Msg)
- func (sm *StateMachine) Reset(in *Msg)
- func (sm *StateMachine) SetDBConn(s *sqlx.DB)
- func (sm *StateMachine) SetLogger(l *log.Logger)
- func (sm *StateMachine) SetMemory(in *Msg, k string, v interface{})
- func (sm *StateMachine) SetOnReset(reset func(in *Msg))
- func (sm *StateMachine) SetState(in *Msg, label string) string
- func (sm *StateMachine) SetStates(sss ...[]State)
- func (sm *StateMachine) State() int
- type TimeRange
- type User
- func (u *User) CheckActiveAuthorization(db *sqlx.DB) (bool, error)
- func (u *User) Create(db *sqlx.DB, fidT FlexIDType, fid string) error
- func (u *User) DeleteSessions(db *sqlx.DB) error
- func (u *User) GetAddress(db *sqlx.DB, text string) (*Address, error)
- func (u *User) GetCards(db *sqlx.DB) ([]Card, error)
- func (u *User) GetEmail() string
- func (u *User) GetName() string
- func (u *User) GetPrimaryCard(db *sqlx.DB) (*Card, error)
- func (u *User) IsAuthenticated(m AuthMethod) (bool, error)
- func (u *User) SaveAddress(db *sqlx.DB, addr *Address) (uint64, error)
- func (u *User) UpdateAddressName(db *sqlx.DB, id uint64, name string) (*Address, error)
- type Vendor
- type Vocab
- type VocabFn
- type VocabHandler
Constants ¶
This section is empty.
Variables ¶
var ErrInvalidFlexIDType = errors.New("invalid flexid type")
ErrInvalidFlexIDType is returned when a FlexIDType is invalid not matching one of the pre-defined FlexIDTypes for email (1) or phone (2).
var ErrMissingFlexID = errors.New("missing flexid")
ErrMissingFlexID is returned when a FlexID is expected, but none found.
var ErrMissingFlexIDType = errors.New("missing flexidtype")
ErrMissingFlexIDType is returned when a FlexIDType is expected, but none found.
var ErrMissingUser = errors.New("missing user")
ErrMissingUser is returned when a user expected, but none found.
var ErrNoAddress = errors.New("no address")
ErrNoAddress signals that no address could be found when one was expected.
Functions ¶
Types ¶
type Address ¶
type Address struct { ID uint64 Name string Line1 string Line2 string City string State string Zip string Zip5 string Zip4 string Country string DisplayAddress string }
Address holds all relevant information in an address for presentation to the user and communication to external services, including the USPS address validation tool. Right now, an effort is only made to support US-based addresses.
type Admin ¶
type Admin struct {
// contains filtered or unexported fields
}
Admin is a special type of user that has access to Abot's admin panel.
func NewAdmin ¶
func NewAdmin() *Admin
NewAdmin returns a singleton that represents the current admin running the deployed service. Its name and email are set through the environment variables ADMIN_NAME and ADMIN_EMAIL.
type AtomicSet ¶
type AtomicSet struct {
// contains filtered or unexported fields
}
AtomicSet is a thread-safe map that acts as a Set of strings (no duplicates are possible). To initialize this struct, use NewAtomicSet().
func NewAtomicSet ¶
func NewAtomicSet() AtomicSet
NewAtomicSet returns an AtomicSet to track whether string values have been set or not in an efficient and thread-safe way.
type AuthMethod ¶
type AuthMethod int
AuthMethod allows you as the plugin developer to control the level of security required in an authentication. Select an appropriate security level depending upon your risk tolerance for fraud compared against the quality and ease of the user experience.
NOTE this is just a stub and isn't implemented TODO build the constants defining the types of AuthMethods
type Card ¶
type Card struct { ID int AddressID sql.NullInt64 Last4 string CardholderName string ExpMonth int ExpYear int Brand string ServiceToken string Zip5Hash []byte `sql:"zip5hash"` }
Card represents a credit card. Note that information such as the card number, security code and zip code are not present in this struct, since that data should never hit the server and thus can never be stored. Storage of that sensitive data is outsourced to a payment provider (initially Stripe) and is sent directly from the client to that payment provider, bypassing the server entirely.
type CardParams ¶
type CardParams struct { ServiceToken string CardholderName string Last4 string Brand string ExpMonth int ExpYear int AddressZip string }
CardParams is used when saving a new card.
type Contactable ¶
Contactable defines an interface used by dt.MailClient to send customized emails to users, vendors and admins
type EventRequest ¶
type EventRequest int
EventRequest is sent to the state machine to request safely jumping between states (directly to a specific Label) with guards checking that each new state is valid.
type FlexIDType ¶
type FlexIDType int
FlexIDType is used to identify a user when only an email, phone, or other "flexible" ID is available.
type HandlerMap ¶
HandlerMap maps HTTPRoutes (the method and URL path) to a echo router handler.
func NewHandlerMap ¶
func NewHandlerMap(rhs []RouteHandler) HandlerMap
NewHandlerMap builds a HandlerMap from a slice of RouteHandlers. This is a convenience function, since RouteHandlers directly is very verbose for plugins.
func (HandlerMap) AddRoutes ¶
func (hm HandlerMap) AddRoutes(prefix string, e *echo.Echo)
AddRoutes to the Echo router dynamically, enabling drivers to add routes to an application at runtime usually as part of their initialization. AddRoutes panics if the HTTP method in the HandlerMap is unknown (i.e. not GET, POST, PUT, PATCH, or DELETE).
type Location ¶
Location represents some location saved for a user or plugin. This is used by itsabot.org/abot/shared/knowledge to quickly retrieve either the user's last location (if recent) or request another location using the previous as a hint, e.g. "Are you still in Los Angeles?"
type Memory ¶
Memory holds a generic "memory" of Ava's usually set by a plugin, such as the current state of a plugin, selected products, results of a search, current offset in those search results, etc. Since the value is returned as a a []byte (and stored in the database in the same way), it can represent any datatype, and it's up to the plugin developer to recall which memories correspond to which datatypes.
func (Memory) Bool ¶
Bool is a helper method making it easier to perform a common use-case, converting a memory's []byte Val into bool and protecting against a common error.
type Msg ¶
type Msg struct { ID uint64 FlexID string FlexIDType int Sentence string User *User StructuredInput *nlp.StructuredInput Stems []string Plugin string State map[string]interface{} CreatedAt *time.Time // AbotSent determines if msg is from the user or Ava AbotSent bool NeedsTraining bool // Tokens breaks the sentence into words. Tokens like ,.' are treated as // individual words. Tokens []string Route string }
Msg is a message received by a user. It holds various fields that are useful for plugins which are populated by Abot core in core/process.
func (*Msg) GetLastRoute ¶
GetLastRoute for a given user so the previous plugin can be called again if no new trigger is detected.
type Product ¶
type Product struct { ID string Name string Size string Stock uint Price uint64 VendorID uint64 Category string Varietals []string Reviews []struct { Score uint Body string } }
Product represents a product result returned from ElasticSearch. Note that because it's an ElasticSearch result, it has a string ID.
type ProductSel ¶
ProductSel is a user's product selection, keeping track of both the product selected and the quantity desired.
type ProductSels ¶
type ProductSels []ProductSel
ProductSels represents a slice of product selections, adding a helper method that makes it easy to calculate the prices (subtotal, tax, shipping, and total).
type Purchase ¶
type Purchase struct { ID uint64 UserID uint64 User *User VendorID uint64 Vendor *Vendor ShippingAddress *Address ShippingAddressID sql.NullInt64 Products []string // product names ProductSels ProductSels Tax uint64 Shipping uint64 Total uint64 AvaFee uint64 CreditCardFee uint64 TransferFee uint64 VendorPayout uint64 VendorPaidAt *time.Time DeliveryExpectedAt *time.Time EmailsSentAt *time.Time CreatedAt *time.Time // contains filtered or unexported fields }
Purchase represents a user purchase and associated useful information such as a breakdown of pricing, products purchased and the time a delivery is expected.
func NewPurchase ¶
func NewPurchase(db *sqlx.DB, pc *PurchaseConfig) (*Purchase, error)
NewPurchase creates a Purchase and fills in information like a pricing breakdown automatically based on a provided PurchaseConfig.
func (*Purchase) DisplayID ¶
DisplayID returns a user-facing identifier for a purchase made that can be referenced in future communications, such as if purchased items arrive damaged.
func (*Purchase) Subtotal ¶
Subtotal is a helper function to return the purchase price before tax and shipping, i.e. only the cost of the products purchased.
func (*Purchase) UpdateEmailsSent ¶
UpdateEmailsSent records the time at which a purchase confirmation and vendor request were sent. See itsabot.org/abot/shared/task/request_auth.go:makePurchase for an example.
type PurchaseConfig ¶
type PurchaseConfig struct { *User Prices []uint64 VendorID uint64 ShippingAddress *Address ProductSels ProductSels }
PurchaseConfig is a smaller set of purchase information that plugins can use to more easily build a full Purchase.
type RouteHandler ¶
RouteHandler is a complete struct containing both an HTTPRoute and a handler.
type State ¶
type State struct { // OnEntry preprocesses and asks the user for information. If you need // to do something when the state begins, like run a search or hit an // endpoint, do that within the OnEntry function, since it's only called // once. OnEntry func(*Msg) string // OnInput sets the category in the cache/DB. Note that if invalid, this // state's Complete function will return false, preventing the user from // continuing. User messages will continue to hit this OnInput func // until Complete returns true. // // A note on error handling: errors should be logged but are not // propogated up to the user. Due to the preferred style of thin // States, you should generally avoid logging errors directly in // the OnInput function and instead log them within any called functions // (e.g. setPreference). OnInput func(*Msg) // Complete will determine if the state machine continues. If true, // it'll move to the next state. If false, the user's next response will // hit this state's OnInput function again. Complete func(*Msg) (bool, string) // SkipIfComplete will run Complete() on entry. If Complete() == true, // then it'll skip to the next state. SkipIfComplete bool // Label enables jumping directly to a State with stateMachine.SetState. // Think of it as enabling a safe goto statement. This is especially // useful when combined with a KeywordHandler, enabling a user to jump // straight to something like a "checkout" state. The state machine // checks before jumping that it has all required information before // jumping ensuring Complete() == true at all skipped states, so the // developer can be sure, for example, that the user has selected some // products and picked a shipping address before arriving at the // checkout step. In the case where one of the jumped Complete() // functions returns false, the state machine will stop at that state, // i.e. as close to the desired state as possible. Label string }
State is a collection of pre-defined functions that are run when a user reaches the appropriate state within a stateMachine.
type StateMachine ¶
type StateMachine struct { Handlers []State // contains filtered or unexported fields }
StateMachine enables plugin developers to easily build complex state machines given the constraints and use-cases of an A.I. bot. It primarily holds a slice of function Handlers, which is all possible states for a given stateMachine. The unexported variables are useful internally in keeping track of state automatically for developers and make an easy API like stateMachine.Next() possible.
func NewStateMachine ¶
func NewStateMachine(pluginName string) *StateMachine
NewStateMachine initializes a stateMachine to its starting state.
func (*StateMachine) GetDBConn ¶
func (sm *StateMachine) GetDBConn() *sqlx.DB
GetDBConn allows for accessing a stateMachine's provided database connection.
func (*StateMachine) GetMemory ¶
func (sm *StateMachine) GetMemory(in *Msg, k string) Memory
GetMemory retrieves a memory for a given key. Accessing that Memory's value is described in itsabot.org/abot/shared/datatypes/memory.go.
func (*StateMachine) HasMemory ¶
func (sm *StateMachine) HasMemory(in *Msg, k string) bool
HasMemory is a helper function to simply a common use-case, determing if some key/value has been set in Ava, i.e. if the memory exists.
func (*StateMachine) LoadState ¶
func (sm *StateMachine) LoadState(in *Msg)
LoadState upserts state into the database. If there is an existing state for a given user and plugin, the stateMachine will load it. If not, the stateMachine will insert a starting state into the database.
func (*StateMachine) Next ¶
func (sm *StateMachine) Next(in *Msg) (response string)
Next moves a stateMachine from its current state to its next state. Next handles a variety of corner cases such as reaching the end of the states, ensuring that the current state's Complete() == true, etc. It directly returns the next response of the stateMachine, whether that's the Complete() failed string or the OnEntry() string.
func (*StateMachine) OnInput ¶
func (sm *StateMachine) OnInput(in *Msg)
OnInput runs the stateMachine's current OnInput function. Most of the time this is not used directly, since Next() will automatically run this function when appropriate. It's an exported function to provide users more control over their
func (*StateMachine) Reset ¶
func (sm *StateMachine) Reset(in *Msg)
Reset the stateMachine both in memory and in the database. This also runs the programmer-defined reset function (SetOnReset) to reset memories to some starting state for running the same plugin multiple times. This is usually called from a plugin's Run() function. See plugins/ava_purchase/ava_purchase.go for an example.
func (*StateMachine) SetDBConn ¶
func (sm *StateMachine) SetDBConn(s *sqlx.DB)
SetDBConn gives a stateMachine itsabot.org/abot/shared access to a plugin's database connection. This is required even if no states require database access, since the stateMachine's current state (among other things) are peristed to the database between user requests.
func (*StateMachine) SetLogger ¶
func (sm *StateMachine) SetLogger(l *log.Logger)
SetLogger enables the logger with any plugin-defined settings to be used internally by the stateMachine. This ensures consistency in the logs of a plugin.
func (*StateMachine) SetMemory ¶
func (sm *StateMachine) SetMemory(in *Msg, k string, v interface{})
SetMemory saves to some key to some value in Ava's memory, which can be accessed by any state or plugin. Memories are stored in a key-value format, and any marshalable/unmarshalable datatype can be stored and retrieved. Note that Ava's memory is global, peristed across plugins. This enables plugins that subscribe to an agreed-upon memory API to communicate between themselves. Thus, if it's absolutely necessary that no some other plugins modify or access a memory, use a long key unlikely to collide with any other plugin's.
func (*StateMachine) SetOnReset ¶
func (sm *StateMachine) SetOnReset(reset func(in *Msg))
SetOnReset sets the OnReset function for the stateMachine, which should be called from a plugin's Run() function. See plugins/ava_purchase/ava_purchase.go for an example.
func (*StateMachine) SetState ¶
func (sm *StateMachine) SetState(in *Msg, label string) string
SetState jumps from one state to another by its label. It will safely jump forward but NO safety checks are performed on backward jumps. It's therefore up to the developer to ensure that data is still OK when jumping backward. Any forward jump will check the Complete() function of each state and get as close as it can to the desired state as long as each Complete() == true at each state.
func (*StateMachine) SetStates ¶
func (sm *StateMachine) SetStates(sss ...[]State)
SetStates takes [][]State as an argument. Note that it's a slice of a slice, which is used to enable tasks like requesting a user's shipping address, which themselves are []Slice, to be included inline when defining the states of a stateMachine. See plugins/ava_purchase/ava_purchase.go as an example.
func (*StateMachine) State ¶
func (sm *StateMachine) State() int
State returns the current state of a stateMachine. state is an unexported field to protect programmers from directly editing it. While reading state can be done through this function, changing state should happen only through the provided stateMachine API (stateMachine.Next(), stateMachine.SetState()), which allows for safely moving between states.
type User ¶
type User struct { Name string Email string Password string // temp storage prior to hashing PaymentServiceID string LocationID int ID uint64 AuthorizationID sql.NullInt64 LastAuthenticationMethod AuthMethod LastAuthenticated *time.Time Admin bool // Trainer determines whether the user has access to the training // interface and will be notified via email when new training is // required Trainer bool }
User represents a user, which is usually the user that sent a message to Abot.
func (*User) CheckActiveAuthorization ¶
CheckActiveAuthorization determines if a message to Ava was fulfilling an authorization request. RequestAuth nulls out the authorizationid once auth has been completed.
func (*User) DeleteSessions ¶
DeleteSessions removes any open sessions by the user. This enables "logging out" of the web-based client.
func (*User) GetAddress ¶
GetAddress standardizes the name of addresses for faster searching and consistent responses.
func (*User) GetPrimaryCard ¶
GetPrimaryCard retrieves the primary credit card for a specific user.
func (*User) IsAuthenticated ¶
func (u *User) IsAuthenticated(m AuthMethod) (bool, error)
IsAuthenticated confirms that the user is authenticated for a particular AuthMethod.
func (*User) SaveAddress ¶
SaveAddress of a specific user to the database.
type Vendor ¶
type Vendor struct { ID uint64 BusinessName string `sql:"businessname"` ContactName string `sql:"contactname"` ContactEmail string `sql:"contactemail"` }
Vendor represents some seller of a product or service Ava provides that is contactable via email to notify them of a new user purchase or transaction.
type Vocab ¶
type Vocab struct { Commands map[string]struct{} Objects map[string]struct{} // contains filtered or unexported fields }
Vocab maintains sets of Commands and Objects recognized by plugins as well as the functions to be performed when such Commands or Objects are found.
func NewVocab ¶
func NewVocab(vhs ...VocabHandler) *Vocab
NewVocab returns Vocab with all Commands and Objects stemmed using the Porter2 Snowball algorithm.
func (*Vocab) HandleKeywords ¶
HandleKeywords is a runs the first matching VocabFn in the sentence. For an example, see github.com/itsabot/plugin_purchase/plugin_purchase.go.
type VocabFn ¶
VocabFn is a function run when the user sends a matched vocab word as defined by a plugin. For an example, see github.com/itsabot/plugin_purchase/plugin_purchase.go. The response returned is a user-presentable string from the VocabFn.
type VocabHandler ¶
type VocabHandler struct { Fn VocabFn Trigger *nlp.StructuredInput }
VocabHandler maintains sets of Commands and Objects recognized by plugins as well as the functions to be performed when such Commands or Objects are found.