slackcnr

package module
v0.0.0-...-cb8aef7 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2024 License: MIT Imports: 5 Imported by: 0

README

Slack Channel Name Resolver for golang

Usage

package main

import (
	"context"
	"errors"
	"log"
	"os"

	"github.com/mashiike/slackcnr"
	"github.com/slack-go/slack"
)

func main() {
	client := slack.New(os.Getenv("SLACK_BOT_TOKEN"))

	resolver := slackcnr.New(
		client,
		slackcnr.WithSearchPublicChannels(),
		slackcnr.WithExcludeArchived(),
	)
	ctx := context.Background()
	channel, err := resolver.Lookup(ctx, "general")
	if err != nil {
		if errors.Is(err, slackcnr.ErrNotFound) {
			log.Println("`general` channel not found")
			return
		}
		log.Println(err)
		return
	}
	log.Printf("channel: %s", channel.ID)
}

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNotFound = errors.New("channel not found")

Functions

This section is empty.

Types

type InMemoryStorage

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

func NewInMemoryStorage

func NewInMemoryStorage(expredDuration time.Duration) *InMemoryStorage

NewInMemoryStorage creates a new in-memory storage. if expredDuration is 0, it never expires.

func (*InMemoryStorage) GetByChannelName

func (s *InMemoryStorage) GetByChannelName(ctx context.Context, channelName string) (*slack.Channel, error)

func (*InMemoryStorage) NeedRefresh

func (s *InMemoryStorage) NeedRefresh(ctx context.Context) bool

func (*InMemoryStorage) SetChannels

func (s *InMemoryStorage) SetChannels(ctx context.Context, channels []slack.Channel) error

type Resolver

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

func New

func New(client SlackClient, optFns ...ResolverOption) *Resolver

New creates a new resolver with the provided slack client and options.

func (*Resolver) Lookup

func (r *Resolver) Lookup(ctx context.Context, channelName string) (*slack.Channel, error)

Lookup finds a channel by name.

func (*Resolver) Refresh

func (r *Resolver) Refresh(ctx context.Context) error

Refresh refreshes the cache storage with the latest channels.

type ResolverOption

type ResolverOption func(*resolverOptions)

func WithBatchSize

func WithBatchSize(size int) ResolverOption

WithBatchSize sets the batch size for users.conversations API and conversations.list API limit parameter. default is 1000.

func WithCacheStorage

func WithCacheStorage(storage Storage) ResolverOption

WithCacheStorage sets the cache storage for the resolver. default is in-memory storage.

func WithExcludeArchived

func WithExcludeArchived() ResolverOption

WithExcludeArchived excludes archived channels from the search result.

func WithRefreshOnCacheMiss

func WithRefreshOnCacheMiss() ResolverOption

WithRefreshOnCacheMiss refreshes the cache storage when a channel is not found in the cache.

func WithSearchPublicChannels

func WithSearchPublicChannels() ResolverOption

WithSearchPublicChannels enables searching public channels. with conversations.list API.

type SlackClient

type SlackClient interface {
	GetConversationsForUserContext(ctx context.Context, params *slack.GetConversationsForUserParameters) (channels []slack.Channel, nextCursor string, err error)
	GetConversationsContext(ctx context.Context, params *slack.GetConversationsParameters) (channels []slack.Channel, nextCursor string, err error)
}

type Storage

type Storage interface {
	SetChannels(ctx context.Context, channels []slack.Channel) error
	GetByChannelName(ctx context.Context, channelName string) (*slack.Channel, error)
	NeedRefresh(ctx context.Context) bool
}

Storage defines the interface for caching slack channels.

Jump to

Keyboard shortcuts

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