From a249b289c8463960380b4f3a87849d53696b58b7 Mon Sep 17 00:00:00 2001
From: Felix Eckhofer <felix@eckhofer.com>
Date: Sun, 22 Dec 2024 14:44:32 +0100
Subject: [PATCH] Refactor telegram notification settings

---
 app/jobs/telegram_group_chat_notification_job.rb |  3 ++-
 app/jobs/telegram_notify_upcoming_job.rb         |  6 ++----
 app/subscribers/telegram_bot_subscriber.rb       |  5 ++---
 config/environments/development.rb               |  2 ++
 config/environments/production.rb                |  2 ++
 config/initializers/fetch_credentials.rb         |  5 +++++
 db/seeds.rb                                      | 10 ++++++----
 7 files changed, 21 insertions(+), 12 deletions(-)
 create mode 100644 config/initializers/fetch_credentials.rb

diff --git a/app/jobs/telegram_group_chat_notification_job.rb b/app/jobs/telegram_group_chat_notification_job.rb
index e588bd9..f55fef9 100644
--- a/app/jobs/telegram_group_chat_notification_job.rb
+++ b/app/jobs/telegram_group_chat_notification_job.rb
@@ -9,7 +9,8 @@ class TelegramGroupChatNotificationJob < NotificationJob
     return unless channel&.data
     token = channel.data['token']
     return unless token
-    args[:target] = "2192297" if Rails.env.development?
+    args[:parse_mode] ||= "HTML"
+    args[:target] ||= Rails.application.config.telegram_default_target
     Telegram::Bot::Client.run(token) do |bot|
       bot.api.send_message(chat_id: args[:target], text: args[:text], parse_mode: args[:parse_mode])
     end
diff --git a/app/jobs/telegram_notify_upcoming_job.rb b/app/jobs/telegram_notify_upcoming_job.rb
index 5f06d8d..1b34d45 100644
--- a/app/jobs/telegram_notify_upcoming_job.rb
+++ b/app/jobs/telegram_notify_upcoming_job.rb
@@ -13,13 +13,11 @@ class TelegramNotifyUpcomingJob < ApplicationJob
         message = notify_names.join(' ') + ": Your scheduled session <i>#{session.title}</i> starts at <b>#{session.starts_at.strftime("%H:%M")}</b> on <b>#{session.stage.name}</b>"
         message += "<br>Speakers: #{session.speakers.map(&:name).join(', ')}"
 
-        TelegramGroupChatNotificationJob.perform_later(target: "-316096320", text: message, parse_mode: 'HTML')
-        # TelegramGroupChatNotificationJob.perform_now(target: "2192297", text: message, parse_mode: 'HTML')
+        TelegramGroupChatNotificationJob.perform_later(text: message)
       else
         message = "<b>⚠️ No assignees</b> for session <i>#{session.title}</i> starting at <b>#{session.starts_at.strftime("%H:%M")}</b> on <b>#{session.stage.name}</b>"
         message += "<br>Speakers: #{session.speakers.map(&:name).join(', ')}"
-        TelegramGroupChatNotificationJob.perform_later(target: "-316096320", text: message, parse_mode: 'HTML')
-        # TelegramGroupChatNotificationJob.perform_now(target: "2192297", text: message, parse_mode: 'HTML')
+        TelegramGroupChatNotificationJob.perform_later(text: message)
       end
     end
   end
diff --git a/app/subscribers/telegram_bot_subscriber.rb b/app/subscribers/telegram_bot_subscriber.rb
index 88c02a6..9344a86 100644
--- a/app/subscribers/telegram_bot_subscriber.rb
+++ b/app/subscribers/telegram_bot_subscriber.rb
@@ -30,8 +30,7 @@ class TelegramBotSubscriber
     message = "<b><a href=\"#{conference_session_url(session.conference, session, host: 'rescheduled.c3lingo.org', protocol: 'https')}\">Session #{session.title}</a> #{action}</b>\n" +
       changes.map { |attr, (from, to)| "- #{attr}: #{from} -> #{to}" }.join("\n") + "\n#{conference_session_url(session.conference, session)}"
 
-    TelegramGroupChatNotificationJob.perform_later(target: "-316096320", text: message, parse_mode: 'HTML')
-    # TelegramGroupChatNotificationJob.perform_later(target: "2192297", text: message, parse_mode: 'HTML')
+    TelegramGroupChatNotificationJob.perform_later(text: message)
   end
 
   def handle_session_speaker_event(event)
@@ -45,6 +44,6 @@ class TelegramBotSubscriber
     message = "<b><a href=\"#{conference_session_url(session.conference, session, host: 'rescheduled.c3lingo.org', protocol: 'https')}\">Session #{session.title}</a> Speaker Change</b>\n" +
       "#{session_speaker.speaker.name} #{action == 'destroyed' ? 'removed' : 'added'}" + "\n#{conference_session_url(session.conference, session)}"
 
-    TelegramGroupChatNotificationJob.perform_later(target: "-316096320", text: message, parse_mode: 'HTML')
+    TelegramGroupChatNotificationJob.perform_later(text: message)
   end
 end
diff --git a/config/environments/development.rb b/config/environments/development.rb
index d27cc5b..7b58374 100644
--- a/config/environments/development.rb
+++ b/config/environments/development.rb
@@ -73,6 +73,8 @@ Rails.application.configure do
 
   # Raise error when a before_action's only/except options reference missing actions
   config.action_controller.raise_on_missing_callback_actions = true
+
+  config.telegram_default_target = ENV["TELEGRAM_DEFAULT_TARGET"] || "2192297"
 end
 
 Rails.application.routes.default_url_options[:host] = '127.0.0.1'
diff --git a/config/environments/production.rb b/config/environments/production.rb
index 629f0a3..8e5dae5 100644
--- a/config/environments/production.rb
+++ b/config/environments/production.rb
@@ -103,6 +103,8 @@ Rails.application.configure do
   # ]
   # Skip DNS rebinding protection for the default health check endpoint.
   # config.host_authorization = { exclude: ->(request) { request.path == "/up" } }
+
+  config.telegram_default_target = ENV["TELEGRAM_DEFAULT_TARGET"] || "-316096320"
 end
 
 # Rails.application.routes.default_url_options.merge({ host: 'rescheduled.c3lingo.org', protocol: 'https' })
diff --git a/config/initializers/fetch_credentials.rb b/config/initializers/fetch_credentials.rb
new file mode 100644
index 0000000..7787029
--- /dev/null
+++ b/config/initializers/fetch_credentials.rb
@@ -0,0 +1,5 @@
+def fetch_credential(key)
+  ENV[key.upcase] ||
+    Rails.application.credentials.dig(Rails.env.to_sym, key) ||
+    Rails.application.credentials[key.to_sym]
+end
diff --git a/db/seeds.rb b/db/seeds.rb
index 19ceddd..4d66821 100644
--- a/db/seeds.rb
+++ b/db/seeds.rb
@@ -78,7 +78,9 @@ end
   end
 end
 
-NotificationChannel.create!(
-  name: 'telegram_group_chat',
-  data: { token: '6001822848:AAGR0hPl3upppQAQy2VrJBHud466QVsBnyQ' }
-)
+if token = fetch_credential("telegram_bot_token")
+  NotificationChannel.find_or_create_by(name: "telegram_group_chat") do |c|
+    c.token = token
+    c.save!
+  end
+end
-- 
GitLab