Documentation
¶
Index ¶
- Constants
- Variables
- func Close() error
- func Enter(fn func(res *Response) error) handler
- func Hear(pattern string, fn func(res *Response) error) handler
- func Leave(fn func(res *Response) error) handler
- func NewHandler(h handler) handler
- func RegisterAdapter(name string, newFunc func(*Robot) (Adapter, error))
- func RegisterStore(name string, newFunc func(*Robot) (Store, error))
- func Respond(pattern string, fn func(res *Response) error) handler
- func Topic(pattern string, fn func(res *Response) error) handler
- type Adapter
- type BasicAdapter
- type BasicHandler
- type BasicStore
- type Envelope
- type Handler
- type Message
- type Response
- func (res *Response) Emote(strings ...string) error
- func (res *Response) Play(strings ...string) error
- func (res *Response) Reply(strings ...string) error
- func (res *Response) Room() string
- func (res *Response) Send(strings ...string) error
- func (res *Response) Text() string
- func (res *Response) Topic(strings ...string) error
- func (res *Response) UserID() string
- func (res *Response) UserName() string
- func (res *Response) UserRoles() []string
- type Robot
- func (robot *Robot) Handle(handlers ...handler)
- func (robot *Robot) Handlers() []handler
- func (robot *Robot) Receive(msg *Message) error
- func (robot *Robot) Run() error
- func (robot *Robot) SetAdapter(adapter Adapter)
- func (robot *Robot) SetName(name string)
- func (robot *Robot) SetStore(store Store)
- func (robot *Robot) Stop() error
- type Store
- type User
- type UserMap
- func (um *UserMap) All() map[string]User
- func (um *UserMap) Decode() (map[string]User, error)
- func (um *UserMap) Encode() ([]byte, error)
- func (um *UserMap) Get(id string) (User, error)
- func (um *UserMap) GetByName(name string) (User, error)
- func (um *UserMap) Load() error
- func (um *UserMap) Save() error
- func (um *UserMap) Set(id string, user User) error
Examples ¶
Constants ¶
const ( HEAR = "HEAR" RESPOND = "RESPOND" TOPIC = "TOPIC" ENTER = "ENTER" LEAVE = "LEAVE" )
Handler constants
Variables ¶
var ( // Config is a global config Config = newConfig() // Logger is a global logger Logger = newLogger() // Router is a global HTTP muxer Router = newRouter() )
var Adapters = map[string]adapter{}
Adapters is a map of registered adapters
var Handlers = map[string]handler{}
Handlers is a map of registered handlers
var Stores = map[string]store{}
Stores is a map of registered stores
Functions ¶
func RegisterAdapter ¶
RegisterAdapter registers an adapter
func RegisterStore ¶
RegisterStore registers a new store
Types ¶
type Adapter ¶
type Adapter interface {
// New() (Adapter, error)
Run() error
Stop() error
Receive(*Message) error
Send(*Response, ...string) error
Emote(*Response, ...string) error
Reply(*Response, ...string) error
Topic(*Response, ...string) error
Play(*Response, ...string) error
String() string
}
Adapter interface
func NewAdapter ¶
NewAdapter creates a new initialized adapter
type BasicAdapter ¶
type BasicAdapter struct {
*Robot
}
BasicAdapter declares common functions shared by all adapters
func (*BasicAdapter) SetRobot ¶
func (a *BasicAdapter) SetRobot(r *Robot)
SetRobot sets the adapter's Robot
func (*BasicAdapter) String ¶
func (a *BasicAdapter) String() string
type BasicHandler ¶
type BasicHandler struct {
Method string
Pattern string
Usage string
Run func(res *Response) error
}
BasicHandler is used to construct handlers that are low complexity and may not benefit from creating a custom handler type.
Example (Hear) ¶
package main
import (
"github.com/danryan/hal"
)
func main() {
res := hal.Response{
Match: []string{},
}
h := &hal.BasicHandler{
Method: hal.HEAR,
Pattern: `echo (.+)`,
Usage: "echo <string> - repeats <string> back",
Run: func(res *hal.Response) error {
res.Send(res.Match[1])
},
}
}
Output: > echo foo bar baz foo bar baz
Example (Respond) ¶
&BasicHandler{
Method: hal.RESPOND,
Pattern: `(?i)ping`, // (?i) is a flag that makes the match case insensitive
Usage: `hal ping - replies with "PONG"`,
Run: func(res *hal.Response) error {
res.Send("PONG")
},
}
func (*BasicHandler) Handle ¶
func (h *BasicHandler) Handle(res *Response) error
Handle implements the hal.Handler interface
type BasicStore ¶
type BasicStore struct {
Robot *Robot
}
BasicStore struct to be embedded in other stores
func (*BasicStore) SetRobot ¶
func (s *BasicStore) SetRobot(r *Robot)
SetRobot sets the adapter's Robot
func (*BasicStore) String ¶
func (s *BasicStore) String() string
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler declares common functions shared by all handlers
type Response ¶
Response struct
func NewResponse ¶
NewResponse returns a new Response object
func NewResponseFromMessage ¶
NewResponseFromMessage returns a new Response object with an associated Message
type Robot ¶
type Robot struct {
Name string
Alias string
Adapter Adapter
Store Store
Users *UserMap
// contains filtered or unexported fields
}
Robot receives messages from an adapter and sends them to listeners
func (*Robot) Handle ¶
func (robot *Robot) Handle(handlers ...handler)
Handle registers a new handler with the robot
func (*Robot) Handlers ¶
func (robot *Robot) Handlers() []handler
Handlers returns the robot's handlers
func (*Robot) SetAdapter ¶
SetAdapter sets robot's adapter
type Store ¶
type Store interface {
Open() error
Close() error
Get(string) ([]byte, error)
Set(key string, data []byte) error
Delete(string) error
}
Store interface for storage backends to implement
type UserMap ¶
UserMap handles the known users