|
require "twitter"
module Agents
class TwitterPublishAgent < Agent
include TwitterConcern
cannot_be_scheduled!
description <<-MD
The TwitterPublishAgent publishes tweets from the events it receives.
Twitter credentials must be supplied as either [credentials](/user_credentials) called
`twitter_consumer_key`, `twitter_consumer_secret`, `twitter_oauth_token`, and `twitter_oauth_token_secret`,
or as options to this Agent called `consumer_key`, `consumer_secret`, `oauth_token`, and `oauth_token_secret`.
To get oAuth credentials for Twitter, [follow these instructions](https://github.com/cantino/huginn/wiki/Getting-a-twitter-oauth-token).
You must also specify a `message_path` parameter: a [JSONPaths](http://goessner.net/articles/JsonPath/) to the value to tweet.
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.
MD
def validate_options
errors.add(:base, "expected_update_period_in_days is required") unless options['expected_update_period_in_days'].present?
end
def working?
event_created_within?(options['expected_update_period_in_days']) && most_recent_event.payload['success'] == true && !recent_error_logs?
end
def default_options
{
'expected_update_period_in_days' => "10",
'message_path' => "text"
}
end
def receive(incoming_events)
# if there are too many, dump a bunch to avoid getting rate limited
if incoming_events.count > 20
incoming_events = incoming_events.first(20)
end
incoming_events.each do |event|
tweet_text = Utils.value_at(event.payload, options['message_path'])
begin
publish_tweet tweet_text
create_event :payload => {
'success' => true,
'published_tweet' => tweet_text,
'agent_id' => event.agent_id,
'event_id' => event.id
}
rescue Twitter::Error => e
create_event :payload => {
'success' => false,
'error' => e.message,
'failed_tweet' => tweet_text,
'agent_id' => event.agent_id,
'event_id' => event.id
}
end
end
end
def publish_tweet(text)
Twitter.update(text)
end
end
end
|