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 interface{}) (handler, error)
- 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
- func UserHasRole(res *Response, role string) bool
- type Adapter
- type Auth
- type BasicAdapter
- type BasicStore
- type Envelope
- type FullHandler
- type Handler
- type Message
- type Options
- 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 ...interface{})
- 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() []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 AvailableAdapters = map[string]adapter{}
AvailableAdapters 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 NewHandler ¶
func NewHandler(h interface{}) (handler, error)
NewHandler checks whether h implements the handler interface, wrapping it in a FullHandler
func RegisterAdapter ¶
RegisterAdapter registers an adapter
func RegisterStore ¶
RegisterStore registers a new store
func UserHasRole ¶
UserHasRole determines whether the Response's user has a given role
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 Auth ¶
type Auth struct {
// contains filtered or unexported fields
}
Auth type to group authentication methods
func (*Auth) RemoveRole ¶
RemoveRole adds a role to a User
func (*Auth) UsersWithRole ¶
UsersWithRole returns a slice of Users that have a given role
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 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 Envelope ¶
Envelope contains metadata about the chat message.
func (*Envelope) SetOptions ¶
SetOptions sets the Envelope's Options
type FullHandler ¶
type FullHandler struct {
// contains filtered or unexported fields
}
FullHandler declares common functions shared by all handlers
type Handler ¶
Handler type
Example (Hear) ¶
package main import ( "github.com/danryan/hal" ) func main() { res := hal.Response{ Match: []string{}, } h := &hal.Handler{ 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) ¶
&Handler{ 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") }, }
Output:
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 Auth *Auth // contains filtered or unexported fields }
Robot receives messages from an adapter and sends them to listeners
func (*Robot) Handle ¶
func (robot *Robot) Handle(handlers ...interface{})
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