Class: TelegramBotEngine::Bot

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/telegram_bot_engine/bot.rb

Overview

A Telegram bot identity, persisted as data (docs/0001 §3.1). This is the gem’s next first-class entity beside Subscription/AllowedUser/Event: everything keyed by a *Telegram bot identity* lives here; only source/channel routing keyed by an *app source name* stays in the host app.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#skip_webhook_autoregisterObject

Transient: set on the implicit ensure_default! seed so a no-‘bot:` broadcast/notify never triggers a synchronous setWebhook from inside the back-compat path.



29
30
31
# File 'app/models/telegram_bot_engine/bot.rb', line 29

def skip_webhook_autoregister
  @skip_webhook_autoregister
end

Class Method Details

.defaultObject

The default Bot — the back-compat anchor every no-‘bot:` call site resolves to. Seeds one on first use so the host’s existing single-bot config keeps working.



56
57
58
# File 'app/models/telegram_bot_engine/bot.rb', line 56

def default
  find_by(default: true) || ensure_default!
end

.ensure_default!Object

Idempotently seed the default bot from the host’s existing single-bot config/ENV so nothing breaks on first boot (docs/0001 §4, §6). Safe to call repeatedly.



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'app/models/telegram_bot_engine/bot.rb', line 67

def ensure_default!
  existing = find_by(default: true)
  return existing if existing

  bot = new(
    name: default_seed_name,
    slug: "default",
    token: default_seed_token,
    active: true,
    default: true
  )
  # This is a lazy seed reachable from the back-compat broadcast/notify path; keep it
  # free of synchronous Telegram I/O. The host registers the default's webhook explicitly
  # (admin save, or WebhookRegistrar.register(Bot.default)) — see docs/0001 §3.6.
  bot.skip_webhook_autoregister = true
  bot.save!
  bot
end

.resolve(slug) ⇒ Object

A Bot by its stable slug handle (e.g. “default”, “assistant”).



61
62
63
# File 'app/models/telegram_bot_engine/bot.rb', line 61

def resolve(slug)
  find_by!(slug: slug)
end

Instance Method Details

#clientObject

The memoized Telegram::Bot::Client for this bot’s token, resolved via the registry.



107
108
109
# File 'app/models/telegram_bot_engine/bot.rb', line 107

def client
  TelegramBotEngine.client_for(self)
end