| 1 | package socketmode |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "log" |
| 6 | "os" |
| 7 | "time" |
| 8 | |
| 9 | "github.com/gorilla/websocket" |
| 10 | |
| 11 | "github.com/slack-go/slack" |
| 12 | ) |
| 13 | |
| 14 | // EventType is the type of events that are emitted by scoketmode.Client. |
| 15 | // You receive and handle those events from a socketmode.Client.Events channel. |
| 16 | // Those event types does not necessarily match 1:1 to those of Slack Events API events. |
| 17 | type EventType string |
| 18 | |
| 19 | const ( |
| 20 | // The following request types are the types of requests sent from Slack via Socket Mode WebSocket connection |
| 21 | // and handled internally by the socketmode.Client. |
| 22 | // The consumer of socketmode.Client will never see it. |
| 23 | |
| 24 | RequestTypeHello = "hello" |
| 25 | RequestTypeEventsAPI = "events_api" |
| 26 | RequestTypeDisconnect = "disconnect" |
| 27 | RequestTypeSlashCommands = "slash_commands" |
| 28 | RequestTypeInteractive = "interactive" |
| 29 | |
| 30 | // The following event types are for events emitted by socketmode.Client itself and |
| 31 | // does not originate from Slack. |
| 32 | EventTypeConnecting = EventType("connecting") |
| 33 | EventTypeInvalidAuth = EventType("invalid_auth") |
| 34 | EventTypeConnectionError = EventType("connection_error") |
| 35 | EventTypeConnected = EventType("connected") |
| 36 | EventTypeIncomingError = EventType("incoming_error") |
| 37 | EventTypeErrorWriteFailed = EventType("write_error") |
| 38 | EventTypeErrorBadMessage = EventType("error_bad_message") |
| 39 | |
| 40 | // |
| 41 | // The following event types are guaranteed to not change unless Slack changes |
| 42 | // |
| 43 | |
| 44 | EventTypeHello = EventType("hello") |
| 45 | EventTypeDisconnect = EventType("disconnect") |
| 46 | EventTypeEventsAPI = EventType("events_api") |
| 47 | EventTypeInteractive = EventType("interactive") |
| 48 | EventTypeSlashCommand = EventType("slash_commands") |
| 49 | |
| 50 | websocketDefaultTimeout = 10 * time.Second |
| 51 | defaultMaxPingInterval = 30 * time.Second |
| 52 | ) |
| 53 | |
| 54 | // Open calls the "apps.connections.open" endpoint and returns the provided URL and the full Info block. |
| 55 | // |
| 56 | // To have a fully managed Websocket connection, use `New`, and call `Run()` on it. |
| 57 | func (smc *Client) Open() (info *slack.SocketModeConnection, websocketURL string, err error) { |
| 58 | ctx, cancel := context.WithTimeout(context.Background(), websocketDefaultTimeout) |
| 59 | defer cancel() |
| 60 | |
| 61 | return smc.StartSocketModeContext(ctx) |
| 62 | } |
| 63 | |
| 64 | // OpenContext calls the "apps.connections.open" endpoint and returns the provided URL and the full Info block. |
| 65 | // |
| 66 | // To have a fully managed Websocket connection, use `New`, and call `Run()` on it. |
| 67 | func (smc *Client) OpenContext(ctx context.Context) (info *slack.SocketModeConnection, websocketURL string, err error) { |
| 68 | return smc.StartSocketModeContext(ctx) |
| 69 | } |
| 70 | |
| 71 | // Option options for the managed Client. |
| 72 | type Option func(client *Client) |
| 73 | |
| 74 | // OptionDialer takes a gorilla websocket Dialer and uses it as the |
| 75 | // Dialer when opening the websocket for the Socket Mode connection. |
| 76 | func OptionDialer(d *websocket.Dialer) Option { |
| 77 | return func(smc *Client) { |
| 78 | smc.dialer = d |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | // OptionPingInterval determines how often we expect Slack to deliver WebSocket ping to us. |
| 83 | // If no ping is delivered to us within this interval after the last ping, we assumes the WebSocket connection |
| 84 | // is dead and needs to be reconnected. |
| 85 | func OptionPingInterval(d time.Duration) Option { |
| 86 | return func(smc *Client) { |
| 87 | smc.maxPingInterval = d |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | // OptionDebug enable debugging for the client |
| 92 | func OptionDebug(b bool) func(*Client) { |
| 93 | return func(c *Client) { |
| 94 | c.debug = b |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | // OptionLog set logging for client. |
| 99 | func OptionLog(l logger) func(*Client) { |
| 100 | return func(c *Client) { |
| 101 | c.log = internalLog{logger: l} |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | // New returns a Socket Mode client which provides a fully managed connection to |
| 106 | // Slack's Websocket-based Socket Mode. |
| 107 | func New(api *slack.Client, options ...Option) *Client { |
| 108 | result := &Client{ |
| 109 | Client: *api, |
| 110 | Events: make(chan Event, 50), |
| 111 | socketModeResponses: make(chan *Response, 20), |
| 112 | maxPingInterval: defaultMaxPingInterval, |
| 113 | log: log.New(os.Stderr, "slack-go/slack/socketmode", log.LstdFlags|log.Lshortfile), |
| 114 | } |
| 115 | |
| 116 | for _, opt := range options { |
| 117 | opt(result) |
| 118 | } |
| 119 | |
| 120 | return result |
| 121 | } |