diff --git a/app/jobs/telegram_notify_upcoming_job.rb b/app/jobs/telegram_notify_upcoming_job.rb new file mode 100644 index 0000000000000000000000000000000000000000..acd2c67656d7529eb468ec8b7f8088ee8e7f3eeb --- /dev/null +++ b/app/jobs/telegram_notify_upcoming_job.rb @@ -0,0 +1,20 @@ +class TelegramNotifyUpcomingJob < ApplicationJob + queue_as :default + + def perform(**args) + Session.scheduled.includes(:stage, :assignments).where(stage: { name: ["Stage 1", "Stage 2", "Standby"] }).where(starts_at: Time.now..(Time.now + args[:interval])).order(:starts_at).each do |session| + assignees = session.assignments.map(&:user) + if assignees.length.positive? + notify_names = assignees.map { |a| a.telegram_username ? "@#{a.telegram_username}" : a.name } + + 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>" + TelegramGroupChatNotificationJob.perform_later(target: "-316096320", text: message, parse_mode: 'HTML') + # TelegramGroupChatNotificationJob.perform_now(target: "2192297", text: message, parse_mode: 'HTML') + 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>" + TelegramGroupChatNotificationJob.perform_later(target: "-316096320", text: message, parse_mode: 'HTML') + # TelegramGroupChatNotificationJob.perform_now(target: "2192297", text: message, parse_mode: 'HTML') + end + end + end +end diff --git a/config/cronotab.rb b/config/cronotab.rb index 976cc9776733843ffa1f1641ca7451ef83c8654f..04bbfe168995d6dce881cec7131ab97b8b960097 100644 --- a/config/cronotab.rb +++ b/config/cronotab.rb @@ -15,3 +15,4 @@ # Crono.perform(FetchConferenceDataJob, 'rp2024').every 5.minutes +Crono.perform(TelegramNotifyUpcomingJob, interval: 15.minutes).every 15.minutes diff --git a/test/jobs/telegram_notify_upcoming_job_test.rb b/test/jobs/telegram_notify_upcoming_job_test.rb new file mode 100644 index 0000000000000000000000000000000000000000..811f6e47cd0e40d6a06a1f75db76cd4bdaf34163 --- /dev/null +++ b/test/jobs/telegram_notify_upcoming_job_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class TelegramNotifyUpcomingJobTest < ActiveJob::TestCase + # test "the truth" do + # assert true + # end +end