Module: SlackBot::GrapeExtension

Defined in:
lib/slack_bot/grape_extension.rb

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/slack_bot/grape_extension.rb', line 107

def self.included(base)
  base.format :json
  base.content_type :json, "application/json"
  base.use ActionDispatch::RemoteIp
  base.helpers SlackBot::GrapeHelpers

  base.before do
    verify_slack_signature!
  end

  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 body false if !result

      result
    end
  end

  base.resource :interactions do
    post do
      payload = JSON.parse(params[:payload])

      action_user_session =
        resolve_user_session(
          payload.dig("user", "team_id"),
          payload.dig("user", "id")
        )
      action_user = action_user_session&.user

      action_type = payload["type"]
      result = case action_type
      when "block_actions", "view_submission"
        handle_block_actions_view(
          view: payload["view"],
          user: action_user,
          params: params
        )
      else
        raise "Unknown action type: #{action_type}"
      end

      return body false if result.blank?

      result
    end
  end

  base.resource :events do
    post do
      result =
        case params[:type]
        when "url_verification"
          url_verification(params)
        when "event_callback"
          events_callback(params)
        end

      return body false if result.blank?

      result
    end
  end

  base.resource :menu_options do
    get do
      SlackBot::DevConsole.log_input "SlackApi::MenuOptions#get: #{params.inspect}"

      action_id = params[:action_id]
      menu_options_klass = config.find_menu_options(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 body false if menu_options.blank?

      menu_options
    end
  end
end