Codebase list golang-github-nlopes-slack / debian/0.1.0-1 websocket.go
debian/0.1.0-1

Tree @debian/0.1.0-1 (Download .tar.gz)

websocket.go @debian/0.1.0-1

1b96f65
 
 
d3cfd25
32bffe6
1b96f65
 
 
 
 
b02dfc6
6d7bb20
b02dfc6
f452f1c
b02dfc6
 
16288f9
650aeee
d8daca0
7cff306
16288f9
d3cfd25
 
676c3c4
334c7e2
 
065c48f
16288f9
d3cfd25
 
 
eb27a01
 
648871c
650aeee
16288f9
 
82e7260
 
 
7cff306
 
 
 
 
1b96f65
 
7cff306
 
 
 
 
 
 
 
 
 
334c7e2
 
82e7260
16288f9
eb27a01
32bffe6
 
d3cfd25
 
82e7260
 
16288f9
 
20e9aa8
82e7260
 
 
 
 
 
 
16288f9
 
82e7260
 
16288f9
 
 
 
9a9d871
16288f9
 
9a9d871
 
16288f9
1b96f65
package slack

import (
	"encoding/json"
	"errors"
	"time"

	"golang.org/x/net/websocket"
)

const (
	// MaxMessageTextLength is the current maximum message length in number of characters as defined here
	// https://api.slack.com/rtm#limits
	MaxMessageTextLength = 4000
)

// RTM represents a managed websocket connection. It also supports
// all the methods of the `Client` type.
//
// Create this element with Client's NewRTM() or NewRTMWithOptions(*RTMOptions)
type RTM struct {
	idGen IDGenerator
	pings map[int]time.Time

	// Connection life-cycle
	conn             *websocket.Conn
	IncomingEvents   chan RTMEvent
	outgoingMessages chan OutgoingMessage
	killChannel      chan bool
	forcePing        chan bool
	rawEvents        chan json.RawMessage
	wasIntentional   bool
	isConnected      bool

	// Client is the main API, embedded
	Client
	websocketURL string

	// UserDetails upon connection
	info *Info

	// useRTMStart should be set to true if you want to use
	// rtm.start to connect to Slack, otherwise it will use
	// rtm.connect
	useRTMStart bool
}

// RTMOptions allows configuration of various options available for RTM messaging
//
// This structure will evolve in time so please make sure you are always using the
// named keys for every entry available as per Go 1 compatibility promise adding fields
// to this structure should not be considered a breaking change.
type RTMOptions struct {
	// UseRTMStart set to true in order to use rtm.start or false to use rtm.connect
	// As of 11th July 2017 you should prefer setting this to false, see:
	// https://api.slack.com/changelog/2017-04-start-using-rtm-connect-and-stop-using-rtm-start
	UseRTMStart bool
}

// Disconnect and wait, blocking until a successful disconnection.
func (rtm *RTM) Disconnect() error {
	if !rtm.isConnected {
		return errors.New("Invalid call to Disconnect - Slack API is already disconnected")
	}
	rtm.killChannel <- true
	return nil
}

// Reconnect only makes sense if you've successfully disconnectd with Disconnect().
func (rtm *RTM) Reconnect() error {
	logger.Println("RTM::Reconnect not implemented!")
	return nil
}

// GetInfo returns the info structure received when calling
// "startrtm", holding all channels, groups and other metadata needed
// to implement a full chat client. It will be non-nil after a call to
// StartRTM().
func (rtm *RTM) GetInfo() *Info {
	return rtm.info
}

// SendMessage submits a simple message through the websocket.  For
// more complicated messages, use `rtm.PostMessage` with a complete
// struct describing your attachments and all.
func (rtm *RTM) SendMessage(msg *OutgoingMessage) {
	if msg == nil {
		rtm.Debugln("Error: Attempted to SendMessage(nil)")
		return
	}

	rtm.outgoingMessages <- *msg
}