twilio_agent.rb 3.9 KB

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