Documentation
¶
Overview ¶
Package telegram implements the Telegram Platform for Chatwright: an emulated Telegram Bot API server that delivers updates and captures the bot's outbound calls, normalized to Chatwright's neutral platform types.
The Telegram wire types come from the bots-go-framework platform adapter github.com/bots-go-framework/bots-api-telegram (tgbotapi), so Chatwright parses and builds messages exactly as the framework does. The bot under test remains free to be written in any language or framework — this server only speaks the Telegram Bot API over HTTP.
Index ¶
- Constants
- func Platform() platform.Platform
- type Emulator
- func (e *Emulator) BotAPIURL() string
- func (e *Emulator) Close()
- func (e *Emulator) Journal(chatID int64) ([]platform.JournalEntry, error)
- func (e *Emulator) SetWebhook(url string, client *http.Client)
- func (e *Emulator) SubmitClick(chatID int64, user platform.User, data string, targetMessageID int) error
- func (e *Emulator) SubmitText(chatID int64, user platform.User, text string) error
- func (e *Emulator) Transcript(chatID int64) string
- func (e *Emulator) WaitForEdit(chatID int64, messageID int, afterVersion int, timeout time.Duration) (*platform.Message, bool)
- func (e *Emulator) WaitForMessage(chatID int64, consumed int, timeout time.Duration) (*platform.Message, bool)
Constants ¶
const EmulatedBotUserID int64 = 1
EmulatedBotUserID is the Telegram user id this emulator always assigns to the single bot-under-test it simulates — the same id getMe returns and every outbound (bot-originated) message/edit is sent "from". The emulator does not yet distinguish multiple bot identities within one instance (see journalEntry's own doc comment on its recorded-but-unused token field), so this is the one value a caller needs to attribute any bot-originated platform.JournalEntry (via its FromID) to the bot.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Emulator ¶
type Emulator struct {
// contains filtered or unexported fields
}
Emulator is an in-process HTTP server emulating the Telegram Bot API. It owns delivery of updates to the bot-under-test: SubmitText/SubmitClick build the platform-native update and either push it to a configured webhook or queue it for the bot to retrieve via getUpdates — the harness never builds wire bytes or POSTs them itself.
func NewEmulator ¶
func NewEmulator() *Emulator
NewEmulator starts a fake Telegram Bot API server on a random local port.
func NewEmulatorAt ¶
NewEmulatorAt starts a fake Telegram Bot API server bound to addr (e.g. "127.0.0.1:54321") instead of a random local port. This is what lets an externally-started bot process — written in any language, since Chatwright only speaks HTTP — be configured with the emulator's exact API base URL before it starts: pick a free address once (e.g. bind to "127.0.0.1:0", read the assigned port back, then close it), pass that same address here and to the process's configuration, and the emulator ends up listening exactly where the process already expects it.
func (*Emulator) BotAPIURL ¶
BotAPIURL is the base URL the bot-under-test should use as its Telegram Bot API host, in place of https://api.telegram.org.
func (*Emulator) Journal ¶
func (e *Emulator) Journal(chatID int64) ([]platform.JournalEntry, error)
Journal returns chatID's chronological, structured journal entries — the same events Transcript renders as prose, given directly to callers (the observe package's Engine, diagnostics) that need to reason about them structurally.
func (*Emulator) SetWebhook ¶
SetWebhook registers the URL (and HTTP client) the emulator pushes updates to. Passing an empty url clears it, switching delivery back to queuing updates for getUpdates.
func (*Emulator) SubmitClick ¶
func (e *Emulator) SubmitClick(chatID int64, user platform.User, data string, targetMessageID int) error
SubmitClick delivers a user's button click (an interactive action activation) to the bot-under-test as a Telegram callback query. It does not reserve a message ID: a callback query references an existing message (targetMessageID) rather than creating a new one.
func (*Emulator) SubmitText ¶
SubmitText delivers a user's text message to the bot-under-test: it reserves the message's ID from chatID's shared sequence, journals the inbound event, builds the Telegram update, and delivers it.
func (*Emulator) Transcript ¶
Transcript renders a chronological, human-readable dump of everything recorded for chatID — inbound user messages, outbound bot messages (shown at their current, possibly-edited text) and button clicks — for inclusion in assertion failure messages. It is the emulator's own record, independent of what any BotMessage handle has consumed or asserted on. It renders from the same structured entries Journal returns, so the two never drift.