diff --git a/app/subscribers/telegram_bot_subscriber.rb b/app/subscribers/telegram_bot_subscriber.rb new file mode 100644 index 0000000000000000000000000000000000000000..bbbd6068764a2212ef04f46fe9c049f1021b2add --- /dev/null +++ b/app/subscribers/telegram_bot_subscriber.rb @@ -0,0 +1,31 @@ +class TelegramBotSubscriber + def self.subscribe + ActiveSupport::Notifications.subscribe(/republica\.import/) do |*args| + event = ActiveSupport::Notifications::Event.new(*args) + new.handle_event(event) # Call the instance method + end + end + + def handle_event(event) + # 1. Extract Relevant Information + model_name = event.name.split(".").last # Get "session" or "speaker" + record = event.payload[:record] + changes = event.payload[:changes] + + # 2. Format Message for Telegram + message = format_telegram_message(model_name, record, changes) + + # 3. Send Message via Telegram Bot API + # (Replace with your actual Telegram bot API integration) + # TelegramBotAPI.sendMessage(chat_id: YOUR_CHAT_ID, text: message) + Rails.logger.info("event: #{message}") + end + + private + + def format_telegram_message(model_name, record, changes) + # Customize this to your desired message format + "**#{model_name.capitalize} Updated:**\n\n" + + changes.map { |attr, (from, to)| "- #{attr}: #{from} -> #{to}" }.join("\n") + end +end diff --git a/config/initializers/subscribers.rb b/config/initializers/subscribers.rb new file mode 100644 index 0000000000000000000000000000000000000000..cc511bfbc2d1d31c4f65ecfa6931fe2ae62a691d --- /dev/null +++ b/config/initializers/subscribers.rb @@ -0,0 +1,3 @@ +Rails.application.config.to_prepare do + TelegramBotSubscriber.subscribe # Subscribe to notifications +end \ No newline at end of file