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

added ability to auto-hashtag and post to multiple networks #77

Merged
merged 1 commit into from
Jun 8, 2023
Merged
Show file tree
Hide file tree
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
8 changes: 8 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,11 @@ AYRSHARE_API_KEY=
# Set to true to enable dynamic throttling of NextLeg API requests.
# See imagine_image.rb for more details.
DYNAMIC_THROTTLE = true

# Toggle Social Networks
TWITTER_ENABLED = TRUE
LINKEDIN_ENABLED = TRUE

# This will strip out the hashtags from the tweet and replace them with
# Trending real-time hashtags from Ayrshare
AUTOMATIC_HASHTAGS = TRUE
22 changes: 18 additions & 4 deletions app/jobs/tweets/publish_tweet_job.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,35 @@
class Tweets::PublishTweetJob < ApplicationJob
queue_as :default
MAX_CHARACTERS = 280

def perform(discussion:)
return if discussion.tweet.uploaded

story = discussion.story
tweet = discussion.tweet

platforms = []
platforms.push 'twitter' if ENV['TWITTER_ENABLED']
platforms.push 'linkedin' if ENV['LINKEDIN_ENABLED']

tweet_text = JSON.parse(tweet.stem)['tweet']

if story.sub_topic.ai_disclaimer
tweet_text = "📟 AI Perspective: #{tweet_text}"
end

# Removes hashtags and shortens the max characters to accommodate for hashtags.
if ENV['AUTOMATIC_HASHTAGS'] == 'true'
tweet_text = tweet_text.gsub(/#\w+/, '')
max_characters = 220
auto_hashtag = true
else
max_characters = 280
auto_hashtag = false
end

# Truncate tweet_text to fit within the MAX_CHARACTERS limit
# 23 characters are reserved for URL and a space
truncated_tweet_text = tweet_text.truncate(MAX_CHARACTERS - 23, omission: '...')
# 28 characters are reserved for URL and a space
truncated_tweet_text = tweet_text.truncate(max_characters - 28, omission: '...')

card_image = tweet.discussion.story.imaginations.where(aspect_ratio: :card).sample(1)
card_image_url = "https://ucarecdn.com/#{card_image.last.uploadcare.last['uuid']}/-/format/auto/-/quality/smart/-/preview/"
Expand All @@ -29,8 +42,9 @@ def perform(discussion:)

full_tweet = "#{truncated_tweet_text} #{discussion_url}"

Ayrshare.post_message(post: full_tweet, platforms: ['twitter'], media_urls: [card_image_url])
return if platforms.blank?

Ayrshare.post_message(post: full_tweet, platforms: , media_urls: [card_image_url], auto_hashtag:)
tweet.update(uploaded: true)
end

Expand Down
4 changes: 2 additions & 2 deletions app/lib/ayrshare.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ def self.configure
yield(configuration)
end

def self.post_message(post:, platforms:, media_urls: [])
def self.post_message(post:, platforms:, media_urls: [], auto_hashtag:)
url = "https://app.ayrshare.com/api/post"
headers = { 'Authorization' => "Bearer #{configuration.api_key}" }
body = { post:, platforms:, mediaUrls: [media_urls] }
body = { post:, platforms:, mediaUrls: [media_urls], autoHashtag: auto_hashtag }

response = HTTParty.post(url, headers:, body:)

Expand Down