Sha256: cb438708f1a478768c4613c1ef301329b871d48e5a6f99ffb3e2aff5304cce37

Contents?: true

Size: 1.22 KB

Versions: 3

Compression:

Stored size: 1.22 KB

Contents

# frozen_string_literal: true

module Waylon
  module Webhooks
    # Webhook for the Slack Sense for Waylon
    class Slack < Waylon::Webhook
      before do
        content_type "application/json"
      end

      post "/" do
        request.body.rewind
        verify(request) unless ENV.fetch("LOCAL_MODE", false)
        if @parsed_body.is_a?(Hash) && @parsed_body[:type] == "url_verification"
          { challenge: @parsed_body[:challenge] }.to_json
        else
          enqueue(@parsed_body)
          { status: :ok }.to_json
        end
      rescue ::Slack::Events::Request::InvalidSignature, ::Slack::Events::Request::TimestampExpired
        halt(403, { error: "Unable to authenticate request" }.to_json)
      rescue StandardError => e
        log("Encountered #{e.message}", :warn)
        halt(422, { error: "Unprocessable entity: #{e.message}" }.to_json)
      end

      options "/" do
        halt 200
      end

      # Used to verify incoming Slack requests
      def verify(incoming_request)
        slack_request = ::Slack::Events::Request.new(incoming_request)
        slack_request.verify!
      end

      # Automatically informs Waylon about this Webhook
      Waylon::WebhookRegistry.register(:slack, self)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
waylon-slack-0.1.2 lib/waylon/webhooks/slack.rb
waylon-slack-0.1.1 lib/waylon/webhooks/slack.rb
waylon-slack-0.1.0 lib/waylon/webhooks/slack.rb