From 7039a675ed02b4b50c3141680dd4c63e0dc5010c Mon Sep 17 00:00:00 2001 From: Caio Almeida <117518+caiosba@users.noreply.github.com> Date: Mon, 18 Sep 2023 09:29:32 -0300 Subject: [PATCH] Fixing: Media headers should work for non-WhatsApp tiplines Only WhatsApp requires template messages. The fix here is to use the regular message type when sending newsletters to non-WhatsApp subscribers. Fixes CV2.3646. --- app/models/team.rb | 2 +- app/models/tipline_newsletter.rb | 20 ++++++++++++++++--- app/models/tipline_resource.rb | 4 +--- app/workers/tipline_newsletter_worker.rb | 2 +- .../workers/tipline_newsletter_worker_test.rb | 7 +++++++ 5 files changed, 27 insertions(+), 8 deletions(-) diff --git a/app/models/team.rb b/app/models/team.rb index 5e1aa51f74..0e355ce27c 100644 --- a/app/models/team.rb +++ b/app/models/team.rb @@ -614,7 +614,7 @@ def available_newsletter_header_types tbi = TeamBotInstallation.where(team_id: self.id, user_id: BotUser.smooch_user&.id.to_i).last unless tbi.nil? ['none', 'image', 'video', 'audio', 'link_preview'].each do |header_type| - mapped_header_type = TiplineNewsletter::HEADER_TYPE_MAPPING[header_type] + mapped_header_type = TiplineNewsletter::WHATSAPP_HEADER_TYPE_MAPPING[header_type] if !tbi.send("get_smooch_template_name_for_newsletter_#{mapped_header_type}_no_articles").blank? && !tbi.send("get_smooch_template_name_for_newsletter_#{mapped_header_type}_one_articles").blank? && !tbi.send("get_smooch_template_name_for_newsletter_#{mapped_header_type}_two_articles").blank? && diff --git a/app/models/tipline_newsletter.rb b/app/models/tipline_newsletter.rb index 2583ba58d9..b4c1c540ee 100644 --- a/app/models/tipline_newsletter.rb +++ b/app/models/tipline_newsletter.rb @@ -1,12 +1,17 @@ class TiplineNewsletter < ApplicationRecord SCHEDULE_DAYS = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday'] - HEADER_TYPE_MAPPING = { + WHATSAPP_HEADER_TYPE_MAPPING = { 'none' => 'none', 'image' => 'image', 'video' => 'video', 'audio' => 'video', # WhatsApp doesn't support audio header, so we convert it to video 'link_preview' => 'none' } + NON_WHATSAPP_HEADER_TYPE_MAPPING = { + 'image' => 'image', + 'video' => 'file', + 'audio' => 'file' + } MAXIMUM_ARTICLE_LENGTH = { # Number of articles => Maximum length for each article 1 => 694, 2 => 345, @@ -114,7 +119,7 @@ def last_scheduled_by def whatsapp_template_name number = ['no', 'one', 'two', 'three'][self.articles.size] - type = HEADER_TYPE_MAPPING[self.header_type] + type = WHATSAPP_HEADER_TYPE_MAPPING[self.header_type] "newsletter_#{type}_#{number}_articles" end @@ -142,12 +147,13 @@ def body self.articles.join("\n\n") end + # For WhatsApp tiplines def format_as_template_message date = I18n.l(Time.now.to_date, locale: self.language.to_s.tr('_', '-'), format: :long) file_url = file_type = nil if ['image', 'audio', 'video'].include?(self.header_type) file_url = CheckS3.rewrite_url(self.header_media_url) - file_type = HEADER_TYPE_MAPPING[self.header_type] + file_type = WHATSAPP_HEADER_TYPE_MAPPING[self.header_type] end introduction = self.team.get_shorten_outgoing_urls ? UrlRewriter.shorten_and_utmize_urls(self.introduction, self.team.get_outgoing_urls_utm_code, self) : self.introduction params = [date, introduction, self.articles].flatten.reject{ |param| param.blank? } @@ -155,6 +161,14 @@ def format_as_template_message Bot::Smooch.format_template_message(self.whatsapp_template_name, params, file_url, self.build_content, self.language, file_type, preview_url) end + # For non-WhatsApp tiplines, ideally + def format_as_tipline_message + message = self.build_content + message = (self.team.get_shorten_outgoing_urls ? UrlRewriter.shorten_and_utmize_urls(message, self.team.get_outgoing_urls_utm_code) : message) + params = (['image', 'audio', 'video'].include?(self.header_type) ? { 'type' => NON_WHATSAPP_HEADER_TYPE_MAPPING[self.header_type], 'mediaUrl' => CheckS3.rewrite_url(self.header_media_url) } : {}) + [message, params] + end + def self.content_name 'newsletter' end diff --git a/app/models/tipline_resource.rb b/app/models/tipline_resource.rb index fa2e14dd1a..c8b073ef85 100644 --- a/app/models/tipline_resource.rb +++ b/app/models/tipline_resource.rb @@ -14,9 +14,7 @@ def format_as_tipline_message message = [] message << "*#{self.title.strip}*" unless self.title.strip.blank? message << self.content unless self.content.blank? - if self.content_type == 'static' - # FIXME: Handle static content - elsif self.content_type == 'rss' && !self.rss_feed_url.blank? + if self.content_type == 'rss' && !self.rss_feed_url.blank? message << Rails.cache.fetch("tipline_resource:rss_feed:#{Digest::MD5.hexdigest(self.rss_feed_url)}:#{self.number_of_articles}", expires_in: 15.minutes, skip_nil: true) do rss_feed = RssFeed.new(self.rss_feed_url) rss_feed.get_articles(self.number_of_articles).join("\n\n") diff --git a/app/workers/tipline_newsletter_worker.rb b/app/workers/tipline_newsletter_worker.rb index 5a3d7e2858..e3f8faa61b 100644 --- a/app/workers/tipline_newsletter_worker.rb +++ b/app/workers/tipline_newsletter_worker.rb @@ -40,7 +40,7 @@ def perform(team_id, language, job_created_at = 0) RequestStore.store[:smooch_bot_platform] = ts.platform Bot::Smooch.get_installation('team_bot_installation_id', tbi.id) { |i| i.id == tbi.id } - response = Bot::Smooch.send_message_to_user(ts.uid, newsletter.format_as_template_message) + response = (ts.platform == 'WhatsApp' ? Bot::Smooch.send_message_to_user(ts.uid, newsletter.format_as_template_message) : Bot::Smooch.send_message_to_user(ts.uid, *newsletter.format_as_tipline_message)) if response.code.to_i < 400 log team_id, language, "Newsletter sent to subscriber ##{ts.id}, response: (#{response.code}) #{response.body.inspect}" diff --git a/test/workers/tipline_newsletter_worker_test.rb b/test/workers/tipline_newsletter_worker_test.rb index 3248472871..304994efae 100644 --- a/test/workers/tipline_newsletter_worker_test.rb +++ b/test/workers/tipline_newsletter_worker_test.rb @@ -82,4 +82,11 @@ def setup TiplineNewsletter.any_instance.stubs(:content_has_changed?).raises(RssFeed::RssLoadError) assert_equal 0, TiplineNewsletterWorker.new.perform(@team.id, 'en') end + + test "should send newsletter for non-WhatsApp subscription" do + create_tipline_subscription team_id: @team.id, platform: 'Telegram' + assert_nothing_raised do + TiplineNewsletterWorker.perform_async(@team.id, 'en') + end + end end