Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance TwitterPublishAgent #3053

Merged
merged 2 commits into from
Jan 4, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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