Class: TelegramSupportBot::AutoAwayScheduler
- Inherits:
-
Object
- Object
- TelegramSupportBot::AutoAwayScheduler
- Defined in:
- lib/telegram_support_bot/auto_away_scheduler.rb
Instance Method Summary collapse
- #cancel_scheduled_task(user_message_id) ⇒ Object
-
#initialize(adapter, configuration) ⇒ AutoAwayScheduler
constructor
A new instance of AutoAwayScheduler.
- #schedule_auto_away_message(user_message_id, chat_id) ⇒ Object
Constructor Details
#initialize(adapter, configuration) ⇒ AutoAwayScheduler
Returns a new instance of AutoAwayScheduler.
3 4 5 6 7 |
# File 'lib/telegram_support_bot/auto_away_scheduler.rb', line 3 def initialize(adapter, configuration) @adapter = adapter @configuration = configuration @scheduled_tasks = {} end |
Instance Method Details
#cancel_scheduled_task(user_message_id) ⇒ Object
32 33 34 35 36 37 |
# File 'lib/telegram_support_bot/auto_away_scheduler.rb', line 32 def cancel_scheduled_task() if task = @scheduled_tasks[] task.kill # Terminate the scheduled thread @scheduled_tasks.delete() end end |
#schedule_auto_away_message(user_message_id, chat_id) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/telegram_support_bot/auto_away_scheduler.rb', line 9 def (, chat_id) # Cancel any existing scheduled task for this message to avoid duplicate messages cancel_scheduled_task() # Only proceed if auto-away is configured auto_away_interval = @configuration.auto_away_interval || 60 # Immediately store the thread object in the hash to mark this message ID as scheduled @scheduled_tasks[] = Thread.new do sleep(auto_away_interval) # Wait for the specified interval # After waking up, check if the task is still relevant if @scheduled_tasks[] (chat_id) notify_support_chat # Once the auto-away message has been sent, remove this task from the schedule @scheduled_tasks.delete() end end end |