rtm

package
v0.0.0-...-6a96c08 Latest Latest
Warning

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

Go to latest
Published: Jun 26, 2015 License: ISC Imports: 9 Imported by: 0

Documentation

Overview

Package rtm implements the Slock real-time messaging API.

Index

Constants

This section is empty.

Variables

View Source
var DefaultServeMux = NewServeMux()

DefaultServeMux is the default ServeMux and used by Serve. We are following the patterns in net/http but right now we don't build out all the underlying infrastructure - for now the default is the only mux.

Functions

func DialAndListen

func DialAndListen(token string) (err error)

DialAndListen opens a connection to the Slack RTM server and begins handling incoming events using the DefaultServeMux. The method blocks so should be called in a goroutine if other processing needs to be done. If other threads wish to send messages without being triggered by incoming events, a handler should be registered for the "hello" event. When the hello event is received the RTM connection has been received and the ResponseWriter can be saved and used to send messages.

func Handle

func Handle(pattern string, handler Handler)

Handle adds a handler for an event on the DefaultServeMux. See ServeMux documentation for usage.

func HandleFunc

func HandleFunc(pattern string, handler func(resp ResponseWriter, event interface{}))

HandleFunc adds a handler functino for an event on the DefaultServeMux. See ServeMux documentation for usage.

Types

type Client

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

Client is a Slack Real-Time Messaging (RTM) client.

Clients contain state information so they should be created instead of reused.

func (*Client) DialAndListen

func (c *Client) DialAndListen(token string, handler Handler) (err error)

DialAndListen opens a connection to the Slack RTM server and begins handling incoming events using the provided handler. The method blocks so should be called in a goroutine if other processing needs to be done. If other threads wish to send messages without being triggered by incoming events, a handler should be registered for the "hello" event. When the hello event is received the RTM connection has been received and the ResponseWriter can be saved and used to send messages.

func (*Client) Write

func (c *Client) Write(msg map[string]interface{}) (int, error)

Write sends the provided msg to the RTM server. All msgs must contain a "type" field. The "id" field will be automatically configured by the client.

func (*Client) WriteMsg

func (c *Client) WriteMsg(channel, text string) (int, error)

WriteMsg is a simple convenience for sending RTM simple text messages. The "id" field will be automatically configured by the client.

type Handler

type Handler interface {
	HandleEvent(resp ResponseWriter, event interface{})
}

Handler interface should be implemented by any object that wants to receive events for a particular event type.

HandleEvent may write zero or more responses to ResponseWriter and then return. Returning signals that the request is finished and that the event server can move on to the next request on the connection.

If HandleEvent panics, the server (the caller of HandleEvent) assumes that the effect of the panic was isolated to the active request. It recovers the panic, logs a stack trace to the server error log, and continues received events.

type HandlerFunc

type HandlerFunc func(ResponseWriter, interface{})

The HandlerFunc type is an adapter to allow the use of ordinary functions as event handlers. If f is a function with the appropriate signature, HandlerFunc(f) is a Handler object that calls f.

func (HandlerFunc) HandleEvent

func (f HandlerFunc) HandleEvent(w ResponseWriter, e interface{})

HandleEvent calls f(w, r).

type Preferences

type Preferences map[string]interface{}

Preferences contains information about the preferences set for the parent object

type ResponseWriter

type ResponseWriter interface {
	// Write sends the data to the connection as part of an RTM reply.
	// The event object must be JSON serializable.
	// Returns the number of bytes written or an error.
	Write(event map[string]interface{}) (int, error)
	// WriteMsg sends a simple RTM message. This is a simple convenience
	// for sending message objects to the RTM server.
	WriteMsg(channel, text string) (int, error)
}

ResponseWriter interface provides the methods for Handlers to write to an active rtm connection.

type Self

type Self struct {
	// ID uuid for the user e.g. "U023BECGF",
	ID string `json:"id"`
	// Name of the user e.g. "bobby"
	Name string `json:"name"`
	// Preferences for the user
	Preferences Preferences `json:"prefs"`
	// Timestamp the user's account was created e.g. 1402463766
	Created int64 `json:"created"`
	// ManualPresence indicates the presence mode for the user (active, manual)
	ManualPresence string `json:"manual_presence"`
}

Self describes the user's account

type ServeMux

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

ServeMux is an RTM event multixplexer. It matches incoming RTM events by type and calls the handler that most closely matches the pattern. Pattern matching resolves to the "best" match (most precise). Handlers that register identical patterns will be dispatched to by random.

func NewServeMux

func NewServeMux() *ServeMux

NewServeMux creates a new ServeMux.

func (*ServeMux) Handle

func (mux *ServeMux) Handle(pattern string, handler Handler)

Handle adds a Handler that will be dispatched when any event that matches the provided pattern is received.

func (*ServeMux) HandleEvent

func (mux *ServeMux) HandleEvent(resp ResponseWriter, event interface{})

HandleEvent handles any incoming event from an RTM stream. Responses may be written to the ResponseWritter (but is not required).

func (*ServeMux) HandleFunc

func (mux *ServeMux) HandleFunc(pattern string, handler func(resp ResponseWriter, event interface{}))

HandleFunc adds a handler that will be dispatched when an event that matches the provided pattern is received. The redundant functionality matches net/http and makes up for the difference in Go between anonmyous functions and interfaces.

func (*ServeMux) Handler

func (mux *ServeMux) Handler(event interface{}) (h Handler, pattern string)

Handler determines the correct handler to match a provided event. The handler return can be nil indicating no handlers are registered for the provided pattern. If the handler is non-nil the matching pattern is also returned (for debugging/testing).

type StartResponse

type StartResponse struct {
	// Ok is true if the RTM stream can begin
	Ok bool `json:"ok"`
	// Error contains an error message if Ok is false
	Error string `json:"error,omitempty"`
	// URL is the web socket to connect to (must be used within 30 sec)
	// e.g. "wss:\/\/ms9.slack-msgs.com\/websocket\/7I5yBpcvk"
	URL string `json:"url"`
}

StartResponse is received from the Slack rtm.start API.

type Team

type Team struct {
	// ID is the uuid for the team e.g. T024BE7LD
	ID string `json:"id"`
	// Name is the name of the slack team
	Name string `json:"name"`
	// EmailDomain is the slack default email domain for team members (can be empty)
	EmailDomain string `json:"email_domain"`
	// Domain is the slack domain for the current team
	Domain string `json:"domain"`
	// MsgEditWindowMins is the number of minutes for the last message to be editable or -1
	MsgEditWindowMins int `json:"msg_edit_window_mins"`
	// OverStorageLimit is true if the account is over the storage limit
	OverStorageLimit bool `json:"over_storage_limit"`
	// Preferences for the user
	Preferences Preferences `json:"prefs"`
	// Plan contains the current billing plan for the team (std, pro, etc)
	Plan string `json:"plan"`
}

Team contains information on the teams the user belongs to.

Jump to

Keyboard shortcuts

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