Skip to content
Snippets Groups Projects
Commit dc801a1f authored by Teal's avatar Teal
Browse files

start subscriber

parent f066e785
No related branches found
No related tags found
No related merge requests found
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
Rails.application.config.to_prepare do
TelegramBotSubscriber.subscribe # Subscribe to notifications
end
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment