adapter

package
v0.4.0-dev.0 Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2019 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrAdapterNameCollision is emitted by startAdapters() if two adapters
	// have the same name.
	ErrAdapterNameCollision = errors.New("adapter name collision")

	// ErrAuthenticationFailure is emitted when an AuthenticationErrorEvent
	// is received.
	ErrAuthenticationFailure = errors.New("authentication failure")

	// ErrChannelNotFound is returned when OnChannelMessage can't find
	// information is the originating channel.
	ErrChannelNotFound = errors.New("channel not found")

	// ErrCogNotBootstrapped is returned by findOrMakeCogUser() if a user
	// attempts to trigger a command but Cog hasn't yet been bopotstrapped.
	ErrCogNotBootstrapped = errors.New("Cog hasn't been bootstrapped yet")

	// ErrSelfRegistrationOff is returned by findOrMakeCogUser() if an unknown
	// user attempts to trigger a command but self-registration is configured
	// to false.
	ErrSelfRegistrationOff = errors.New("user doesn't exist and self-registration is off")

	// ErrMultipleCommands is returned by GetCommandEntry when the same command
	// shortcut matches commands in two or more bundles.
	ErrMultipleCommands = errors.New("multiple commands match that pattern")

	// ErrNoSuchAdapter is returned by GetAdapter if a requested adapter name
	// can't be found.
	ErrNoSuchAdapter = errors.New("no such adapter")

	// ErrNoSuchCommand is returned by GetCommandEntry if a request command
	// isn't found.
	ErrNoSuchCommand = errors.New("no such bundle")

	// ErrUserNotFound is throws by several methods if a provider fails to
	// return requested user information.
	ErrUserNotFound = errors.New("user not found")
)

Functions

func GetCommandEntry

func GetCommandEntry(tokens []string) (data.CommandEntry, error)

GetCommandEntry accepts a tokenized parameter slice and returns any associated data.CommandEntry instances. If the number of matching commands is > 1, an error is returned.

func OnChannelMessage

func OnChannelMessage(event *ProviderEvent, data *ChannelMessageEvent) (*data.CommandRequest, error)

OnChannelMessage handles ChannelMessageEvent events. If a command is found in the text, it will emit a data.CommandRequest instance to the commands channel. TODO Support direct in-channel mentions.

func OnConnected

func OnConnected(event *ProviderEvent, data *ConnectedEvent)

OnConnected handles ConnectedEvent events.

func OnDirectMessage

func OnDirectMessage(event *ProviderEvent, data *DirectMessageEvent) (*data.CommandRequest, error)

OnDirectMessage handles DirectMessageEvent events.

func ScrubMarkdown

func ScrubMarkdown(text string) string

ScrubMarkdown removes unnecessary/undesirable Slack markdown (of links, of example) from text recieved from Slack.

func StartListening

func StartListening() (<-chan data.CommandRequest, chan<- data.CommandResponse, <-chan error)

StartListening instructs all relays to establish connections, receives all events from all relays, and forwards them to the various On* handler functions.

func TokenizeParameters

func TokenizeParameters(commandString string) []string

TokenizeParameters splits a command string into parameter tokens. Any trigger characters (i.e., !) are expected to have already been removed.

func TriggerCommand

func TriggerCommand(rawCommand string, adapter Adapter, channelID string, userID string) (*data.CommandRequest, error)

TriggerCommand is called by OnChannelMessage or OnDirectMessage when a valid command trigger is identified.

Types

type Adapter

type Adapter interface {
	// GetBotUser returns the info for the user associated with this bot in
	// its respective provider.
	GetBotUser() *UserInfo

	// GetChannelInfo provides info on a specific provider channel accessible
	// to the adapter.
	GetChannelInfo(channelID string) (*ChannelInfo, error)

	// GetName provides the name of this adapter as per the configuration.
	GetName() string

	// GetPresentChannels returns a slice of channels that a user is present in.
	GetPresentChannels(userID string) ([]*ChannelInfo, error)

	// GetUserInfo provides info on a specific provider channel accessible
	// to the adapter.
	GetUserInfo(userID string) (*UserInfo, error)

	// Listen causes the Adapter to ititiate a connection to its provider and
	// begin relaying back events via the returned channel.
	Listen() <-chan *ProviderEvent

	// SendErrorMessage sends an error message to a specified channel.
	// TODO Create a MessageBuilder at some point to replace this.
	SendErrorMessage(channelID string, title string, text string) error

	// SendMessage sends a standard output message to a specified channel.
	// TODO Create a MessageBuilder at some point to replace this.
	SendMessage(channel string, message string) error
}

Adapter represents a connection to a chat provider.

func GetAdapter

func GetAdapter(name string) (Adapter, error)

GetAdapter returns the requested adapter instance, if one exists. If not, an error is returned.

type AuthenticationErrorEvent

type AuthenticationErrorEvent struct {
	Msg string
}

AuthenticationErrorEvent indicates failure to authenticate

type ChannelInfo

type ChannelInfo struct {
	ID      string
	Members []string
	Name    string
}

ChannelInfo contains the basic information for a single channel in any provider.

type ChannelJoinedEvent

type ChannelJoinedEvent struct {
	Channel string
}

ChannelJoinedEvent indicates the bot has joined a channel

type ChannelMessageEvent

type ChannelMessageEvent struct {
	ChannelID string
	Text      string
	UserID    string
}

ChannelMessageEvent indicates received a message via a public or private channel (message.channels)

type ConnectedEvent

type ConnectedEvent struct {
}

ConnectedEvent indicates the client has successfully connected to the provider server

type DirectMessageEvent

type DirectMessageEvent struct {
	ChannelID string
	Text      string
	UserID    string
}

DirectMessageEvent indicates the bot has received a direct message from a user (message.im)

type DisconnectedEvent

type DisconnectedEvent struct {
	Intentional bool
}

DisconnectedEvent indicates the client has disconnected from the provider server

type ErrorEvent

type ErrorEvent struct {
	Code int
	Msg  string
}

ErrorEvent indicates an error reported by the provider. The occurs before a successful connection, Code will be unset.

func (ErrorEvent) Error

func (e ErrorEvent) Error() string

type Info

type Info struct {
	User     *UserInfo
	Provider *ProviderInfo
}

Info is used by events to wrap user and provider info.

type ProviderEvent

type ProviderEvent struct {
	// The type of event
	EventType string

	// The event instance
	Data interface{}

	// Contextual info (user, provider)
	Info *Info

	// The adapter that generated the event
	Adapter Adapter
}

ProviderEvent is the main wrapper. You will find all the other messages attached as Data

type ProviderInfo

type ProviderInfo struct {
	Type string
	Name string
}

ProviderInfo contains the basic information for a chat provider.

func NewProviderInfoFromConfig

func NewProviderInfoFromConfig(ap data.Provider) *ProviderInfo

NewProviderInfoFromConfig can create a ProviderInfo from a data.Provider instance.

func (*ProviderInfo) SetProviderInfo

func (p *ProviderInfo) SetProviderInfo(provider data.Provider) *ProviderInfo

SetProviderInfo can set a ProviderInfo from a data.Provider instance.

type SlackAdapter

type SlackAdapter struct {
	Adapter
	// contains filtered or unexported fields
}

SlackAdapter is the Slack provider implementation of a relay, which knows how to receive events from the Slack API, translate them into Cog2 events, and forward them along.

func NewSlackAdapter

func NewSlackAdapter(provider data.SlackProvider) SlackAdapter

NewSlackAdapter will construct a SlackAdapter instance for a given provider configuration.

func (SlackAdapter) GetChannelInfo

func (s SlackAdapter) GetChannelInfo(channelID string) (*ChannelInfo, error)

GetChannelInfo returns the ChannelInfo for a requested channel.

func (SlackAdapter) GetName

func (s SlackAdapter) GetName() string

GetName returns this adapter's configured name

func (SlackAdapter) GetPresentChannels

func (s SlackAdapter) GetPresentChannels(userID string) ([]*ChannelInfo, error)

GetPresentChannels returns a slice of channel ID strings that the Adapter is present in. This is expensive. Don't use it often.

func (SlackAdapter) GetUserInfo

func (s SlackAdapter) GetUserInfo(userID string) (*UserInfo, error)

GetUserInfo returns the UserInfo for a requested user.

func (SlackAdapter) Listen

func (s SlackAdapter) Listen() <-chan *ProviderEvent

Listen instructs the relay to begin listening to the provider that it's attached to. It exits immediately, returning a channel that emits ProviderEvents.

func (*SlackAdapter) OnChannelMessage

func (s *SlackAdapter) OnChannelMessage(event *slack.MessageEvent, info *Info) *ProviderEvent

OnChannelMessage is called when the Slack API emits an MessageEvent for a message in a channel.

func (*SlackAdapter) OnConnected

func (s *SlackAdapter) OnConnected(event *slack.ConnectedEvent, info *Info) *ProviderEvent

OnConnected is called when the Slack API emits a ConnectedEvent.

func (*SlackAdapter) OnConnectionError

func (s *SlackAdapter) OnConnectionError(event *slack.ConnectionErrorEvent, info *Info) *ProviderEvent

OnConnectionError is called when the Slack API emits an ConnectionErrorEvent.

func (*SlackAdapter) OnDirectMessage

func (s *SlackAdapter) OnDirectMessage(event *slack.MessageEvent, info *Info) *ProviderEvent

OnDirectMessage is called when the Slack API emits an MessageEvent for a direct message.

func (*SlackAdapter) OnDisconnected

func (s *SlackAdapter) OnDisconnected(event *slack.DisconnectedEvent, info *Info) *ProviderEvent

OnDisconnected is called when the Slack API emits a DisconnectedEvent.

func (*SlackAdapter) OnInvalidAuth

func (s *SlackAdapter) OnInvalidAuth(event *slack.InvalidAuthEvent, info *Info) *ProviderEvent

OnInvalidAuth is called when the Slack API emits an InvalidAuthEvent.

func (*SlackAdapter) OnLatencyReport

func (s *SlackAdapter) OnLatencyReport(event *slack.LatencyReport, info *Info) *ProviderEvent

OnLatencyReport is called when the Slack API emits a LatencyReport.

func (*SlackAdapter) OnMessage

func (s *SlackAdapter) OnMessage(event *slack.MessageEvent, info *Info) *ProviderEvent

OnMessage is called when the Slack API emits a MessageEvent.

func (*SlackAdapter) OnRTMError

func (s *SlackAdapter) OnRTMError(event *slack.RTMError, info *Info) *ProviderEvent

OnRTMError is called when the Slack API emits an RTMError.

func (SlackAdapter) SendErrorMessage

func (s SlackAdapter) SendErrorMessage(channelID string, title string, text string) error

SendErrorMessage will send a message (from the bot) into the specified channel.

func (SlackAdapter) SendMessage

func (s SlackAdapter) SendMessage(channelID string, text string) error

SendMessage will send a message (from the bot) into the specified channel.

type UserInfo

type UserInfo struct {
	ID                    string
	Name                  string
	DisplayName           string
	DisplayNameNormalized string
	Email                 string
	FirstName             string
	LastName              string
	RealName              string
	RealNameNormalized    string
}

UserInfo contains the basic information for a single user in any chat provider.

Jump to

Keyboard shortcuts

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