From 933aa26ec9055949df213ab65d460462c5c6b6da Mon Sep 17 00:00:00 2001 From: Leonid Medovyy Date: Thu, 8 Jun 2023 10:55:43 -0700 Subject: [PATCH] added ability to auto-hashtag and post to multiple networks --- .env.example | 8 ++++++++ app/jobs/tweets/publish_tweet_job.rb | 22 ++++++++++++++++++---- app/lib/ayrshare.rb | 4 ++-- 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/.env.example b/.env.example index 4b9b810..5ff4ebf 100644 --- a/.env.example +++ b/.env.example @@ -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 \ No newline at end of file diff --git a/app/jobs/tweets/publish_tweet_job.rb b/app/jobs/tweets/publish_tweet_job.rb index 3ceb5ff..9fcf202 100644 --- a/app/jobs/tweets/publish_tweet_job.rb +++ b/app/jobs/tweets/publish_tweet_job.rb @@ -1,6 +1,5 @@ class Tweets::PublishTweetJob < ApplicationJob queue_as :default - MAX_CHARACTERS = 280 def perform(discussion:) return if discussion.tweet.uploaded @@ -8,15 +7,29 @@ def perform(discussion:) 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/" @@ -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 diff --git a/app/lib/ayrshare.rb b/app/lib/ayrshare.rb index 2a899ba..95a5f0b 100644 --- a/app/lib/ayrshare.rb +++ b/app/lib/ayrshare.rb @@ -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:)