From eadc4a7f1b6a32333eab17189f5a51f9be0b613d Mon Sep 17 00:00:00 2001 From: Teal <git@teal.is> Date: Mon, 27 May 2024 10:30:20 +0200 Subject: [PATCH] fixes I hope --- .../telegram_group_chat_notification_job.rb | 6 +-- app/models/session_speaker.rb | 21 +++++++++ app/subscribers/telegram_bot_subscriber.rb | 45 ++++++++++++------- 3 files changed, 54 insertions(+), 18 deletions(-) diff --git a/app/jobs/telegram_group_chat_notification_job.rb b/app/jobs/telegram_group_chat_notification_job.rb index b8bb906..2000abf 100644 --- a/app/jobs/telegram_group_chat_notification_job.rb +++ b/app/jobs/telegram_group_chat_notification_job.rb @@ -5,11 +5,11 @@ class TelegramGroupChatNotificationJob < NotificationJob def perform(**args) channel = NotificationChannel.find_by(name: 'telegram_group_chat') - return unless channel.data - return + return unless channel&.data token = channel.data['token'] + return unless token Telegram::Bot::Client.run(token) do |bot| - bot.api.send_message(chat_id: args[:target], text: args[:text]) + bot.api.send_message(chat_id: args[:target], text: args[:text], parse_mode: args[:parse_mode]) end end end diff --git a/app/models/session_speaker.rb b/app/models/session_speaker.rb index b4bb73f..34125c2 100644 --- a/app/models/session_speaker.rb +++ b/app/models/session_speaker.rb @@ -1,4 +1,25 @@ class SessionSpeaker < ApplicationRecord belongs_to :session belongs_to :speaker + + after_create :notify_on_create + after_destroy :notify_on_destroy + + private + + def notify_on_create + Rails.logger.debug('session_speaker.created') + ActiveSupport::Notifications.instrument( + "session_speaker.created", + record: self + ) + end + + def notify_on_destroy + Rails.logger.debug('session_speaker.destroyed') + ActiveSupport::Notifications.instrument( + "session_speaker.destroyed", + record: self + ) + end end diff --git a/app/subscribers/telegram_bot_subscriber.rb b/app/subscribers/telegram_bot_subscriber.rb index a1eb5db..6fa8ba9 100644 --- a/app/subscribers/telegram_bot_subscriber.rb +++ b/app/subscribers/telegram_bot_subscriber.rb @@ -1,29 +1,44 @@ class TelegramBotSubscriber + include Rails.application.routes.url_helpers + def self.subscribe - ActiveSupport::Notifications.subscribe(/\A(?:session|speaker)\.updated/) do |*args| - event = ActiveSupport::Notifications::Event.new(*args) - new.handle_event(event) # Call the instance method + ActiveSupport::Notifications.subscribe(/\Asession\.updated\Z/) do |*args| + new.handle_session_updated(ActiveSupport::Notifications::Event.new(*args)) + end + ActiveSupport::Notifications.subscribe(/\Asession_speaker\.(?:created|destroyed)\Z/) do |*args| + new.handle_session_speaker_event(ActiveSupport::Notifications::Event.new(*args)) end end - def handle_event(event) - model_name, action = event.name.split(".") + def handle_session_updated(event) + model_name, action = event.name.split('.') record = event.payload[:record] changes = event.payload[:changes] - message = format_telegram_message(model_name, action, record, changes) + return unless changes.any? do |attr, (from, to)| + %w[title language status starts_at ends_at stage_id].include? attr + end + + if changes["stage_id"] + changes["stage"] = [Stage.find(changes["stage_id"].first).name, Stage.find(changes["stage_id"].last).name] + changes.delete("stage_id") + end + + message = "<b><a href=\"#{conference_session_path(record.conference, record)}\">Session #{record.title}</a> #{action}</b>\n" + + changes.map { |attr, (from, to)| "- #{attr}: #{from} \\-> #{to}" }.join("\n") - # TelegramBotAPI.sendMessage(chat_id: YOUR_CHAT_ID, text: message) - Rails.logger.info("event: #{message}") - #TelegramGroupChatNotificationJob.perform_later(target: "-316096320", text: message) - TelegramGroupChatNotificationJob.perform_later(target: "2192297", text: message) + TelegramGroupChatNotificationJob.perform_later(target: "-316096320", text: message, parse_mode: 'HTML') + # TelegramGroupChatNotificationJob.perform_later(target: "2192297", text: message, parse_mode: 'HTML') end - private + def handle_session_speaker_event(event) + model_name, action = event.name.split('.') + record = event.payload[:record] + session = record.session + + message = "<b><a href=\"#{conference_session_path(session.conference, session)}\">Session #{session.title}</a> Speaker Change</b>\n" + + "#{record.speaker.name} #{action == 'destroyed' ? 'removed' : 'added'}" - def format_telegram_message(model_name, action, record, changes) - # Customize this to your desired message format - "**#{model_name.capitalize} #{record.id} #{action}:**\n\n" + - changes.map { |attr, (from, to)| "- #{attr}: #{from} -> #{to}" }.join("\n") + TelegramGroupChatNotificationJob.perform_later(target: "-316096320", text: message, parse_mode: 'HTML') end end -- GitLab