twilio_agent.rb 3.5 KB

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