rtm

package
v0.0.0-...-1aaae71 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2019 License: MIT Imports: 12 Imported by: 0

README

github.com/lestrrat-go/slack/rtm

Slack RTM Client

Synopsis

Simple RTM Client:

package rtm_test

import (
  "context"
  "fmt"

  "github.com/lestrrat-go/slack/rtm"
  "github.com/lestrrat-go/slack"
)

func processMessageEvent(e *rtm.MessageEvent) {
  // Dummy
}

func ExampleClient() {
  ctx, cancel := context.WithCancel(context.Background())
  defer cancel()

  // Create a new Slack REST client. This is required,
  // as the RTM client needs to hit the rtm.start API
  // endpoint in order to initiate a websocket session
  restcl := slack.New(slackToken)

  // Create a new Slack RTM client. Use `Run()` to start
  // listening to incoming events. If you need to stop the
  // RTM client, use the cancel function for the context.Context
  // object that you passed
  rtmcl := rtm.New(restcl)
  go rtmcl.Run(ctx)

  // Now, consume your events. They come from a channel.
  // If you care about properly bailing out of a processing
  // loop like below, you should be using select {} on the
  // channel returned by `rtmcl.Events()`
  for e := range rtmcl.Events() {
    // Event types are listed in event_types.go file, which
    // is auto-generated from Slack's web page describing
    // the events.
    switch typ := e.Type(); typ {
    case rtm.MessageType:
      // It's a message. Event objects carry a unique payload
      // depending on the event type, so you will have to do
      // a type assertion.
      // TODO: come up with a mapping of Type -> Data's underlying
      // type, so it's easier for the users to see
      processMessageEvent(e.Data().(*rtm.MessageEvent))
    default:
      fmt.Printf("Unhandled event: %s", typ)
    }
  }
}

Documentation

Index

Examples

Constants

View Source
const (
	AccountsChangedTypeKey       = "accounts_changed"
	BotAddedTypeKey              = "bot_added"
	BotChangedTypeKey            = "bot_changed"
	ChannelArchiveTypeKey        = "channel_archive"
	ChannelCreatedTypeKey        = "channel_created"
	ChannelDeletedTypeKey        = "channel_deleted"
	ChannelHistoryChangedTypeKey = "channel_history_changed"
	ChannelJoinedTypeKey         = "channel_joined"
	ChannelLeftTypeKey           = "channel_left"
	ChannelMarkedTypeKey         = "channel_marked"
	ChannelRenameTypeKey         = "channel_rename"
	ChannelUnarchiveTypeKey      = "channel_unarchive"
	CommandsChangedTypeKey       = "commands_changed"
	DndUpdatedTypeKey            = "dnd_updated"
	DndUpdatedUserTypeKey        = "dnd_updated_user"
	EmailDomainChangedTypeKey    = "email_domain_changed"
	EmojiChangedTypeKey          = "emoji_changed"
	ErrorTypeKey                 = "error"
	FileChangeTypeKey            = "file_change"
	FileCommentAddedTypeKey      = "file_comment_added"
	FileCommentDeletedTypeKey    = "file_comment_deleted"
	FileCommentEditedTypeKey     = "file_comment_edited"
	FileCreatedTypeKey           = "file_created"
	FileDeletedTypeKey           = "file_deleted"
	FilePublicTypeKey            = "file_public"
	FileSharedTypeKey            = "file_shared"
	FileUnsharedTypeKey          = "file_unshared"
	GoodbyeTypeKey               = "goodbye"
	GroupArchiveTypeKey          = "group_archive"
	GroupCloseTypeKey            = "group_close"
	GroupHistoryChangedTypeKey   = "group_history_changed"
	GroupJoinedTypeKey           = "group_joined"
	GroupLeftTypeKey             = "group_left"
	GroupMarkedTypeKey           = "group_marked"
	GroupOpenTypeKey             = "group_open"
	GroupRenameTypeKey           = "group_rename"
	GroupUnarchiveTypeKey        = "group_unarchive"
	HelloTypeKey                 = "hello"
	ImCloseTypeKey               = "im_close"
	ImCreatedTypeKey             = "im_created"
	ImHistoryChangedTypeKey      = "im_history_changed"
	ImMarkedTypeKey              = "im_marked"
	ImOpenTypeKey                = "im_open"
	ManualPresenceChangeTypeKey  = "manual_presence_change"
	MessageTypeKey               = "message"
	PinAddedTypeKey              = "pin_added"
	PinRemovedTypeKey            = "pin_removed"
	PrefChangeTypeKey            = "pref_change"
	PresenceChangeTypeKey        = "presence_change"
	PongTypeKey                  = "pong"
	ReactionAddedTypeKey         = "reaction_added"
	ReactionRemovedTypeKey       = "reaction_removed"
	ReconnectURLTypeKey          = "reconnect_url"
	StarAddedTypeKey             = "star_added"
	StarRemovedTypeKey           = "star_removed"
	SubteamCreatedTypeKey        = "subteam_created"
	SubteamSelfAddedTypeKey      = "subteam_self_added"
	SubteamSelfRemovedTypeKey    = "subteam_self_removed"
	SubteamUpdatedTypeKey        = "subteam_updated"
	TeamDomainChangeTypeKey      = "team_domain_change"
	TeamJoinTypeKey              = "team_join"
	TeamMigrationStartedTypeKey  = "team_migration_started"
	TeamPlanChangeTypeKey        = "team_plan_change"
	TeamPrefChangeTypeKey        = "team_pref_change"
	TeamProfileChangeTypeKey     = "team_profile_change"
	TeamProfileDeleteTypeKey     = "team_profile_delete"
	TeamProfileReorderTypeKey    = "team_profile_reorder"
	TeamRenameTypeKey            = "team_rename"
	UserChangeTypeKey            = "user_change"
	UserTypingTypeKey            = "user_typing"
)

These constants match the event types generated by Slack

Variables

This section is empty.

Functions

This section is empty.

Types

type ChannelCreated

type ChannelCreated struct {
	Created            objects.EpochTime `json:"created"`
	ID                 string            `json:"id"`
	IsIM               bool              `json:"is_im"`
	IsOpen             bool              `json:"is_open"`
	IsOrgShared        bool              `json:"is_org_shared"`
	LastRead           string            `json:"last_read"`
	Latest             *objects.Message  `json:"latest"`
	UnreadCount        int               `json:"unread_count"`
	UnreadCountDisplay int               `json:"unread_count_display"`
	User               string            `json:"user"`
}

type ChannelCreatedEvent

type ChannelCreatedEvent struct {
	Channel        ChannelCreated `json:"channel"`
	EventTimestamp string         `json:"event_ts"`
}

type ChannelJoinedEvent

type ChannelJoinedEvent struct {
	Channel        *objects.Channel `json:"channel"`
	EventTimestamp string           `json:"event_ts"`
}

type Client

type Client struct {
	// contains filtered or unexported fields
}
Example
package main

import (
	"context"
	"fmt"

	"github.com/lestrrat-go/slack"
	"github.com/lestrrat-go/slack/rtm"
)

func processMessageEvent(e *rtm.MessageEvent) {
	// Dummy
}

func main() {
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	// Create a new Slack REST client. This is required,
	// as the RTM client needs to hit the rtm.start API
	// endpoint in order to initiate a websocket session
	restcl := slack.New(slackToken)

	// Create a new Slack RTM client. Use `Run()` to start
	// listening to incoming events. If you need to stop the
	// RTM client, use the cancel function for the context.Context
	// object that you passed
	rtmcl := rtm.New(restcl)
	go rtmcl.Run(ctx)

	// Now, consume your events. They come from a channel.
	// If you care about properly bailing out of a processing
	// loop like below, you should be using select {} on the
	// channel returned by `rtmcl.Events()`
	for e := range rtmcl.Events() {
		// Event types are listed in event_types.go file, which
		// is auto-generated from Slack's web page describing
		// the events.
		switch typ := e.Type(); typ {
		case rtm.MessageType:
			// It's a message. Event objects carry a unique payload
			// depending on the event type, so you will have to do
			// a type assertion.
			// TODO: come up with a mapping of Type -> Data's underlying
			// type, so it's easier for the users to see
			processMessageEvent(e.Data().(*rtm.MessageEvent))
		default:
			fmt.Printf("Unhandled event: %s", typ)
		}
	}
}
Output:

func New

func New(cl *slack.Client, options ...Option) *Client

func (*Client) Events

func (c *Client) Events() <-chan *Event

func (*Client) Run

func (c *Client) Run(octx context.Context) error

Run starts the RTM run loop.

type DesktopNotificationEvent

type DesktopNotificationEvent struct {
	AvatarImage    string `json:"avatar_image"`
	Channel        string `json:"channel"`
	Content        string `json:"content"`
	EventTimestamp string `json:"event_ts"`
	ImageURI       string `json:"image_uri"`
	IsShared       bool   `json:"is_shared"`
	LaunchURI      string `json:"launch_uri"`
	Msg            string `json:"msg"`
	Title          string `json:"title"`
	SsbFilename    string `json:"ssb_filename"`
	Subtitle       string `json:"subtitle"`
}

This event is... not in the events list... (as of Apr 3, 2017) https://api.slack.com/events

type ErrorEvent

type ErrorEvent struct {
	Message string `json:"msg"`
	Code    int    `json:"code"`
}

type Event

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

func (*Event) Data

func (e *Event) Data() interface{}

func (*Event) Type

func (e *Event) Type() EventType

func (*Event) UnmarshalJSON

func (e *Event) UnmarshalJSON(data []byte) error

type EventType

type EventType int
const (
	InvalidEventType            EventType = iota
	ClientConnectingEventType             // internal
	ClientDisconnectedEventType           // internal
	AccountsChangedType
	BotAddedType
	BotChangedType
	ChannelArchiveType
	ChannelCreatedType
	ChannelDeletedType
	ChannelHistoryChangedType
	ChannelJoinedType
	ChannelLeftType
	ChannelMarkedType
	ChannelRenameType
	ChannelUnarchiveType
	CommandsChangedType
	DndUpdatedType
	DndUpdatedUserType
	EmailDomainChangedType
	EmojiChangedType
	ErrorType
	FileChangeType
	FileCommentAddedType
	FileCommentDeletedType
	FileCommentEditedType
	FileCreatedType
	FileDeletedType
	FilePublicType
	FileSharedType
	FileUnsharedType
	GoodbyeType
	GroupArchiveType
	GroupCloseType
	GroupHistoryChangedType
	GroupJoinedType
	GroupLeftType
	GroupMarkedType
	GroupOpenType
	GroupRenameType
	GroupUnarchiveType
	HelloType
	ImCloseType
	ImCreatedType
	ImHistoryChangedType
	ImMarkedType
	ImOpenType
	ManualPresenceChangeType
	MessageType
	PinAddedType
	PinRemovedType
	PrefChangeType
	PresenceChangeType
	PongType
	ReactionAddedType
	ReactionRemovedType
	ReconnectURLType
	StarAddedType
	StarRemovedType
	SubteamCreatedType
	SubteamSelfAddedType
	SubteamSelfRemovedType
	SubteamUpdatedType
	TeamDomainChangeType
	TeamJoinType
	TeamMigrationStartedType
	TeamPlanChangeType
	TeamPrefChangeType
	TeamProfileChangeType
	TeamProfileDeleteType
	TeamProfileReorderType
	TeamRenameType
	UserChangeType
	UserTypingType
)

func (EventType) String

func (i EventType) String() string

type HelloEvent

type HelloEvent struct{}

type ImCreatedEvent

type ImCreatedEvent struct {
	User    string         `json:"user"`
	Channel ChannelCreated `json:"channel"`
}

type MemberJoinedchannel

type MemberJoinedchannel struct {
	EventTimestamp string `json:"event_ts"`
	Channel        string `json:"channel"`
	ChannelType    string `json:"channel_type"`
	User           string `json:"user"`
}

This event is... not in the events list... (as of Apr 3, 2017) https://api.slack.com/events

type MessageEvent

type MessageEvent struct {
	Channel    string `json:"channel"`
	SourceTeam string `json:"source_team"`
	Team       string `json:"team"`
	Text       string `json:"text"`
	Timestamp  string `json:"ts"`
	User       string `json:"user"`
}

type Option

type Option interface {
	Name() string
	Value() interface{}
}

Option defines an interface of optional parameters to the `rtm.New` constructor.

func WithBackOffStrategy

func WithBackOffStrategy(b backoff.BackOff) Option

WithBackOffStrategy creates a new option that defines the backoff strategy to be used when connections to the slack RTM server fails.

func WithPingInterval

func WithPingInterval(t time.Duration) Option

WithPingInterval creates a new option that defines the length of intervals between each successive pings.

type PongEvent

type PongEvent struct {
	ReplyTo int `json:"reply_to"`
	Time    int `json:"time""`
}

type PresenceChangeEvent

type PresenceChangeEvent struct {
	Presence string `json:"presence"`
	User     string `json:"user"`
}

type ReconnectURLEvent

type ReconnectURLEvent struct {
	URL string `json:"url"`
}

type UserTypingEvent

type UserTypingEvent struct {
	Channel string `json:"channel"`
	User    string `json:"user"`
}

Jump to

Keyboard shortcuts

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