slackapp

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2024 License: MIT Imports: 7 Imported by: 0

README

slackapp

release codecov test go report card godoc license

A basic Slack Events API client for Go, using github.com/go-slack/slack

Overview

This packages provides a Go implementation of Slack's Events API, a streamlined way to build apps and bots that respond to activities in Slack.

The slackapp implementation uses Slack's Socket Mode to establish the connection, meaning apps do not need to designate a public HTTP endpoint for Slack to connect to.

Installing

go get

$ go get -u github.com/slack-go/slack

Slack configuration

To create a SlackApp, you will need to add it to your workspace. In short, this means:

  • Go to Your Apps in your workspace and "Create a New App".
  • In "Socket Mode", enable Socket Mode.
  • In "App Home", note down your App-Level Token (it starts with xapp-1)
  • In "OAuth & Permissions", set the required permissions (see manifest.yaml for a basic example) and note your Bot User OAuth Token token (it starts with xoxb-).
  • In "Events Subscriptions", enable events and subscribe to the bot events needed for your application (e.g. for a Bot, you will at least need app_mention).
  • In "Slack Commands", define any commands that your app implements.
  • Install the app in your workspace.

Writing a SlackApp client

See doc_slackapp_test.go for a basic example of a SlackApp client.

Authors

  • Christophe Lambin

License

This project is licensed under the MIT License - see the LICENSE.md file for details.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type SlackApp

type SlackApp struct {
	*socketmode.Client
	// contains filtered or unexported fields
}

A SlackApp implements Slack's Events API, using Socket Mode. It connects to Slack, listens for incoming events and makes them available using the Event channel.

Example
package main

import (
	"context"
	"github.com/clambin/slackapp"
	"github.com/slack-go/slack"
	"github.com/slack-go/slack/slackevents"
	"github.com/slack-go/slack/socketmode"
	"log/slog"
)

func main() {
	const (
		slackToken = "xoxb-token"
		appToken   = "xapp-token"
	)
	c := slack.New(slackToken, slack.OptionAppLevelToken(appToken))
	app := slackapp.NewSlackApp(c, slog.Default())
	app.AddEventHandler(string(slackevents.AppMention), func(event slackevents.EventsAPIInnerEvent, client *socketmode.Client) {
		// process the AppMention event. The details are in event.Data.(*slackevents.AppMentionEvent).
	})
	app.AddSlashCommand("/sing", func(command slack.SlashCommand, client *socketmode.Client) {
		// process the SlackCommand
	})

	_ = app.Run(context.TODO())
}
Output:

func NewSlackApp

func NewSlackApp(client *slack.Client, logger *slog.Logger) *SlackApp

NewSlackApp creates a new slackapp for the slack client.

func (*SlackApp) AddEventHandler

func (h *SlackApp) AddEventHandler(eventType string, f func(event slackevents.EventsAPIInnerEvent, client *socketmode.Client))

func (*SlackApp) AddSlashCommand

func (h *SlackApp) AddSlashCommand(cmd string, f func(slack.SlashCommand, *socketmode.Client))

func (*SlackApp) Connected

func (h *SlackApp) Connected() bool

Connected returns true if the slackapp is connected to Slack.

func (*SlackApp) Run

func (h *SlackApp) Run(ctx context.Context) error

Run starts the SlackApp. It connects to Slack, receives events and calls the associated registered handler.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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