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

twilio_agent.rb 3.6KB

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