Module: SlackBot::GrapeExtension

Defined in:
lib/slack_bot/grape_extension.rb

Class Method Summary collapse

Class Method Details

.add_commands_resource!(base) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/slack_bot/grape_extension.rb', line 188

def self.add_commands_resource!(base)
  base.resource :commands do
    post ":url_token" do
      command_config = config.find_slash_command_config(params[:url_token], params[:command], params[:text])
      command_klass = command_config&.command_klass
      raise SlackBot::Errors::SlashCommandNotImplemented.new if command_klass.blank?

      args = params[:text].gsub(/^#{command_config.full_token}\s?/, "")
      SlackBot::DevConsole.log_input "SlackApi::SlashCommands#post: #{command_config.url_token} | #{command_config.full_token} | #{args}"

      action = command_klass.new(current_user: current_user, params: params, args: args, config: config)
      verify_slack_team! if action.only_slack_team?
      verify_direct_message_channel! if action.only_direct_message?
      verify_current_user! if action.only_user?

      result = action.call
      return blank_slack_response! unless result

      result
    end
  end
end

.add_events_resource!(base) ⇒ Object



231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/slack_bot/grape_extension.rb', line 231

def self.add_events_resource!(base)
  base.resource :events do
    post do
      result = case params[:type]
      when "url_verification"
        url_verification(params)
      when "event_callback"
        events_callback(params)
      else
        raise SlackBot::Errors::UnknownActionTypeError.new(params[:type])
      end

      return blank_slack_response! if result.blank? || result == false

      result
    end
  end
end

.add_interactions_resource!(base) ⇒ Object



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/slack_bot/grape_extension.rb', line 211

def self.add_interactions_resource!(base)
  base.resource :interactions do
    post do
      payload = parse_interaction_payload!(params[:payload])
      action_user = resolve_action_user(payload)&.user

      result = case payload["type"]
      when "block_actions", "view_submission"
        handle_block_actions_view(view: payload["view"], user: action_user, params: params)
      else
        raise SlackBot::Errors::UnknownActionTypeError.new(payload["type"])
      end

      return blank_slack_response! if result.blank? || result == false

      result
    end
  end
end

.add_menu_options_resource!(base) ⇒ Object



250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/slack_bot/grape_extension.rb', line 250

def self.add_menu_options_resource!(base)
  base.resource :menu_options do
    get do
      SlackBot::DevConsole.log_input "SlackApi::MenuOptions#get: #{params.inspect}"

      menu_options_klass = config.find_menu_options(params[:action_id])
      raise SlackBot::Errors::MenuOptionsNotImplemented.new if menu_options_klass.blank?

      menu_options = menu_options_klass.new(current_user: current_user, params: params, config: config).call
      return blank_slack_response! if menu_options.blank?

      menu_options
    end
  end
end

.configure_base!(base) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/slack_bot/grape_extension.rb', line 175

def self.configure_base!(base)
  base.format :json
  base.content_type :json, "application/json"
  base.use ActionDispatch::RemoteIp if defined?(ActionDispatch::RemoteIp)
  base.helpers SlackBot::GrapeHelpers
  base.rescue_from SlackBot::Error do |e|
    error!({error: e.message}, 200)
  end
  base.before do
    verify_slack_signature!
  end
end

.included(base) ⇒ Object



167
168
169
170
171
172
173
# File 'lib/slack_bot/grape_extension.rb', line 167

def self.included(base)
  configure_base!(base)
  add_commands_resource!(base)
  add_interactions_resource!(base)
  add_events_resource!(base)
  add_menu_options_resource!(base)
end