slack

package module
v0.0.0-...-aa89bbc Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2015 License: MIT Imports: 11 Imported by: 2

README

Slack RTM (Real Time Messaging) API Integration

GoDoc Build Status

This package allows integration with the Slack Real Time Messaging API for consuming and publishing messages in real time to/from Slack.

To make use of the API, you will need to setup a user account or a 'bot' (robot) user (a special kind of user account specifically designed for programatic access to APIs) and obtain an authentication token from Slack.

Once you have a user account and an authentication token you can use the API to connect to slack. Here is an example:

conn, err := slack.Connect(slackToken)
	
if err != nil {
	log.Fatal(err)
}

replier := func(msg *slack.Message) {
	msg.Respond(msg.Text)
}

slack.EventProcessor(conn, replier, nil)

The above snippet of code connects to the Slack RTM API over a web socket and starts listening for all messages directed specifically at the user account used to connect to slack (either a direct message or a message in a channel preceded by '@username:' ). It will then echo the same message back to Slack.

To also process messages not directed specifically at the connected user, a similar function can be passed as the third parameter to the EventProcessor method (either in addition to or instead of the second parameter).

This package is used by Talbot, a bot that is available to be used directly, extended or simply as an example.

Features

Features implemented

  • Processing Slack message events
  • Option to respond
    • just to directed messages (those sent as private messages or preceeded by '@username:' in open channels)
    • to all messages
    • or to both directed and all messages independently.
  • Sending messages to Slack
  • Automatic reconnection following a lost connection
  • Support for explicit web proxies (running on corporate LANs)
  • Chunking of large messages into multiple smaller messages for sending to Slack
  • Updating configuration based upon new member events, etc.

To Do

Still outstanding...

  • Reliable message sending i.e. checking for Ack's for sent messages (especially upon reconnection)
  • Processing of Slack message changed events (currently ignored)
  • Processing other Slack event types

Documentation

Overview

Package slack allows integration with the Slack Real Time Messaging API (https://api.slack.com/rtm) for consuming and publishing messages in real time to/from Slack (https://slack.com).

To make use of the API, you will need to setup a user account or a 'bot' user (https://api.slack.com/bot-users) (a special kind of user account specifically designed for programatic access to APIs) and obtain an authentication token (https://api.slack.com/web#basics) from Slack.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EventProcessor

func EventProcessor(con *Connection, respond messageProcessor, hear messageProcessor)

Starts processing events on the connection from Slack and passes any messages to the hear callback and only messages addressed to the bot to the respond callback

Types

type Channel

type Channel struct {
	Id         string
	Name       string
	IsChannel  bool `json:"is_channel"`
	IsIm       bool `json:"is_im"`
	User       string
	Created    int
	Creator    string
	IsArchived bool `json:"is_archived"`
	IsGeneral  bool `json:"is_general"`
	IsMember   bool `json:"is_member"`
	Members    []string
}

type Config

type Config struct {
	Ok       bool
	Error    string
	Self     User
	Channels []Channel
	Url      string
	Users    []User
}

type Connection

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

Connection type represents the duplex websocket connection to Slack

func Connect

func Connect(token string) (*Connection, error)

Connect to Slack using the supplied authentication token

func (*Connection) Read

func (c *Connection) Read() []byte

Read the next event from the connection to Slack or block until one is available

func (*Connection) Write

func (c *Connection) Write(data []byte)

Write the specified event to Slack. The event is queued and then sent asynchronously.

type Message

type Message struct {

	// the text content of the message
	Text string

	// the name of the user whom the message is from
	From string
	// contains filtered or unexported fields
}

Message type represents a message received from Slack

func (*Message) Reply

func (m *Message) Reply(text string) error

Send a reply to the user who sent this message on the same channel it was received on

func (*Message) Respond

func (m *Message) Respond(text string) error

Send a message in a way that matches the way in which this message was received e.g. if this message was addressed then send a reply back to person who sent the message.

func (*Message) Send

func (m *Message) Send(text string) error

Send a new message on the channel this message was received on

func (*Message) Tell

func (m *Message) Tell(channel string, text string) error

Send a new message on the specified channel

type Processor

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

Processor type processes inbound events from Slack

func (*Processor) Start

func (p *Processor) Start()

Start processing events from Slack

func (*Processor) Write

func (p *Processor) Write(channel string, text string) error

Write the message on the specified channel to Slack

type User

type User struct {
	Id             string
	Name           string
	RealName       string `json:"real_name"`
	Deleted        bool
	IsAdmin        bool `json:"is_admin"`
	IsOwner        bool `json:"is_owner"`
	IsPrimaryOwner bool `json:"is_primary_owner"`
	IsBot          bool `json:"is_bot"`
	Profile        UserProfile
}

type UserProfile

type UserProfile struct {
	FirstName          string `json:"first_name"`
	LastName           string `json:"last_name"`
	RealName           string `json:"real_name"`
	Title              string
	RealNameNormalized string `json:"real_name_normalized"`
	Email              string `json:"email"`
}

Jump to

Keyboard shortcuts

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