Нет описания http://j1x-huginn.herokuapp.com

twitter_publish_agent.rb 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. require "twitter"
  2. module Agents
  3. class TwitterPublishAgent < Agent
  4. cannot_be_scheduled!
  5. description <<-MD
  6. The TwitterPublishAgent publishes tweets from the events it receives.
  7. You [must set up a Twitter app](https://github.com/cantino/huginn/wiki/Getting-a-twitter-oauth-token) and provide it's `consumer_key`, `consumer_secret`, `oauth_token` and `oauth_token_secret`,
  8. (also knows as "Access token" on the Twitter developer's site), along with the `username` of the Twitter user to publish as.
  9. The `oauth_token` and `oauth_token_secret` determine which user the tweet will be sent as.
  10. You must also specify a `message_path` parameter: a [JSONPaths](http://goessner.net/articles/JsonPath/) to the value to tweet.
  11. Set `expected_update_period_in_days` to the maximum amount of time that you'd expect to pass between Events being created by this Agent.
  12. MD
  13. def validate_options
  14. unless options[:username].present? &&
  15. options[:expected_update_period_in_days].present? &&
  16. options[:consumer_key].present? &&
  17. options[:consumer_secret].present? &&
  18. options[:oauth_token].present? &&
  19. options[:oauth_token_secret].present?
  20. errors.add(:base, "expected_update_period_in_days, username, consumer_key, consumer_secret, oauth_token and oauth_token_secret are required")
  21. end
  22. end
  23. def working?
  24. (event = event_created_within(options[:expected_update_period_in_days].to_i.days)) && event.payload.present? && event.payload[:success] == true
  25. end
  26. def default_options
  27. {
  28. :username => "",
  29. :expected_update_period_in_days => "10",
  30. :consumer_key => "---",
  31. :consumer_secret => "---",
  32. :oauth_token => "---",
  33. :oauth_token_secret => "---",
  34. :message_path => "text"
  35. }
  36. end
  37. def receive(incoming_events)
  38. # if there are too many, dump a bunch to avoid getting rate limited
  39. if incoming_events.count > 20
  40. incoming_events = incoming_events.first(20)
  41. end
  42. incoming_events.each do |event|
  43. tweet_text = Utils.value_at(event.payload, options[:message_path])
  44. begin
  45. publish_tweet tweet_text
  46. create_event :payload => {
  47. :success => true,
  48. :published_tweet => tweet_text,
  49. :agent_id => event.agent_id,
  50. :event_id => event.id
  51. }
  52. rescue Twitter::Error => e
  53. create_event :payload => {
  54. :success => false,
  55. :error => e.message,
  56. :failed_tweet => tweet_text,
  57. :agent_id => event.agent_id,
  58. :event_id => event.id
  59. }
  60. end
  61. end
  62. end
  63. def publish_tweet text
  64. Twitter.configure do |config|
  65. config.consumer_key = options[:consumer_key]
  66. config.consumer_secret = options[:consumer_secret]
  67. config.oauth_token = options[:oauth_token]
  68. config.oauth_token_secret = options[:oauth_token_secret]
  69. end
  70. Twitter.update(text)
  71. end
  72. end
  73. end