diff --git a/app/jobs/telegram_group_chat_notification_job.rb b/app/jobs/telegram_group_chat_notification_job.rb index e588bd97dc7e305a4f035002b629db97a968c09b..f55fef9a7640d420cb90d910be3e595536f86850 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 5f06d8dd73a605e26a77363d7af75256d35835a1..1b34d4591a47707f15448a7562563c8631b2a2ea 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 88c02a6fa9b8ccc95ffa2baea35083b5b6b43df7..9344a86af2f6e748d706a613d869d8accd0899b2 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 d27cc5bb53adb3a8f590e4d71f84fe8f501f3027..7b58374f042dbe338dfce8873ca6e9839345e1dd 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 629f0a34616f1a2e5ca9b8a5ebb5b4d64992a30c..8e5dae54a6e0f9773e17f10351224695ebd5b19b 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 0000000000000000000000000000000000000000..778702965f870833f2c444d549fe3029e0f453a4 --- /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 19ceddda7f73a3987da3251560c93589796c2f87..4d668213603d8f3d8c3256861853f9acad08cdbf 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