slack

package module
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Oct 3, 2018 License: MIT Imports: 7 Imported by: 1

README

botopolis Slack adapter

GoDoc Build Status Test Coverage

This is a botopolis adapter to use with Slack.

All you need to get started is a token from the Custom Bot creation page. For example usage, see example_test.go.

Documentation

Overview

Example
package main

import (
	"os"

	"github.com/botopolis/bot"
	"github.com/botopolis/slack"

	slacker "github.com/nlopes/slack"
)

func main() {
	robot := bot.New(
		slack.New(os.Getenv("SLACK_TOKEN")),
	)
	robot.Enter(func(r bot.Responder) error {
		msg := r.Message.Envelope.(*slacker.Message)

		r.Send(bot.Message{Text: "Any friend of " + msg.Inviter + " is a friend of mine"})
		return nil
	},
	)
	robot.Run()
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Adapter

type Adapter struct {
	Robot  *bot.Robot
	Client *slack.Client
	Store  Store

	BotID string
	Name  string
	// contains filtered or unexported fields
}

Adapter is the bot slack adapter it implements bot.Plugin and bot.Chat interfaces

func New

func New(secret string) *Adapter

New called with one's slack token provides a new adapter

func (*Adapter) Direct

func (a *Adapter) Direct(m bot.Message) error

Direct does the same thing as send, but also ensures the message is sent directly to the user

func (*Adapter) Load

func (a *Adapter) Load(r *bot.Robot)

Load provides the slack adapter access to the Robot's logger

func (*Adapter) Messages

func (a *Adapter) Messages() <-chan bot.Message

Messages connects to Slack's RTM API and channels messages through

func (*Adapter) React added in v0.5.0

func (a *Adapter) React(m bot.Message) error

React adds an emote to the last message sent (requires an Envelope to be set). It relies on the timestamp and channel for a message to be present

Example
package main

import (
	"os"

	"github.com/botopolis/bot"
	"github.com/botopolis/slack"

	slacker "github.com/nlopes/slack"
)

func main() {
	adapter := slack.New(os.Getenv("SLACK_TOKEN"))
	fromMessage := slacker.Message{Msg: slacker.Msg{
		Timestamp: "2020-01-03T18:23:14Z",
		Channel:   "general",
	}}
	adapter.React(bot.Message{
		Room:     "general",
		Topic:    "General conversation",
		Envelope: fromMessage,
	})
}
Output:

func (*Adapter) Reply

func (a *Adapter) Reply(m bot.Message) error

Reply does the same thing as send, but prefixes the message with <@userID>, notifying the user of the message.

Example
package main

import (
	"os"

	"github.com/botopolis/bot"
	"github.com/botopolis/slack"
)

func main() {
	adapter := slack.New(os.Getenv("SLACK_TOKEN"))
	fromMessage := bot.Message{
		Text: "Hi bot! How are you?",
		User: "ali",
		Room: "general",
	}
	adapter.Reply(bot.Message{
		Text:     "I'm well, thanks!",
		Envelope: fromMessage,
	})
}
Output:

func (*Adapter) Send

func (a *Adapter) Send(m bot.Message) error

Send send messages to Slack. If only text is provided, it uses the already open RTM connection. If slack.PostMessageParamters are provided in the message.Params field, it will send a web API request.

Example
package main

import (
	"os"

	"github.com/botopolis/bot"
	"github.com/botopolis/slack"
)

func main() {
	adapter := slack.New(os.Getenv("SLACK_TOKEN"))
	adapter.Send(bot.Message{Text: "hello!"})
}
Output:

Example (Custom)
package main

import (
	"os"

	"github.com/botopolis/bot"
	"github.com/botopolis/slack"

	slacker "github.com/nlopes/slack"
)

func main() {
	adapter := slack.New(os.Getenv("SLACK_TOKEN"))
	adapter.Send(bot.Message{Params: slacker.PostMessageParameters{
		Username: "ci",
		Attachments: []slacker.Attachment{
			{
				Color:     "danger",
				Title:     "CI Status",
				TitleLink: "http://ci.org/123",
				Fields: []slacker.AttachmentField{
					{Title: "Passed", Value: "102"},
					{Title: "Failed", Value: "3"},
				},
			},
		},
	}})
}
Output:

func (*Adapter) Topic

func (a *Adapter) Topic(m bot.Message) error

Topic uses the web API to change the topic. It prefers the message.Room and falls back to message.Extra.Channel to determine what channel's topic should be updated.

Example
package main

import (
	"os"

	"github.com/botopolis/bot"
	"github.com/botopolis/slack"
)

func main() {
	adapter := slack.New(os.Getenv("SLACK_TOKEN"))
	adapter.Topic(bot.Message{
		Room:  "general",
		Topic: "General conversation",
	})
}
Output:

func (*Adapter) Unload

func (a *Adapter) Unload(r *bot.Robot)

Unload disconnects from slack's RTM socket

func (*Adapter) Username

func (a *Adapter) Username() string

Username returns the bot's username

type Store

type Store interface {
	// Load takes slack info and adds new users and channels from it
	Load(*slack.Info)
	// Update queries Slack's web API for users and channels
	Update() error
	// UserByID queries the store for a User by ID
	UserByID(id string) (slack.User, bool)
	// UserByName queries the store for a User by Name
	UserByName(name string) (slack.User, bool)
	// UserByEmail queries the store for a User by Name
	UserByEmail(name string) (slack.User, bool)
	// ChannelByID queries the store for a Channel by ID
	ChannelByID(id string) (slack.Channel, bool)
	// ChannelByName queries the store for a Channel by Name
	ChannelByName(id string) (slack.Channel, bool)
	// IMByID queries the store for a IM by ID
	IMByID(id string) (slack.IM, bool)
	// IMByUserID queries the store for a DM by User ID
	IMByUserID(userID string) (slack.IM, bool)
}

Store is the interface to expect from adapter.Store

Example
package main

import (
	"fmt"
	"os"

	"github.com/botopolis/slack"
)

func main() {
	adapter := slack.New(os.Getenv("SLACK_TOKEN"))
	// The store is only populated if:
	// 1. You call adapter.Messages(), which connects it to RTM
	adapter.Messages()
	// 2. You call store.Update()
	adapter.Store.Update()

	// Gives access to slack.User
	if u, ok := adapter.Store.UserByName("beardroid"); ok {
		fmt.Println("Found the bot's real name: " + u.RealName)
	}

	// Gives access to slack.Channel
	if c, ok := adapter.Store.ChannelByName("general"); ok {
		fmt.Println(len(c.Members), " many people in general")
	}
}
Output:

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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