Nenhuma Descrição http://j1x-huginn.herokuapp.com

webhook_agent.rb 3.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. require 'uri'
  2. module Agents
  3. class WebhookAgent < Agent
  4. include WebRequestConcern
  5. cannot_be_scheduled!
  6. cannot_receive_events!
  7. description do <<-MD
  8. The Webhook Agent will create events by receiving webhooks from any source. In order to create events with this agent, make a POST request to:
  9. ```
  10. https://#{ENV['DOMAIN']}/users/#{user.id}/web_requests/#{id || ':id'}/#{options['secret'] || ':secret'}
  11. ```
  12. #{'The placeholder symbols above will be replaced by their values once the agent is saved.' unless id}
  13. Options:
  14. * `secret` - A token that the host will provide for authentication.
  15. * `expected_receive_period_in_days` - How often you expect to receive
  16. events this way. Used to determine if the agent is working.
  17. * `payload_path` - JSONPath of the attribute in the POST body to be
  18. used as the Event payload. Set to `.` to return the entire message.
  19. If `payload_path` points to an array, Events will be created for each element.
  20. * `verbs` - Comma-separated list of http verbs your agent will accept.
  21. For example, "post,get" will enable POST and GET requests. Defaults
  22. to "post".
  23. * `response` - The response message to the request. Defaults to 'Event Created'.
  24. * `recaptcha_secret` - Setting this to a reCAPTCHA "secret" key makes your agent verify incoming requests with reCAPTCHA. Don't forget to embed a reCAPTCHA snippet including your "site" key in the originating form(s).
  25. * `recaptcha_send_remote_addr` - Set this to true if your server is properly configured to set REMOTE_ADDR to the IP address of each visitor (instead of that of a proxy server).
  26. MD
  27. end
  28. event_description do
  29. <<-MD
  30. The event payload is based on the value of the `payload_path` option,
  31. which is set to `#{interpolated['payload_path']}`.
  32. MD
  33. end
  34. def default_options
  35. { "secret" => "supersecretstring",
  36. "expected_receive_period_in_days" => 1,
  37. "payload_path" => "some_key"
  38. }
  39. end
  40. def receive_web_request(params, method, format)
  41. # check the secret
  42. secret = params.delete('secret')
  43. return ["Not Authorized", 401] unless secret == options['secret']
  44. # check the verbs
  45. verbs = (interpolated['verbs'] || 'post').split(/,/).map { |x| x.strip.downcase }.select { |x| x.present? }
  46. return ["Please use #{verbs.join('/').upcase} requests only", 401] unless verbs.include?(method)
  47. [payload_for(params)].flatten.each do |payload|
  48. create_event(payload: payload)
  49. end
  50. [response_message, 201]
  51. end
  52. def working?
  53. event_created_within?(interpolated['expected_receive_period_in_days']) && !recent_error_logs?
  54. end
  55. def validate_options
  56. unless options['secret'].present?
  57. errors.add(:base, "Must specify a secret for 'Authenticating' requests")
  58. end
  59. end
  60. def payload_for(params)
  61. Utils.value_at(params, interpolated['payload_path']) || {}
  62. end
  63. def response_message
  64. interpolated['response'] || 'Event Created'
  65. end
  66. end
  67. end