Module: SlackBot::GrapeExtension

Defined in:
lib/slack_bot/grape_extension.rb,
sig/slack_bot.rbs

Class Method Summary collapse

Class Method Details

.add_commands_resource!(base) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/slack_bot/grape_extension.rb', line 159

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



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/slack_bot/grape_extension.rb', line 198

def self.add_events_resource!(base)
  base.resource :events do
    post do
      return blank_slack_response! if slack_request_retry?

      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



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/slack_bot/grape_extension.rb', line 182

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

      result = route_interaction(payload: payload, user: action_user, params: params)

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

      result
    end
  end
end

.add_menu_options_resource!(base) ⇒ Object



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/slack_bot/grape_extension.rb', line 219

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

      verify_slack_team!
      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



146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/slack_bot/grape_extension.rb', line 146

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) ⇒ void

This method returns an undefined value.

Parameters:

  • base (Class)


138
139
140
141
142
143
144
# File 'lib/slack_bot/grape_extension.rb', line 138

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