twilio_agent.rb 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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,
  32. 'account_sid, auth_token, sender_cell, receiver_cell, receive_text, receive_call and expected_receive_period_in_days are all required')
  33. end
  34. end
  35. def receive(incoming_events)
  36. memory['pending_calls'] ||= {}
  37. interpolate_with_each(incoming_events) do |event|
  38. message = (event.payload['message'].presence || event.payload['text'].presence || event.payload['sms'].presence).to_s
  39. if message.present?
  40. if boolify(interpolated['receive_call'])
  41. secret = SecureRandom.hex 3
  42. memory['pending_calls'][secret] = message
  43. make_call secret
  44. end
  45. if boolify(interpolated['receive_text'])
  46. message = message.slice 0..1600
  47. send_message message
  48. end
  49. end
  50. end
  51. end
  52. def working?
  53. last_receive_at && last_receive_at > interpolated['expected_receive_period_in_days'].to_i.days.ago && !recent_error_logs?
  54. end
  55. def send_message(message)
  56. client.messages.create from: interpolated['sender_cell'],
  57. to: interpolated['receiver_cell'],
  58. body: message
  59. end
  60. def make_call(secret)
  61. client.calls.create from: interpolated['sender_cell'],
  62. to: interpolated['receiver_cell'],
  63. url: post_url(interpolated['server_url'], secret)
  64. end
  65. def post_url(server_url, secret)
  66. "#{server_url}/users/#{user.id}/web_requests/#{id}/#{secret}"
  67. end
  68. def receive_web_request(params, method, format)
  69. if memory['pending_calls'].has_key? params['secret']
  70. response = Twilio::TwiML::VoiceResponse.new { |r|
  71. r.say(message: memory['pending_calls'][params['secret']], voice: 'woman')
  72. }
  73. memory['pending_calls'].delete params['secret']
  74. [response.to_s, 200]
  75. end
  76. end
  77. def client
  78. @client ||= Twilio::REST::Client.new interpolated['account_sid'], interpolated['auth_token']
  79. end
  80. end
  81. end