Class: BoltRb::Handlers::ViewClosedHandler

Inherits:
Base
  • Object
show all
Defined in:
lib/bolt_rb/handlers/view_closed_handler.rb

Overview

Handler for Slack view closed events (modal cancellation/dismissal)

This handler provides the view_closed DSL for matching view_closed payloads. View closed events are triggered when users close a modal by clicking the X button or Cancel, but only when the modal was opened with notify_on_close: true.

Examples:

Basic modal close handler

class CreateTicketCancelHandler < BoltRb::ViewClosedHandler
  view_closed 'create_ticket_modal'

  def handle
    ack
    # Clean up any in-progress state
  end
end

Handler checking if view was cleared programmatically

class SettingsCloseHandler < BoltRb::ViewClosedHandler
  view_closed 'settings_modal'

  def handle
    ack
    unless is_cleared?
      # User manually closed, maybe prompt to save draft
    end
  end
end

Regex-based view matching

class WizardCancelHandler < BoltRb::ViewClosedHandler
  view_closed /^wizard_step_/

  def handle
    ack
    # Clean up wizard state for any step
  end
end

Instance Attribute Summary

Attributes inherited from Base

#context

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#ack, #call, #channel, #client, #handle, inherited, #initialize, middleware_stack, #payload, #respond, #say, use, #user

Constructor Details

This class inherits a constructor from BoltRb::Handlers::Base

Class Method Details

.matches?(payload) ⇒ Boolean

Determines if this handler matches the given payload

Checks if the payload is a view_closed type and if the callback_id matches the configured pattern.

Parameters:

  • payload (Hash)

    The incoming Slack view_closed payload

Returns:

  • (Boolean)

    true if this handler should process the close event



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/bolt_rb/handlers/view_closed_handler.rb', line 68

def matches?(payload)
  return false unless matcher_config
  return false unless payload['type'] == 'view_closed'

  view_callback_id = payload.dig('view', 'callback_id')
  return false unless view_callback_id

  if matcher_config[:callback_id].is_a?(Regexp)
    matcher_config[:callback_id].match?(view_callback_id)
  else
    view_callback_id == matcher_config[:callback_id]
  end
end

.view_closed(callback_id) ⇒ void

This method returns an undefined value.

Configures which callback_id this handler responds to

Examples:

Match exact callback_id

view_closed 'create_ticket_modal'

Match callback_id pattern

view_closed /^wizard_/

Parameters:

  • callback_id (String, Regexp)

    The callback_id to match (exact string or regex)



54
55
56
57
58
59
# File 'lib/bolt_rb/handlers/view_closed_handler.rb', line 54

def view_closed(callback_id)
  @matcher_config = {
    type: :view_closed,
    callback_id: callback_id
  }
end

Instance Method Details

#callback_idString?

Returns the callback_id from the view

Returns:

  • (String, nil)

    The callback_id



93
94
95
# File 'lib/bolt_rb/handlers/view_closed_handler.rb', line 93

def callback_id
  view&.dig('callback_id')
end

#is_cleared?Boolean

Returns whether the view was cleared programmatically

This is true when the view was dismissed via views.update with a clear_on_close response, rather than the user clicking X or Cancel.

Returns:

  • (Boolean)

    true if the view was cleared programmatically



113
114
115
# File 'lib/bolt_rb/handlers/view_closed_handler.rb', line 113

def is_cleared?
  payload['is_cleared'] == true
end

#private_metadataString?

Returns the private_metadata from the view

Private metadata is a string field you can use to pass data between the view open and close events. Often used to store IDs for cleanup.

Returns:

  • (String, nil)

    The private_metadata value



103
104
105
# File 'lib/bolt_rb/handlers/view_closed_handler.rb', line 103

def 
  view&.dig('private_metadata')
end

#user_idString?

Returns the user ID from the payload

Returns:

  • (String, nil)

    The user ID who closed the modal



120
121
122
# File 'lib/bolt_rb/handlers/view_closed_handler.rb', line 120

def user_id
  payload.dig('user', 'id')
end

#viewHash?

Returns the view object from the payload

Returns:

  • (Hash, nil)

    The view object containing callback_id, private_metadata, etc.



86
87
88
# File 'lib/bolt_rb/handlers/view_closed_handler.rb', line 86

def view
  payload['view']
end