twilio_agent.rb 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. require 'securerandom'
  2. module Agents
  3. class TwilioAgent < Agent
  4. cannot_be_scheduled!
  5. cannot_create_events!
  6. no_bulk_receive!
  7. gem_dependency_check { defined?(Twilio) }
  8. description <<-MD
  9. The Twilio Agent receives and collects events and sends them via text message (up to 160 characters) or gives you a call when scheduled.
  10. #{'## Include `twilio-ruby` in your Gemfile to use this Agent!' if dependencies_missing?}
  11. It is assumed that events have a `message`, `text`, or `sms` key, the value of which is sent as the content of the text message/call. You can use the EventFormattingAgent if your event does not provide these keys.
  12. Set `receiver_cell` to the number to receive text messages/call and `sender_cell` to the number sending them.
  13. `expected_receive_period_in_days` is maximum number of days that you would expect to pass between events being received by this agent.
  14. If you would like to receive calls, set `receive_call` to `true`. In this case, `server_url` must be set to the URL of your
  15. Huginn installation (probably "https://#{ENV['DOMAIN']}"), which must be web-accessible. Be sure to set http/https correctly.
  16. MD
  17. def default_options
  18. {
  19. 'account_sid' => 'ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
  20. 'auth_token' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
  21. 'sender_cell' => 'xxxxxxxxxx',
  22. 'receiver_cell' => 'xxxxxxxxxx',
  23. 'server_url' => 'http://somename.com:3000',
  24. 'receive_text' => 'true',
  25. 'receive_call' => 'false',
  26. 'expected_receive_period_in_days' => '1'
  27. }
  28. end
  29. def validate_options
  30. unless options['account_sid'].present? && options['auth_token'].present? && options['sender_cell'].present? && options['receiver_cell'].present? && options['expected_receive_period_in_days'].present? && options['receive_call'].present? && options['receive_text'].present?
  31. errors.add(:base, 'account_sid, auth_token, sender_cell, receiver_cell, receive_text, receive_call and expected_receive_period_in_days are all required')
  32. end
  33. end
  34. def receive(incoming_events)
  35. memory['pending_calls'] ||= {}
  36. interpolate_with_each(incoming_events) do |event|
  37. message = (event.payload['message'].presence || event.payload['text'].presence || event.payload['sms'].presence).to_s
  38. if message.present?
  39. if boolify(interpolated['receive_call'])
  40. secret = SecureRandom.hex 3
  41. memory['pending_calls'][secret] = message
  42. make_call secret
  43. end
  44. if boolify(interpolated['receive_text'])
  45. message = message.slice 0..160
  46. send_message message
  47. end
  48. end
  49. end
  50. end
  51. def working?
  52. last_receive_at && last_receive_at > interpolated['expected_receive_period_in_days'].to_i.days.ago && !recent_error_logs?
  53. end
  54. def send_message(message)
  55. client.account.messages.create :from => interpolated['sender_cell'],
  56. :to => interpolated['receiver_cell'],
  57. :body => message
  58. end
  59. def make_call(secret)
  60. client.account.calls.create :from => interpolated['sender_cell'],
  61. :to => interpolated['receiver_cell'],
  62. :url => post_url(interpolated['server_url'], secret)
  63. end
  64. def post_url(server_url, secret)
  65. "#{server_url}/users/#{user.id}/web_requests/#{id}/#{secret}"
  66. end
  67. def receive_web_request(params, method, format)
  68. if memory['pending_calls'].has_key? params['secret']
  69. response = Twilio::TwiML::Response.new {|r| r.Say memory['pending_calls'][params['secret']], :voice => 'woman'}
  70. memory['pending_calls'].delete params['secret']
  71. [response.text, 200]
  72. end
  73. end
  74. def client
  75. @client ||= Twilio::REST::Client.new interpolated['account_sid'], interpolated['auth_token']
  76. end
  77. end
  78. end