Keine Beschreibung http://j1x-huginn.herokuapp.com

wunderlist_agent.rb 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. module Agents
  2. class WunderlistAgent < Agent
  3. include FormConfigurable
  4. include Oauthable
  5. valid_oauth_providers :wunderlist
  6. cannot_be_scheduled!
  7. description <<-MD
  8. The WunderlistAgent creates new new tasks based on the incoming event.
  9. To be able to use this Agent you need to authenticate with Wunderlist in the [Services](/services) section first.
  10. MD
  11. def default_options
  12. {
  13. 'list_id' => '',
  14. 'title' => '{{title}}'
  15. }
  16. end
  17. form_configurable :list_id, roles: :completable
  18. form_configurable :title
  19. def complete_list_id
  20. response = request_guard do
  21. HTTParty.get lists_url, request_options
  22. end
  23. response.map { |p| {text: "#{p['title']} (#{p['id']})", id: p['id']}}
  24. end
  25. def validate_options
  26. errors.add(:base, "you need to specify the list you want to add tasks to") unless options['list_id'].present?
  27. errors.add(:base, "you need to specify the title of the task to create") unless options['title'].present?
  28. end
  29. def working?
  30. !recent_error_logs?
  31. end
  32. def receive(incoming_events)
  33. incoming_events.each do |event|
  34. mo = interpolated(event)
  35. title = mo[:title][0..244]
  36. log("Creating new task '#{title}' on list #{mo[:list_id]}", inbound_event: event)
  37. request_guard do
  38. HTTParty.post tasks_url, request_options.merge(body: {title: title, list_id: mo[:list_id].to_i}.to_json)
  39. end
  40. end
  41. end
  42. private
  43. def request_guard(&blk)
  44. response = yield
  45. error("Error during http request: #{response.body}") if response.code > 400
  46. response
  47. end
  48. def lists_url
  49. "https://a.wunderlist.com/api/v1/lists"
  50. end
  51. def tasks_url
  52. "https://a.wunderlist.com/api/v1/tasks"
  53. end
  54. def request_options
  55. {:headers => {'Content-Type' => 'application/json',
  56. 'User-Agent' => 'Huginn (https://github.com/cantino/huginn)',
  57. 'X-Access-Token' => service.token,
  58. 'X-Client-ID' => ENV["WUNDERLIST_OAUTH_KEY"] }}
  59. end
  60. end
  61. end