Documentation
¶
Overview ¶
Package telegram implements the notify.Channel adapter for Telegram bots using mymmrac/telego with long-polling. Each NotificationConf gets its own bot instance and goroutine; Init failures are non-fatal so that application boot is never blocked by a misconfigured channel.
Index ¶
- Constants
- func EscapeMarkdownV2(s string) string
- type CallbackActionHandler
- type Config
- type TelegramChannel
- func (c *TelegramChannel) Close(ctx context.Context) error
- func (c *TelegramChannel) Healthy() bool
- func (c *TelegramChannel) Init(ctx context.Context, conf *models.NotificationConf) error
- func (c *TelegramChannel) OnInbound(handler notify.InboundHandler)
- func (c *TelegramChannel) Send(ctx context.Context, n notify.Notification) error
- func (c *TelegramChannel) SetCallbackActionHandler(h CallbackActionHandler)
- func (c *TelegramChannel) SupportsInbound() bool
- func (c *TelegramChannel) Type() string
Constants ¶
const ChannelType = "telegram"
ChannelType is the registry key for the Telegram adapter.
Variables ¶
This section is empty.
Functions ¶
func EscapeMarkdownV2 ¶
EscapeMarkdownV2 escapes Telegram MarkdownV2 reserved characters. Not used by the default Send path on purpose: torrent titles routinely contain `_`, `[`, `]`, `*` etc. and accidental escaping disfigures filenames.
Types ¶
type CallbackActionHandler ¶
type CallbackActionHandler interface {
OnRSSDownload(ctx context.Context, logID uint, userID int64) error
OnRSSIgnore(ctx context.Context, logID uint, userID int64) error
}
CallbackActionHandler dispatches RSS notification button actions parsed from the inline keyboard payload. Set on the channel via SetCallbackActionHandler. If nil, callbacks are acknowledged but no downstream action is taken (stub mode for testing / pre-S5 boots).
type Config ¶
type Config struct {
BotToken string `json:"bot_token"`
AllowedUsers json.RawMessage `json:"allowed_users,omitempty"`
AdminUsers json.RawMessage `json:"admin_users,omitempty"`
DefaultChatID json.RawMessage `json:"default_chat_id,omitempty"`
PollingTimeoutSeconds int `json:"polling_timeout_seconds"`
APIServer string `json:"api_server,omitempty"`
ProxyURL string `json:"proxy_url,omitempty"`
}
Config is the JSON shape stored (encrypted) in NotificationConf.ConfigJSON.
AllowedUsers / AdminUsers / DefaultChatID are stored as json.RawMessage so that the adapter tolerates both numeric and string-quoted values that users commonly type into the Web UI form (e.g. `"8576996727"` or `"@channel"`). Use the helper methods (DefaultChatIDInt, DefaultChatIDUsername, AdminUsersList, AllowedUsersList) to access typed values.
func (*Config) AdminUsersList ¶ added in v0.31.2
AdminUsersList returns admin_users as []int64. Tolerates both raw integer and string-quoted integers in the JSON array; bad entries are silently skipped to avoid taking down the whole channel for one typo.
func (*Config) AllowedUsersList ¶ added in v0.31.2
AllowedUsersList returns allowed_users as []int64 with the same tolerance as AdminUsersList.
func (*Config) DefaultChatIDInt ¶ added in v0.31.2
DefaultChatIDInt returns the integer form of default_chat_id. Accepts both raw JSON integers and string-quoted integers ("123456"). Returns (0, false) when the value is absent or is a non-numeric string (e.g. "@channelusername"), in which case the caller should fall back to DefaultChatIDUsername.
func (*Config) DefaultChatIDUsername ¶ added in v0.31.2
DefaultChatIDUsername returns the @channelusername form. Only returns a non-empty string when the stored value is a string that does NOT parse as an integer (so DefaultChatIDInt and DefaultChatIDUsername never both return a usable value for the same config).
type TelegramChannel ¶
type TelegramChannel struct {
// contains filtered or unexported fields
}
TelegramChannel implements notify.Channel for Telegram.
func (*TelegramChannel) Close ¶
func (c *TelegramChannel) Close(ctx context.Context) error
Close stops the long-poll loop and clears state.
func (*TelegramChannel) Healthy ¶
func (c *TelegramChannel) Healthy() bool
Healthy reports whether Init succeeded and the bot is currently usable.
func (*TelegramChannel) Init ¶
func (c *TelegramChannel) Init(ctx context.Context, conf *models.NotificationConf) error
Init parses the plaintext ConfigJSON (already decrypted by cmd/web.go), builds a per-conf telego bot, and launches the long-poll goroutine.
func (*TelegramChannel) OnInbound ¶
func (c *TelegramChannel) OnInbound(handler notify.InboundHandler)
OnInbound registers the handler invoked once a message passes the permission gate.
func (*TelegramChannel) Send ¶
func (c *TelegramChannel) Send(ctx context.Context, n notify.Notification) error
Send 不看 healthy:它跟着长轮询(收消息)走,getUpdates 返回 502/504、同一个 bot 在别处被轮询(409) 时为假,sendMessage 却不一定发不出去。原来按它拒发,长轮询出问题的几个小时里每条通知都报 not initialized、根本没去发。现在只在没初始化成功或已关闭时拒发,发不出去时报 Telegram 回的原因。
func (*TelegramChannel) SetCallbackActionHandler ¶
func (c *TelegramChannel) SetCallbackActionHandler(h CallbackActionHandler)
SetCallbackActionHandler installs the dispatcher for RSS notification inline-button actions. Safe to call before or after Init. Pass nil to revert to stub mode.
func (*TelegramChannel) SupportsInbound ¶
func (c *TelegramChannel) SupportsInbound() bool
SupportsInbound reports that Telegram delivers inbound chat messages.
func (*TelegramChannel) Type ¶
func (c *TelegramChannel) Type() string
Type returns the registry key.