Skip to content

Commit

Permalink
Merge pull request #3053 from huginn/enhance_twitter_publish_agent
Browse files Browse the repository at this point in the history
Enhance TwitterPublishAgent
  • Loading branch information
knu committed Jan 4, 2022
2 parents b06cf4e + 653665d commit 92e98de
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions app/models/agents/twitter_publish_agent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class TwitterPublishAgent < Agent
To be able to use this Agent you need to authenticate with Twitter in the [Services](/services) section first.
You must also specify a `message` parameter, you can use [Liquid](https://github.com/huginn/huginn/wiki/Formatting-Events-using-Liquid) to format the message.
Additional parameters can be passed via `parameters`.
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.
Expand All @@ -24,7 +25,7 @@ class TwitterPublishAgent < Agent
"success": true,
"published_tweet": "...",
"tweet_id": ...,
"tweet_id_str": "...",
"tweet_url": "...",
"agent_id": ...,
"event_id": ...
}
Expand Down Expand Up @@ -56,6 +57,7 @@ def default_options
{
'expected_update_period_in_days' => "10",
'message' => "{{text}}",
'parameters' => {},
'output_mode' => 'clean'
}
end
Expand All @@ -66,10 +68,10 @@ def receive(incoming_events)
incoming_events = incoming_events.first(20)
end
incoming_events.each do |event|
tweet_text = interpolated(event)['message']
tweet_text, parameters = interpolated(event).values_at('message', 'parameters')
new_event = interpolated['output_mode'].to_s == 'merge' ? event.payload.dup : {}
begin
tweet = publish_tweet tweet_text
tweet = publish_tweet(tweet_text, parameters.presence || {})
rescue Twitter::Error => e
new_event.update(
'success' => false,
Expand All @@ -83,6 +85,7 @@ def receive(incoming_events)
'success' => true,
'published_tweet' => tweet_text,
'tweet_id' => tweet.id,
'tweet_url' => tweet.url,
'agent_id' => event.agent_id,
'event_id' => event.id
)
Expand All @@ -91,8 +94,8 @@ def receive(incoming_events)
end
end

def publish_tweet(text)
twitter.update(text)
def publish_tweet(text, parameters = {})
twitter.update(text, parameters)
end
end
end

0 comments on commit 92e98de

Please sign in to comment.