Class: TelegramSupportBot::AutoAwayScheduler

Inherits:
Object
  • Object
show all
Defined in:
lib/telegram_support_bot/auto_away_scheduler.rb

Instance Method Summary collapse

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(user_message_id)
  if task = @scheduled_tasks[user_message_id]
    task.kill # Terminate the scheduled thread
    @scheduled_tasks.delete(user_message_id)
  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 schedule_auto_away_message(user_message_id, chat_id)
  # Cancel any existing scheduled task for this message to avoid duplicate messages
  cancel_scheduled_task(user_message_id)

  # 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[user_message_id] = 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[user_message_id]
      send_auto_away_message(chat_id)
      notify_support_chat

      # Once the auto-away message has been sent, remove this task from the schedule
      @scheduled_tasks.delete(user_message_id)
    end
  end
end