slackbot

package module
v0.0.0-...-95c8ebd Latest Latest
Warning

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

Go to latest
Published: May 21, 2015 License: MIT Imports: 13 Imported by: 0

README

slackbot

Slackbot implementation library for Go. Easy creation of new SlackCommands and Bots.

package main

import (
    "errors"
    "log"
    "math/rand"
    "os"
    "strings"
    "sync"
    "time"

    slackbot "github.com/dutchcoders/slackbot"
    "github.com/nlopes/slack"
)

func main() {
    engine := slackbot.NewEngine(slackbot.Config{
        PayloadToken: os.Getenv("SLACK_PAYLOAD_TOKEN"),
    })

    engine.AddCommand("/pick", pick)

    go func() {
        bot, err := slackbot.NewBot(slackbot.Config{
            Token:  os.Getenv("SLACK_TOKEN"),
            Origin: "http://localhost",
        })

        if err != nil {
            log.Println(err)
            return
        }

        bot.SetMessageHandler(func(b *slackbot.Bot, message *slackbot.Message) error {
            log.Println(message.Text)
            return nil
        })

        err = bot.Run()
        if err != nil {
            log.Println(err)
        }
    }()

    addr := ":" + os.Getenv("PORT")
    if err := engine.ListenAndServe(addr); err != nil {
        panic(err)
    }

}

func pick(sc *slackbot.Context, w http.ResponseWriter) {
    choices := strings.Split(sc.Text, ",")
    choice := choices[rand.Intn(len(choices))]
    choice = strings.Trim(choice, " ")
    fmt.Fprintf(w, "Hmmm, I'd say pick %s.", choice)
}

Testing

curl -X POST --data "text=test&trigger_word={word}&command={/command}" http://127.0.0.1:5100/

Contributions

Contributions are welcome.

Creators

Remco Verhoef

Code and documentation copyright 2011-2014 Remco Verhoef. Code released under the MIT license.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNotSupported = errors.New("Not supported")

Functions

func MessageHandler

func MessageHandler(fn MessageFunc) handlerFunc

Types

type Attachment

type Attachment struct {
	Fallback    string `json:"fallback,omitempty"`
	ImageWidth  int    `json:"image_width,omitempty"`
	ImageHeight int    `json:"image_height,omitempty"`
	ImageBytes  int    `json:"image_bytes,omitempty"`
	AuthorName  string `json:"author_name,omitempty"`
	Id          int    `json:"id,omitempty"`
	TitleLink   string `json:"title_link,omitempty"`
	FromUrl     string `json:"from_url,omitempty"`
	ImageUrl    string `json:"image_url,omitempty"`
	Text        string `json:"text,omitempty"`
	Title       string `json:"title,omitempty"`
	AuthorLink  string `json:"author_link,omitempty"`
	Type        string `json:"type,omitempty"`
	Subtype     string `json:"subtype,omitempty"`
	Channel     string `json:"channel,omitempty"`
}

type Bot

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

func NewBot

func NewBot(config Config) (*Bot, error)

func (*Bot) NewMessage

func (b *Bot) NewMessage() Message

func (*Bot) Run

func (b *Bot) Run() error

func (*Bot) Send

func (b *Bot) Send(message Message) error

func (*Bot) SetHandler

func (b *Bot) SetHandler(t EventType, fn handlerFunc)

func (*Bot) SetMessageHandler

func (b *Bot) SetMessageHandler(fn MessageFunc)

type Config

type Config struct {
	PayloadToken string
	Token        string
	Origin       string
}

type Context

type Context struct {
	Token       string
	TeamID      string
	ChannelID   string
	ChannelName string
	UserName    string
	UserID      string
	Command     string
	TriggerWord string
	Text        string
}

type Engine

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

func NewEngine

func NewEngine(config Config) *Engine

func (*Engine) AddCommand

func (engine *Engine) AddCommand(cmd string, fn SlackFunc)

func (*Engine) AddHook

func (engine *Engine) AddHook(trigger string, fn SlackFunc)

func (*Engine) ListenAndServe

func (e *Engine) ListenAndServe(addr string) error

func (*Engine) ServeHTTP

func (e *Engine) ServeHTTP(w http.ResponseWriter, req *http.Request)

ServeHTTP makes the router implement the http.Handler interface.

type Event

type Event struct {
	Id        int       `json:"id,omitempty"`
	Type      string    `json:"type,omitempty"`
	Channel   string    `json:"channel,omitempty"`
	User      string    `json:"user,omitempty"`
	Timestamp Timestamp `json:"ts,omitempty"`
}

type EventType

type EventType string
const (
	EventTypeMessage                    EventType = "message"
	EventTypeHello                      EventType = "hello"
	EventTypeChannelMarked              EventType = "channel_marked"
	EventTypeChannelCreated             EventType = "channel_created"
	EventTypeChannelJoined              EventType = "channel_joined"
	EventTypeChannelLeft                EventType = "channel_left"
	EventTypeChannelDeleted             EventType = "channel_deleted"
	EventTypeChannelRename              EventType = "channel_rename"
	EventTypeChannelArchive             EventType = "channel_archive"
	EventTypeChannelUnarchive           EventType = "channel_unarchive"
	EventTypeChannelHistoryChanged      EventType = "channel_history_changed"
	EventTypeChannelImCreated           EventType = "im_created"
	EventTypeChannelImOpen              EventType = "im_open"
	EventTypeChannelImClose             EventType = "im_close"
	EventTypeChannelImMarked            EventType = "im_marked"
	EventTypeChannelImHistoryChanged    EventType = "im_history_changed"
	EventTypeChannelGroupJoined         EventType = "group_joined"
	EventTypeChannelGroupLeft           EventType = "group_left"
	EventTypeChannelGroupOpen           EventType = "group_open"
	EventTypeChannelGroupClose          EventType = "group_close"
	EventTypeChannelGroupArchive        EventType = "group_archive"
	EventTypeChannelGroupUnarchive      EventType = "group_unarchive"
	EventTypeChannelGroupRename         EventType = "group_rename"
	EventTypeChannelGroupMarked         EventType = "group_marked"
	EventTypeChannelGroupHistoryChanged EventType = "group_history_changed"
	EventTypeFileCreated                EventType = "file_created"
	EventTypeFileShared                 EventType = "file_shared"
	EventTypeFileUnshared               EventType = "file_unshared"
	EventTypeFilePublic                 EventType = "file_public"
	EventTypeFilePrivate                EventType = "file_private"
	EventTypeFileChange                 EventType = "file_change"
	EventTypeFileDeleted                EventType = "file_deleted"
	EventTypeFileCommentAdded           EventType = "file_comment_added"
	EventTypeFileCommentEdited          EventType = "file_comment_edited"
	EventTypeFileCommentDeleted         EventType = "file_comment_deleted"
	EventTypePresenceChange             EventType = "presence_change"
	EventTypeManualPresenceChange       EventType = "manual_presence_change"
	EventTypePrefChange                 EventType = "pref_change"
	EventTypeUserChange                 EventType = "user_change"
	EventTypeTeamJoin                   EventType = "team_join"
	EventTypeStarAdded                  EventType = "star_added"
	EventTypeStarRemoved                EventType = "star_removed"
	EventTypeEmojiChanged               EventType = "emoji_changed"
	EventTypeCommandsChanged            EventType = "commands_changed"
	EventTypeTeamPrefChange             EventType = "team_pref_change"
	EventTypeTeamRename                 EventType = "team_rename"
	EventTypeTeamDomainChange           EventType = "team_domain_change"
	EventTypeEmailDomainChanged         EventType = "email_domain_changed"
	EventTypeBotAdded                   EventType = "bot_added"
	EventTypeBotChanged                 EventType = "bot_changed"
	EventTypeAccountsChanged            EventType = "accounts_changed"
	EventTypeTeamMigrationStarted       EventType = "team_migration_started"
)

type Message

type Message struct {
	Id          int          `json:"id,omitempty"`
	Type        string       `json:"type,omitempty"`
	Channel     string       `json:"channel,omitempty"`
	User        string       `json:"user,omitempty"`
	Username    string       `json:"username,omitempty"`
	BotId       string       `json:"bot_id,omitempty"`
	Text        string       `json:"text,omitempty"`
	Timestamp   Timestamp    `json:"ts,omitempty"`
	Attachments []Attachment `json:"attachments,omitempty"`
}

type MessageFunc

type MessageFunc func(*Bot, *Message) error

type SlackFunc

type SlackFunc func(sc *Context, w http.ResponseWriter)

type Timestamp

type Timestamp time.Time

func (*Timestamp) MarshalJSON

func (t *Timestamp) MarshalJSON() ([]byte, error)

func (*Timestamp) UnmarshalJSON

func (t *Timestamp) UnmarshalJSON(b []byte) error

Jump to

Keyboard shortcuts

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