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

twilio_receive_text_agent.rb 3.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. module Agents
  2. class TwilioReceiveTextAgent < Agent
  3. cannot_be_scheduled!
  4. cannot_receive_events!
  5. gem_dependency_check { defined?(Twilio) }
  6. description do <<-MD
  7. The Twilio Receive Text Agent receives text messages from Twilio and emits them as events.
  8. #{'## Include `twilio-ruby` in your Gemfile to use this Agent!' if dependencies_missing?}
  9. In order to create events with this agent, configure Twilio to send POST requests to:
  10. ```
  11. #{post_url}
  12. ```
  13. #{'The placeholder symbols above will be replaced by their values once the agent is saved.' unless id}
  14. Options:
  15. * `server_url` must be set to the URL of your
  16. Huginn installation (probably "https://#{ENV['DOMAIN']}"), which must be web-accessible. Be sure to set http/https correctly.
  17. * `account_sid` and `auth_token` are your Twilio account credentials. `auth_token` must be the primary auth token for your Twilio accout.
  18. * If `reply_text` is set, it's contents will be sent back as a confirmation text.
  19. * `expected_receive_period_in_days` - How often you expect to receive events this way. Used to determine if the agent is working.
  20. MD
  21. end
  22. def default_options
  23. {
  24. 'account_sid' => 'ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
  25. 'auth_token' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
  26. 'server_url' => "https://#{ENV['DOMAIN'].presence || example.com}",
  27. 'reply_text' => '',
  28. "expected_receive_period_in_days" => 1
  29. }
  30. end
  31. def validate_options
  32. unless options['account_sid'].present? && options['auth_token'].present? && options['server_url'].present? && options['expected_receive_period_in_days'].present?
  33. errors.add(:base, 'account_sid, auth_token, server_url, and expected_receive_period_in_days are all required')
  34. end
  35. end
  36. def working?
  37. event_created_within?(interpolated['expected_receive_period_in_days']) && !recent_error_logs?
  38. end
  39. def post_url
  40. if interpolated['server_url'].present?
  41. "#{interpolated['server_url']}/users/#{user.id}/web_requests/#{id || ':id'}/sms-endpoint"
  42. else
  43. "https://#{ENV['DOMAIN']}/users/#{user.id}/web_requests/#{id || ':id'}/sms-endpoint"
  44. end
  45. end
  46. def receive_web_request(request)
  47. params = request.params.except(:action, :controller, :agent_id, :user_id, :format)
  48. method = request.method_symbol.to_s
  49. headers = request.headers
  50. # check the last url param: 'secret'
  51. secret = params.delete('secret')
  52. return ["Not Authorized", 401] unless secret == "sms-endpoint"
  53. signature = headers['HTTP_X_TWILIO_SIGNATURE']
  54. # validate from twilio
  55. @validator ||= Twilio::Util::RequestValidator.new interpolated['auth_token']
  56. if !@validator.validate(post_url, params, signature)
  57. error("Twilio Signature Failed to Validate\n\n"+
  58. "URL: #{post_url}\n\n"+
  59. "POST params: #{params.inspect}\n\n"+
  60. "Signature: #{signature}"
  61. )
  62. return ["Not authorized", 401]
  63. end
  64. if create_event(payload: params)
  65. response = Twilio::TwiML::Response.new do |r|
  66. if interpolated['reply_text'].present?
  67. r.Message interpolated['reply_text']
  68. end
  69. end
  70. return [response.text, 201, "text/xml"]
  71. else
  72. return ["Bad request", 400]
  73. end
  74. end
  75. end
  76. end