slacker

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

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

Go to latest
Published: Aug 26, 2015 License: MIT Imports: 10 Imported by: 1

README

slacker

Slacker is the most basic chat bot library, built for Slack and written in Go.

With this, you can build a bot that responds to messages with whatever functions you want.

Example

Grab the library:

go get github.com/swenson/slacker

Here is a very short, working bot in example/main.go that illustrates this library's limited capabilities.

package main

import (
  "flag"
  "fmt"

  "github.com/swenson/slacker"
)

var token = flag.String("token", "", "Slack bot token to run with")

func main() {
  flag.Parse()
  if *token == "" {
    fmt.Printf("Must specify bot token with --token=your-token\n")
    return
  }
  bot, err := slacker.NewBot(*token)
  if err != nil {
    fmt.Printf("Error connecting to Slack: %s\n", err.Error())
    return
  }

  bot.RespondWith("whats up dog", func(user string, matchParts []string) string {
    return "not much, you?"
  })

  select {} // sleep forever
}

License

MIT License.

Documentation

Index

Constants

View Source
const BaseAPI = "https://slack.com/api/"

the base URL for the Slack web API

View Source
const MaxMessageSize = 1 << 24

The maximum RTM message size we are willing to consider. Although with general WebSockets this is effectively unlimited, Slack seems to cap messages around 16 MB.

View Source
const RtmStart = "rtm.start"

the RTM start method

Variables

This section is empty.

Functions

This section is empty.

Types

type Bot

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

Bot is the basic type for defining a simple chat bot.

func NewBot

func NewBot(token string) (*Bot, error)

NewBot creates a new bot that connects to Slack with the given bot token. It will connect automatically and start processing responses in a goroutine.

func (*Bot) ClearResponses

func (b *Bot) ClearResponses()

ClearResponses can be used to delete all of the rules that a bot knows about.

func (*Bot) GetUser

func (b *Bot) GetUser(id string) (*User, error)

GetUser gets information about a user (by ID).

func (*Bot) RespondWith

func (b *Bot) RespondWith(re string, exec func(string, []string) string)

RespondWith responds to the given regular expression, if it matches anywhere in the string, by executing the given function to construct a response. The exec arguments are the user id and the array of the regular expression groups.

func (*Bot) Say

func (b *Bot) Say(channel, message string) error

Say posts a message to a given channel. The channel can be a name or an ID.

type BotInfo

type BotInfo struct {
}

BotInfo is a placeholder for now.

type Channel

type Channel struct {
	ID          string    `json:"id"`
	Name        string    `json:"name"`
	IsChannel   bool      `json:"is_channel"`
	Created     int64     `json:"created"`
	Creator     string    `json:"creator"`
	IsArchived  bool      `json:"is_archived"`
	IsGeneral   bool      `json:"is_general"`
	Members     []string  `json:"members"`
	Topic       *SetValue `json:"topic"`
	Purpose     *SetValue `json:"purpose"`
	IsMember    bool      `json:"is_member"`
	LastRead    string    `json:"last_read"`
	Latest      *Message  `json:"latest"`
	UnreadCount int64     `json:"unread_count"`
}

Channel is the Slack API's channel response object.

type Group

type Group struct {
}

Group is a placeholder for now.

type IM

type IM struct {
}

IM is a placeholder for now.

type Message

type Message map[string]interface{}

Message is a basic map that is used for JSON messages with the Slack APIs.

type Rtm

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

Rtm is a structure that holds state for a real-time messaging WebSocket connection

func RtmConnect

func RtmConnect(slack *Slack, url string) (*Rtm, error)

RtmConnect connects to the given URL, which should be an authenticated URL from the rtm.start API call.

type RtmStartResponse

type RtmStartResponse struct {
	OK       bool        `json:"ok"`
	Err      interface{} `json:"error"`
	URL      string      `json:"url"`
	Self     *User       `json:"self"`
	Team     *Team       `json:"team"`
	Users    []User      `json:"users"`
	Channels []Channel   `json:"channels"`
	Groups   []Group     `json:"groups"`
	IMs      []IM        `json:"ims"`
	Bots     []BotInfo   `json:"bots"`
}

RtmStartResponse is the JSON response from the rtm.start method.

type Rule

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

Rule is a basic type representing bot a bot response rule.

type SetValue

type SetValue struct {
	Value   string `json:"value"`
	Creator string `json:"creator"`
	LastSet int64  `json:"last_set"`
}

SetValue is a type used by Slack to represent a string value that has a creater and a set date, e.g., the topic.

type Slack

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

Slack can be used to send API calls to the Slack API, and also maintains an RTM (real-time messaging) connection.

func Connect

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

Connect uses the given token to create a Slack API object, and starts up a RTM connection.

func (*Slack) GetUser

func (s *Slack) GetUser(id string) (*User, error)

GetUser returns a Slack user's information by ID.

func (*Slack) Say

func (s *Slack) Say(channel, text string) error

Say sends the given message on the given channel. channel can be an id or a name.

func (*Slack) SayID

func (s *Slack) SayID(channel, text string)

SayID sends the given message on the given channel. Note that the channel needs to be the channel id, and not the channel name.

type Team

type Team struct {
	ID                string                 `json:"id"`
	Name              string                 `json:"name"`
	EmailDomain       string                 `json:"email_domain"`
	Domain            string                 `json:"domain"`
	MsgEditWindowMins int64                  `json:"msg_edit_window_mins"`
	OverStorageLimit  bool                   `json:"over_storage_limit"`
	Prefs             map[string]interface{} `json:"prefs"`
}

Team is the Slack API's team response object.

type User

type User struct {
	ID                string                 `json:"id"`
	Name              string                 `json:"name"`
	Deleted           bool                   `json:"deleted"`
	Color             string                 `json:"color"`
	Profile           map[string]interface{} `json:"profile"`
	IsAdmin           bool                   `json:"is_admin"`
	IsOwner           bool                   `json:"is_owner"`
	IsPrimaryOwner    bool                   `json:"is_primary_owner"`
	IsRestricted      bool                   `json:"is_restricted"`
	IsUltraRestricted bool                   `json:"is_ultra_restricted"`
	Has2fa            bool                   `json:"has_2fa"`
	HasFiles          bool                   `json:"has_files"`
}

User is the Slack API's user response object.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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