slack

package
v0.0.0-...-650939c Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2017 License: MPL-2.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Clients = cmap.New()

This data structure holds all clients that are connected to Slack's RealTime API keyed by Team ID

Functions

func InitClients

func InitClients()

InitClients initializes all clients which are present in Redis on boot. This looks for all hashes in the key RELAX_BOTS_KEY and calls NewClient for each hash present in the redis key. It also starts a loop to listen to REDIS_BOTS_PUBSUB when new clients need to be started. It listens to Redis on pubsub instead of a queue because there can be multiple instances of Relax running and they all need to start a Slack client.

Types

type Action

type Action struct {
	Name    string        `json:"name"`
	Text    string        `json:"text"`
	Type    string        `json:"type"`
	Style   string        `json:"style"`
	Value   string        `json:"value"`
	Confirm ConfirmAction `json:"confirm"`
}

type Attachment

type Attachment struct {
	Fallback       string   `json:"fallback"`
	Color          string   `json:"color"`
	Pretext        string   `json:"pretext"`
	AuthorName     string   `json:"author_name"`
	AuthorLink     string   `json:"author_link"`
	AuthorIcon     string   `json:"author_icon"`
	Title          string   `json:"title"`
	TitleLink      string   `json:"title_link"`
	Text           string   `json:"text"`
	Fields         []Field  `json:"fields"`
	ImageUrl       string   `json:"image_url"`
	ThumbUrl       string   `json:"thumb_url"`
	Footer         string   `json:"footer"`
	FooterIcon     string   `json:"footer_icon"`
	Ts             int64    `json:"ts"`
	AttachmentType string   `json:"attachment_type"`
	CallbackId     string   `json:"callback_id"`
	Actions        []Action `json:"actions"`
}

type Channel

type Channel struct {
	Id        string `json:"id"`
	Created   int64  `json:"created"`
	Name      string `json:"name"`
	CreatorId string `json:"creator"`

	Im bool
}

Channel represents a channel in Slack

type Client

type Client struct {
	Token     string `json:"token"`
	TeamId    string `json:"team_id"`
	Provider  string `json:"provider"`
	Namespace string `json:"namespace"`
	// contains filtered or unexported fields
}

Client is the backbone of this entire project and is used to make connections to the Slack API, and send response events back to the user

func NewClient

func NewClient(initJSON string) (*Client, error)

NewClient initializes a new client that connects to Slack's RealTime API The client is initialized by JSON that looks like this: {"token":"xoxo_token","team_id":"team_id","provider":"slack"} where token is the token used by the bot, team_id is the ID for the team and provider is "slack"

func (*Client) IncrementHeartBeatsMissed

func (c *Client) IncrementHeartBeatsMissed()

func (*Client) Login

func (c *Client) Login() error

Login calls the "rtm.start" Slack API and gets a bunch of information such as the websocket URL to connect to, users and channel information for the team and so on

func (*Client) LoginAndStart

func (c *Client) LoginAndStart() error

LoginAndStarts is a simple helper function that calls login and start successively so that it can be invoked in a goroutine (as is done in InitClients)

func (*Client) ResetHeartBeatsMissed

func (c *Client) ResetHeartBeatsMissed()

func (*Client) Start

func (c *Client) Start() error

Start starts a websocket connection to Slack's servers and starts listening for messages If it detects an "invalid_auth" or "inactive_account" message, it means that the token provided by the user has expired or is incorrect and so it sends a "disable_bot" event back to the user so that they can take remedial action.

func (*Client) Stop

func (c *Client) Stop() error

Stop closes the websocket connection to Slack's websocket servers

type Command

type Command struct {
	Id        string `json:"id"`
	Type      string `json:"type"`
	TeamId    string `json:"team_id"`
	UserId    string `json:"user_id"`
	ChannelId string `json:"channel_id"`
	Namespace string `json:"namespace"`
	Payload   string `json:"payload"`
}

type ConfirmAction

type ConfirmAction struct {
	Title       string `json:"title"`
	Text        string `json:"text"`
	OkText      string `json:"ok_text"`
	DismissText string `json:"dismiss_text"`
}

type Event

type Event struct {
	Type           string       `json:"type"`
	UserUid        string       `json:"user_uid"`
	ChannelUid     string       `json:"channel_uid"`
	TeamUid        string       `json:"team_uid"`
	Im             bool         `json:"im"`
	Text           string       `json:"text"`
	RelaxBotUid    string       `json:"relax_bot_uid"`
	Timestamp      string       `json:"timestamp"`
	Provider       string       `json:"provider"`
	EventTimestamp string       `json:"event_timestamp"`
	Namespace      string       `json:"namespace"`
	Attachments    []Attachment `json:"attachments"`
}

Event represents an event that is to be consumed by the user, for e.g. when a message is received, an emoji reaction is added, etc. an event is sent back to the user.

type Field

type Field struct {
	Title string `json:"title"`
	Value string `json:"value"`
	Short bool   `json:"short"`
}

type Im

type Im struct {
	Id        string `json:"id"`
	Created   int64  `json:"created"`
	CreatorId string `json:"user"`
}

Im represents an IM (Direct Message) channel on Slack

type Message

type Message struct {
	Id               string       `json:"id"`
	Type             string       `json:"type"`
	Subtype          string       `json:"subtype"`
	Text             string       `json:"text"`
	Timestamp        string       `json:"ts"`
	DeletedTimestamp string       `json:"deleted_ts"`
	Reaction         string       `json:"reaction"`
	Hidden           bool         `json:"hidden"`
	Attachments      []Attachment `json:"attachments"`
	// For some events, such as message_changed, message_deleted, etc.
	// the Timestamp field contains the timestamp of the original message
	// so to make sure only one instance of the event is sent to REDIS_QUEUE_WEB
	// only once, and will be used by the `shouldSendToBot` function
	EventTimestamp string `json:"event_ts"`

	ReplyTo string `json:"reply_to"`
	User    User
	Channel Channel

	RawUser    json.RawMessage `json:"user"`
	RawChannel json.RawMessage `json:"channel"`
	RawMessage json.RawMessage `json:"message"`
	RawItem    json.RawMessage `json:"item"`
}

Message represents a message on Slack

func (*Message) ChannelId

func (m *Message) ChannelId() string

func (*Message) EmbeddedItem

func (m *Message) EmbeddedItem() *Message

func (*Message) EmbeddedMessage

func (m *Message) EmbeddedMessage() *Message

func (*Message) UserId

func (m *Message) UserId() string

type Metadata

type Metadata struct {
	Ok           bool      `json:"ok"`
	Self         User      `json:"self"`
	Url          string    `json:"url"`
	ImsList      []Im      `json:"ims"`
	ChannelsList []Channel `json:"channels"`
	GroupsList   []Channel `json:"groups"`
	UsersList    []User    `json:"users"`
	Error        string    `json:"error"`

	Users    map[string]User
	Channels map[string]Channel
}

Metadata contains data about a Client, such as whether it has been authenticated for e.g. Ok == true means a connection has been made, as well as a map of channels, users and IMs

type Payload

type Payload struct {
	Message  string `json:"message"`
	ImageUrl string `json:"image_url"`
}

type User

type User struct {
	Id                  string `json:"id"`
	Name                string `json:"name"`
	Color               string `json:"color"`
	Timezone            string `json:"tz"`
	TimezoneDescription string `json:"tz_label"`
	TimezoneOffset      int64  `json:"tz_offset"`
	IsDeleted           bool   `json:"deleted"`
	IsAdmin             bool   `json:"is_admin"`
	IsBot               bool   `json:"is_bot"`
	IsOwner             bool   `json:"is_owner"`
	IsPrimaryOwner      bool   `json:"is_primary_owner"`
	IsRestricted        bool   `json:"is_restricted"`
}

User represents a user on Slack

Jump to

Keyboard shortcuts

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