slack

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2020 License: BSD-3-Clause Imports: 8 Imported by: 2

README

Joe Bot - Slack Adapter

Connecting joe with the Slack chat application. https://github.com/go-joe/joe


This repository contains a module for the Joe Bot library.

Getting Started

This library is packaged using the new Go modules. You can get it via:

go get github.com/go-joe/slack-adapter

Example usage

In order to connect your bot to slack you can simply pass it as module when creating a new bot:

package main

import (
	"github.com/go-joe/joe"
	"github.com/go-joe/slack-adapter"
)

func main() {
	b := joe.New("example-bot",
		slack.Adapter(os.Getenv("SLACK_TOKEN")),
		…
    )
	
	b.Respond("ping", Pong)

	err := b.Run()
	if err != nil {
		b.Logger.Fatal(err.Error())
	}
}

So far the adapter will emit the following events to the robot brain:

  • joe.ReceiveMessageEvent
  • joe.UserTypingEvent

Built With

  • nlopes/slack - Slack API in Go
  • zap - Blazing fast, structured, leveled logging in Go

Contributing

The current implementation is rather minimal and there are many more features that could be implemented on the slack adapter so you are highly encouraged to contribute. If you want to hack on this repository, please read the short CONTRIBUTING.md guide first.

Versioning

We use SemVer for versioning. For the versions available, see the [tags on this repository][tags.

Authors

  • Friedrich Große - Initial work - fgrosse

See also the list of contributors who participated in this project.

License

This project is licensed under the BSD-3-Clause License - see the LICENSE file for details.

Documentation

Overview

Package slack implements a slack adapter for the joe bot library.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Adapter

func Adapter(token string, opts ...Option) joe.Module

Adapter returns a new slack Adapter as joe.Module.

Apart from the typical joe.ReceiveMessageEvent event, this adapter also emits the joe.UserTypingEvent. The ReceiveMessageEvent.Data field is always a pointer to the corresponding github.com/nlopes/slack.MessageEvent instance.

Types

type BotAdapter

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

BotAdapter implements a joe.Adapter that reads and writes messages to and from Slack.

func NewAdapter

func NewAdapter(ctx context.Context, conf Config) (*BotAdapter, error)

NewAdapter creates a new *BotAdapter that connects to Slack. Note that you will usually configure the slack adapter as joe.Module (i.e. using the Adapter function of this package).

func (*BotAdapter) Close

func (a *BotAdapter) Close() error

Close disconnects the adapter from the slack API.

func (*BotAdapter) React added in v0.7.0

func (a *BotAdapter) React(reaction reactions.Reaction, msg joe.Message) error

React implements joe.ReactionAwareAdapter by letting the bot attach the given reaction to the message.

func (*BotAdapter) RegisterAt

func (a *BotAdapter) RegisterAt(brain *joe.Brain)

RegisterAt implements the joe.Adapter interface by emitting the slack API events to the given brain.

func (*BotAdapter) Send

func (a *BotAdapter) Send(text, channelID string) error

Send implements joe.Adapter by sending all received text messages to the given slack channel ID.

type Config

type Config struct {
	Token  string
	Name   string
	Debug  bool
	Logger *zap.Logger

	// SendMsgParams contains settings that are applied to all messages sent
	// by the BotAdapter.
	SendMsgParams slack.PostMessageParameters

	// Log unknown message types as error message for debugging. This option is
	// disabled by default.
	LogUnknownMessageTypes bool

	// Listen and respond to all messages not just those directed at the Bot User.
	ListenPassive bool
}

Config contains the configuration of a BotAdapter.

type Option

type Option func(*Config) error

An Option is used to configure the slack adapter.

func WithDebug

func WithDebug(debug bool) Option

WithDebug enables debug messages of the slack client.

func WithListenPassive added in v0.9.0

func WithListenPassive() Option

WithListenPassive makes the adapter listen and respond to all messages not just those directed at it

func WithLogUnknownMessageTypes added in v0.8.0

func WithLogUnknownMessageTypes() Option

WithLogUnknownMessageTypes makes the adapter log unknown message types as error message for debugging. This option is disabled by default.

func WithLogger

func WithLogger(logger *zap.Logger) Option

WithLogger can be used to inject a different logger for the slack adapater.

func WithMessageParams added in v0.5.0

func WithMessageParams(params slack.PostMessageParameters) Option

WithMessageParams overrides the default parameters that are used when sending any message to slack.

Jump to

Keyboard shortcuts

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