slackwatch: github.com/mikegrb/slackwatch/pkg/slackwatch Index | Examples | Files

package slackwatch

import "github.com/mikegrb/slackwatch/pkg/slackwatch"

Package slackwatch preforms configured actions when DMed on Slack. Out of the box, it expects a JSON formatted config file named .slackwatch in your home directory.

{
  "SlackToken": "xoxp-123-543",
  "Actions": [
    { "Command": "/usr/bin/afplay", "Args": "klaxon.wav" },
    { "URL": "https://hassio.local/api/services/homeassistant/turn_on?api_password=letmein",
      "Body": "{\"entity_id\":\"switch.bat_signal\"}"
    }
  ]
}

Code:

config := slackwatch.Config{
    SlackToken: "xoxp-123",
    Actions: []slackwatch.Action{
        slackwatch.CommandAction{Command: "/usr/bin/afplay", Args: "klaxon.wav"},
        slackwatch.URLAction{URL: "https://example.com/trigger_action"},
        slackwatch.URLAction{
            URL:         "https://example.com/other_endpoint",
            Body:        "Slackwatch Triggered",
            ContentType: "application/javascript",
        },
    },
}

s := slackwatch.New(config)
s.Run()

Index

Examples

Package Files

action.go alert.go commands.go config.go lookup_cache.go message.go slackwatch.go

type Action Uses

type Action interface {
    Execute(Message)
}

An Action 's execute method is called when an interesting message is received if armed.

type CommandAction Uses

type CommandAction struct {
    Command string
    Args    string
}

CommandAction specifies a command to execute on Alert

func (CommandAction) Execute Uses

func (c CommandAction) Execute(m Message)

Execute is called to run the command.

type Config Uses

type Config struct {
    SlackToken      string
    Actions         []Action
    WatchedChannels []string
}

Config is passed to the New constructor.

func NewConfigFromFile Uses

func NewConfigFromFile(path string) Config

NewConfigFromFile parses a json formatted config file and returns a Config.

Code:

config := slackwatch.NewConfigFromFile(os.Getenv("HOME") + "/.slackwatch")
s := slackwatch.New(config)
s.Run()

type Message Uses

type Message struct {
    Timestamp string
    ChannelID string // of the form C02G9FMGB
    Channel   string // without preceding #
    UserID    string // of the form U3L6DT8P3
    User      string
    Text      string // raw text of the message, you probably want DetokenizedText()
    // contains filtered or unexported fields
}

Message represents an incoming message

func (Message) DetokenizedText Uses

func (m Message) DetokenizedText() string

DetokenizedText replaces user mentions with their name instead of their ID.

func (Message) IsFromMe Uses

func (m Message) IsFromMe() bool

IsFromMe returns true if the user our API token belongs to sent the message.

func (Message) IsInteresting Uses

func (m Message) IsInteresting() bool

IsInteresting returns true if we were mentioned or this is a DM.

func (Message) IsWatchedChannel Uses

func (m Message) IsWatchedChannel() bool

IsWatchedChannel returns true if the channel is on the watch list.

func (Message) String Uses

func (m Message) String() string

String returns a string representation of the detokenized text preceded by the channel and username, suitable for logging or showing to the user. * [general] <user2> hello @user1 The initial asterisk or space indicates if the message is considered interesting, see IsInteresting.

type Slackwatch Uses

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

Slackwatch struct holds state. You should call New(config) rather than creating it yourself.

func New Uses

func New(config Config) *Slackwatch

New creates a slackwatch instance and returns a pointer to it.

func (*Slackwatch) Run Uses

func (s *Slackwatch) Run()

Run is a blocking call that makes the connection to Slack and handles incoming events.

type URLAction Uses

type URLAction struct {
    URL         string
    Body        string
    ContentType string // defaults to application/octet-stream
}

URLAction specifies an HTTP request to make on Alert. If Body is provided, an HTTP post is made, otherwise, an HTTP get.

func (URLAction) Execute Uses

func (u URLAction) Execute(m Message)

Execute is called to make the HTTP request

Package slackwatch imports 11 packages (graph) and is imported by 1 packages. Updated 2019-09-21. Refresh now. Tools for package owners.