hal

package module
v0.0.0-...-1ff3b47 Latest Latest
Warning

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

Go to latest
Published: Aug 28, 2018 License: Apache-2.0 Imports: 13 Imported by: 0

README

HAL

This bot was originally adapted from HAL which is no longer maintained. This project should be regarded as a WIP.

Roadmap

  • Work on debug messaging
  • Sensible tests and coverage
  • Enhance documentation
  • Handlers are quit confusing for some reason

Development instructions

  1. make all
  2. Insert your slack token into run.sh
  3. chmod a+x run.sh
  4. ./run.sh

Documentation

Index

Examples

Constants

View Source
const (
	HEAR    = "HEAR"
	RESPOND = "RESPOND"
	TOPIC   = "TOPIC"
	ENTER   = "ENTER"
	LEAVE   = "LEAVE"
)

Handler constants

Variables

View Source
var (
	// Config is a global config
	Config = newConfig()
	// Logger is a global logger
	Logger = newLogger()
	// Router is a global HTTP muxer
	Router = newRouter()
	// HealthStatus is a global detailed status struct
	HealthStatus = newHealthStatus()
)
View Source
var AvailableAdapters = map[string]adapter{}

AvailableAdapters is a map of registered adapters

View Source
var Handlers = map[string]handler{}

Handlers is a map of registered handlers

View Source
var Stores = map[string]store{}

Stores is a map of registered stores

Functions

func Close

func Close() error

Close shuts down the robot. Unused?

func Enter

func Enter(fn func(res *Response) error) handler

Enter returns a new listener for Enter messages

func Hear

func Hear(pattern string, fn func(res *Response) error) handler

Hear a message

func Leave

func Leave(fn func(res *Response) error) handler

Leave creates a new listener for Leave messages

func NewHandler

func NewHandler(h interface{}) (handler, error)

NewHandler checks whether h implements the handler interface, wrapping it in a FullHandler

func RegisterAdapter

func RegisterAdapter(name string, newFunc func(*Robot) (Adapter, error))

RegisterAdapter registers an adapter

func RegisterStore

func RegisterStore(name string, newFunc func(*Robot) (Store, error))

RegisterStore registers a new store

func Respond

func Respond(pattern string, fn func(res *Response) error) handler

Respond creates a new listener for Respond messages

func Topic

func Topic(pattern string, fn func(res *Response) error) handler

Topic returns a new listener for Topic messages

func UserHasRole

func UserHasRole(res *Response, role string) bool

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

func NewAdapter(robot *Robot) (Adapter, error)

NewAdapter creates a new initialized adapter

type Auth

type Auth struct {
	// contains filtered or unexported fields
}

Auth type to group authentication methods

func NewAuth

func NewAuth(r *Robot) *Auth

NewAuth returns a pointer to an initialized Auth

func (*Auth) AddRole

func (a *Auth) AddRole(user User, r string) error

AddRole adds a role to a User

func (*Auth) Admins

func (a *Auth) Admins() (admins []User)

Admins returns a slice of admin Users

func (*Auth) HasRole

func (a *Auth) HasRole(id string, roles ...string) bool

HasRole checks whether a user located by id has a given role(s)

func (*Auth) IsAdmin

func (a *Auth) IsAdmin(user User) bool

IsAdmin checks whether a user is an admin

func (*Auth) RemoveRole

func (a *Auth) RemoveRole(user User, role string) error

RemoveRole adds a role to a User

func (*Auth) UsersWithRole

func (a *Auth) UsersWithRole(role string) (users []User)

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

type Envelope struct {
	Room    string
	User    *User
	Options map[string]interface{}
}

Envelope contains metadata about the chat message.

func (*Envelope) SetOptions

func (e *Envelope) SetOptions(opts map[string]interface{})

SetOptions sets the Envelope's Options

type FullHandler

type FullHandler struct {
	// contains filtered or unexported fields
}

FullHandler declares common functions shared by all handlers

func (*FullHandler) Handle

func (h *FullHandler) Handle(res *Response) error

Handle func

func (*FullHandler) Match

func (h *FullHandler) Match(res *Response) bool

Match func

func (*FullHandler) Regexp

func (h *FullHandler) Regexp() *regexp.Regexp

Regexp func

type Handler

type Handler struct {
	Method  string
	Pattern string
	Usage   string
	Run     func(res *Response) error
}

Handler type

Example (Hear)
package main

import (
	"github.com/mattouille/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:

func (*Handler) Handle

func (h *Handler) Handle(res *Response) error

Handle func

type Message

type Message struct {
	ID   string
	User User
	Room string
	Text string
	Type string
}

Message represents an incoming chat message.

func (*Message) String

func (msg *Message) String() string

String implements the Stringer interface

type Options

type Options map[string]interface{}

Options type

type Response

type Response struct {
	Robot    *Robot
	Envelope *Envelope
	Message  *Message
	Match    []string
}

Response struct

func NewResponse

func NewResponse(robot *Robot) *Response

NewResponse returns a new Response object

func NewResponseFromMessage

func NewResponseFromMessage(robot *Robot, msg *Message) *Response

NewResponseFromMessage returns a new Response object with an associated Message

func (*Response) Emote

func (res *Response) Emote(strings ...string) error

Emote posts an emote back to the chat source

func (*Response) Play

func (res *Response) Play(strings ...string) error

Play posts a sound message

func (*Response) Reply

func (res *Response) Reply(strings ...string) error

Reply posts a message mentioning the current user

func (*Response) Room

func (res *Response) Room() string

Room returns the Envelope room of the response's message

func (*Response) Send

func (res *Response) Send(strings ...string) error

Send posts a message back to the chat source

func (*Response) Text

func (res *Response) Text() string

Text is the text of the response's message

func (*Response) Topic

func (res *Response) Topic(strings ...string) error

Topic posts a topic changing message

func (*Response) UserID

func (res *Response) UserID() string

UserID returns the id of the Envelope's User

func (*Response) UserName

func (res *Response) UserName() string

UserName returns the id of the Envelope's User

func (*Response) UserRoles

func (res *Response) UserRoles() []string

UserRoles returns the roles of the Envelope's User

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 New

func New() (*Robot, error)

New returns a Robot instance.

func NewRobot

func NewRobot() (*Robot, error)

NewRobot returns a new Robot instance

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) Receive

func (robot *Robot) Receive(msg *Message) error

Receive dispatches messages to our handlers

func (*Robot) Run

func (robot *Robot) Run() error

Run initiates the startup process

func (*Robot) SetAdapter

func (robot *Robot) SetAdapter(adapter Adapter)

SetAdapter sets robot's adapter

func (*Robot) SetName

func (robot *Robot) SetName(name string)

SetName sets robot's name

func (*Robot) SetStore

func (robot *Robot) SetStore(store Store)

SetStore sets robot's adapter

func (*Robot) Stop

func (robot *Robot) Stop() error

Stop initiates the shutdown process

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

func NewStore

func NewStore(robot *Robot) (Store, error)

NewStore returns an initialized store

type User

type User struct {
	ID      string
	Name    string
	Roles   []string
	Options map[string]interface{}
}

User is a chat participant

func NewUser

func NewUser() *User

func (*User) Get

func (u *User) Get(k string) (interface{}, error)

type UserMap

type UserMap struct {
	Map map[string]User

	sync.Mutex
	// contains filtered or unexported fields
}

UserMap handles the known users

func NewUserMap

func NewUserMap(robot *Robot) *UserMap

NewUserMap returns an initialized UserMap

func (*UserMap) All

func (um *UserMap) All() []User

All returns the underlying map of all users

func (*UserMap) Decode

func (um *UserMap) Decode() (map[string]User, error)

Decode unmarshals a JSON object into a map of strings to Users

func (*UserMap) Encode

func (um *UserMap) Encode() ([]byte, error)

Encode marshals a UserMap to JSON

func (*UserMap) Get

func (um *UserMap) Get(id string) (User, error)

Get looks up a user by id and returns a User object

func (*UserMap) GetByName

func (um *UserMap) GetByName(name string) (User, error)

GetByName looks up a user by name and returns a User object

func (*UserMap) Load

func (um *UserMap) Load() error

Load retrieves known users from the store and populates the UserMap

func (*UserMap) Save

func (um *UserMap) Save() error

Save persists known users to the store

func (*UserMap) Set

func (um *UserMap) Set(id string, user User) error

Set adds or updates a user in the UserMap and persists it to the store

Directories

Path Synopsis
adapter
store

Jump to

Keyboard shortcuts

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