Class: AgentsControl::Channels::Telegram::Bot

Inherits:
Object
  • Object
show all
Defined in:
lib/agents_control/channels/telegram/bot.rb

Overview

The long-polling loop.

Webhooks are deliberately not used: they need a public address and a certificate. Long polling works behind NAT, from a coffee shop, and with the laptop lid closed — everywhere this tool actually lives.

Constant Summary collapse

OFFSET_KEY =
"telegram:offset"
BACKOFF =

Pause after a network failure: grows toward a ceiling so a closed laptop doesn't hammer reconnects all night.

[1, 2, 5, 10, 30, 60].freeze

Instance Method Summary collapse

Constructor Details

#initialize(api:, router:, store:, config:, logger: $stdout) ⇒ Bot

Returns a new instance of Bot.



19
20
21
22
23
24
25
26
# File 'lib/agents_control/channels/telegram/bot.rb', line 19

def initialize(api:, router:, store:, config:, logger: $stdout)
  @api = api
  @router = router
  @store = store
  @config = config
  @logger = logger
  @running = false
end

Instance Method Details

#runObject



53
54
55
56
57
58
# File 'lib/agents_control/channels/telegram/bot.rb', line 53

def run
  start
  trap_signals
  log("bot started, listening for updates")
  wait
end

#running?Boolean

Returns:

  • (Boolean)


62
# File 'lib/agents_control/channels/telegram/bot.rb', line 62

def running? = @running

#startObject

Polling runs on its own thread while the main one waits for a stop signal: otherwise Ctrl-C has no effect while a long poll is hanging, and launchd gets to send SIGKILL first. Kept separate from wait so an interactive console can hold its own input loop while the bot runs alongside.



33
34
35
36
37
38
39
# File 'lib/agents_control/channels/telegram/bot.rb', line 33

def start
  @running = true
  @stopped = Thread::Queue.new
  @poller = Thread.new { poll_loop }

  self
end

#stopObject



60
# File 'lib/agents_control/channels/telegram/bot.rb', line 60

def stop = @stopped&.push(nil)

#waitObject

Wait for a stop. Returns false if the reason was a second instance on the same token.



43
44
45
46
47
48
49
50
51
# File 'lib/agents_control/channels/telegram/bot.rb', line 43

def wait
  reason = @stopped.pop

  @running = false
  @poller&.kill
  log(reason) if reason.is_a?(String)

  reason != :conflict
end