notify

package
v1.0.0-rc.26 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 13, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// LinkConnected 表示对端确实接上了(只有能判断这件事的适配器才会返回它)。
	LinkConnected = "connected"
	// LinkWaiting 表示实例在跑、但对端还没接上(QQ 绑好端口等 NapCat 握手就是这个态)。
	LinkWaiting = "waiting"
)

* 连通态取值。 * * 为什么不能直接拿 Healthy() 当「已连接」:这四个适配器里 Healthy() 的含义都只是 * **构造/启动成功**,不是对端真的接上了 —— * · QQ:监听端口一绑上就 healthy=true,而 NapCat 还没握手时发送会明确失败 * (「QQ 通道未连接 (NapCat 尚未握手)」);适配器自己的测试注释也写了这一点; * · Telegram:bot 造出来就 healthy=true,没做任何网络确认; * · Webhook:只判 config != nil; * · WeCom:恒为 true(它只是个出站 URL,没有长连接可言)。 * 所以把 Healthy() 映射成「已连接」会在界面上说一件不存在的事。

View Source
const (
	DigestWindow    = 30 * time.Second
	DigestThreshold = 5
	DigestMaxItems  = 50
)
View Source
const (
	EventRSSNew              = "rss.new"
	EventRSSMatched          = "rss.matched"
	EventRSSDigest           = "rss.digest"
	EventSubscribeDownloaded = "subscribe.downloaded"
	EventDoubanFailed        = "subscribe.douban_failed"
	EventMediaOrganized      = "media.organized"
	EventLoginExpiring       = "site.login_expiring"
	EventProbeFailed         = "site.probe_failed"
	EventProbeRecovered      = "site.probe_recovered"
	EventLoginTest           = "site.login_test"
	EventAttendanceDaily     = "attendance.daily"
	EventReportDaily         = "report.daily"
	EventDeadTorrents        = "downloader.dead_torrents"
	EventRemotePaired        = "remote.paired"
	EventTest                = "test"
)

事件类型。生产者用这些常量经 NewEvent 建事件;消息模板按它们一种一套。

Variables

View Source
var CommonVars = []Var{
	{Key: "event", Type: VarString, Desc: "事件类型", Sample: EventRSSMatched},
	{Key: "event_name", Type: VarString, Desc: "事件名称", Sample: "RSS 命中过滤规则"},
	{Key: "time", Type: VarString, Desc: "发生时间(本地时间,2006-01-02 15:04:05)", Sample: "2026-10-12 08:00:00"},
	{Key: "timestamp", Type: VarInt, Desc: "发生时间(秒级时间戳)", Sample: int64(1791763200)},
	{Key: "title", Type: VarString, Desc: "内置的标题"},
	{Key: "text", Type: VarString, Desc: "内置的正文"},
}

CommonVars 是每种事件都有的变量。

View Source
var ErrChannelTypeUnknown = errors.New("未知通知通道类型")
View Source
var Events = []EventSpec{
	{
		Type: EventRSSNew, Name: "RSS 新种子", Desc: "RSS 拉到新种子(通知方式选了「全部」)",
		Vars: []Var{
			{Key: "site", Type: VarString, Desc: "站点", Sample: "hdtime"},
			{Key: "rss", Type: VarString, Desc: "RSS 名称", Sample: "HDTime 电影"},
			{Key: "torrent_title", Type: VarString, Desc: "种子标题", Sample: "Fake.Movie.2026.1080p.WEB-DL.H.264-FAKE"},
			{Key: "torrent_id", Type: VarString, Desc: "种子编号", Sample: "300101"},
			{Key: "link", Type: VarString, Desc: "RSS 里这一条的链接", Sample: "https://hdtime.org/details.php?id=300101"},
			{Key: "published", Type: VarString, Desc: "发布时间", Sample: "2026-10-12 08:00"},
		},
		SampleTitle: "Fake.Movie.2026.1080p.WEB-DL.H.264-FAKE",
		SampleText:  "🆕 [hdtime] Fake.Movie.2026.1080p.WEB-DL.H.264-FAKE\n\n📅 2026-10-12 08:00\n🔗 https://hdtime.org/details.php?id=300101",
	},
	{
		Type: EventRSSMatched, Name: "RSS 命中过滤规则", Desc: "RSS 的种子命中了过滤规则(通知方式选了「命中规则」)",
		Vars: []Var{
			{Key: "site", Type: VarString, Desc: "站点", Sample: "hdtime"},
			{Key: "rss", Type: VarString, Desc: "RSS 名称", Sample: "HDTime 电影"},
			{Key: "torrent_title", Type: VarString, Desc: "种子标题", Sample: "Fake.Movie.2026.1080p.WEB-DL.H.264-FAKE"},
			{Key: "torrent_id", Type: VarString, Desc: "种子编号", Sample: "300101"},
			{Key: "subtitle", Type: VarString, Desc: "副标题", Sample: "假电影 | 中字"},
			{Key: "size", Type: VarString, Desc: "大小", Sample: "15.00 GB"},
			{Key: "size_bytes", Type: VarInt, Desc: "大小(字节)", Sample: int64(16106127360)},
			{Key: "free", Type: VarBool, Desc: "是不是免费", Sample: true},
			{Key: "free_end", Type: VarString, Desc: "免费到什么时候(本地时间;没有就是空)", Sample: "2026-10-13 08:00:00"},
			{Key: "free_remaining", Type: VarString, Desc: "免费还剩多久", Sample: "23h59min"},
			{Key: "rule", Type: VarString, Desc: "命中的过滤规则", Sample: "只要免费"},
			{Key: "link", Type: VarString, Desc: "种子详情页", Sample: "https://hdtime.org/details.php?id=300101"},
			{Key: "seeders", Type: VarInt, Desc: "做种数", Sample: int64(12)},
			{Key: "leechers", Type: VarInt, Desc: "下载数", Sample: int64(3)},
			{Key: "has_hr", Type: VarBool, Desc: "有没有 H&R", Sample: false},
		},
		SampleTitle: "Fake.Movie.2026.1080p.WEB-DL.H.264-FAKE",
		SampleText:  "🎯 [hdtime] Fake.Movie.2026.1080p.WEB-DL.H.264-FAKE\n\n📦 15.00 GB\n🆓 免费 (剩余 23h59min)\n📌 匹配规则:只要免费\n🔗 https://hdtime.org/details.php?id=300101",
	},
	{
		Type: EventRSSDigest, Name: "RSS 合并通知", Desc: "短时间里来了多条 RSS 通知,合成一条发",
		Vars: []Var{
			{Key: "count", Type: VarInt, Desc: "一共几条", Sample: int64(2)},
			{Key: "items", Type: VarList, Desc: "各条通知", Item: []Var{
				{Key: "title", Type: VarString, Desc: "这一条的内置标题"},
				{Key: "site", Type: VarString, Desc: "站点"},
				{Key: "torrent_title", Type: VarString, Desc: "种子标题"},
				{Key: "torrent_id", Type: VarString, Desc: "种子编号"},
			}, Sample: []map[string]any{
				{"title": "Fake.Movie.A.2026.1080p", "site": "hdtime", "torrent_title": "Fake.Movie.A.2026.1080p", "torrent_id": "300101"},
				{"title": "Fake.Movie.B.2026.2160p", "site": "hdtime", "torrent_title": "Fake.Movie.B.2026.2160p", "torrent_id": "300102"},
			}},
			more("省略的条数"),
		},
		SampleTitle: "RSS 摘要 · 2 条",
		SampleText:  "📦 2 条新通知\n\n1. Fake.Movie.A.2026.1080p\n2. Fake.Movie.B.2026.2160p\n",
	},
	{
		Type: EventSubscribeDownloaded, Name: "订阅下载", Desc: "订阅找到了资源并推送到下载器",
		Vars: []Var{
			{Key: "subscription", Type: VarString, Desc: "订阅(带年份与季)", Sample: "沙丘 (2021)"},
			{Key: "media_type", Type: VarString, Desc: "movie 或 tv", Sample: "movie"},
			{Key: "year", Type: VarInt, Desc: "年份", Sample: int64(2021)},
			{Key: "season", Type: VarInt, Desc: "季(电影是 0)", Sample: int64(0)},
			{Key: "episodes", Type: VarString, Desc: "这次下的集数(整季、第 3–5 集;电影是空)", Sample: ""},
			{Key: "torrent_title", Type: VarString, Desc: "种子标题", Sample: "Dune.2021.2160p.BluRay.x265-FAKE"},
			{Key: "site", Type: VarString, Desc: "站点", Sample: "hdtime"},
			{Key: "quality", Type: VarString, Desc: "质量(分辨率、来源、编码)", Sample: "2160p BluRay x265"},
			{Key: "poster", Type: VarString, Desc: "海报地址", Sample: "https://image.tmdb.org/t/p/w500/d5NXSklXo0qyIYkgV94XAgMIckC.jpg"},
		},
		SampleTitle: "订阅下载:沙丘 (2021)",
		SampleText:  "种子:Dune.2021.2160p.BluRay.x265-FAKE\n站点:hdtime\n质量:2160p BluRay x265\nhttps://image.tmdb.org/t/p/w500/d5NXSklXo0qyIYkgV94XAgMIckC.jpg",
	},
	{
		Type: EventDoubanFailed, Name: "豆瓣想看拉取失败", Desc: "豆瓣想看连续几次没拉到",
		Vars: []Var{
			{Key: "source", Type: VarString, Desc: "豆瓣用户", Sample: "我的豆瓣"},
			{Key: "fails", Type: VarInt, Desc: "连续失败次数", Sample: int64(3)},
			{Key: "error", Type: VarString, Desc: "最近一次的原因", Sample: "豆瓣返回 403"},
		},
		SampleTitle: "豆瓣想看拉取失败",
		SampleText:  "我的豆瓣:连续 3 次没拉到(豆瓣返回 403),之后按退避继续重试",
	},
	{
		Type: EventMediaOrganized, Name: "入库", Desc: "下载完的电影或剧集整理进了媒体库",
		Vars: []Var{
			{Key: "name", Type: VarString, Desc: "片名(带年份)", Sample: "沙丘 (2021)"},
			{Key: "media_type", Type: VarString, Desc: "movie 或 tv", Sample: "movie"},
			{Key: "episodes", Type: VarString, Desc: "入库的集(剧集才有)", Sample: ""},
			{Key: "files", Type: VarInt, Desc: "入库的文件数", Sample: int64(1)},
			{Key: "quality", Type: VarString, Desc: "画质", Sample: "2160p BluRay"},
			{Key: "library", Type: VarString, Desc: "媒体库", Sample: "电影"},
			{Key: "torrent", Type: VarString, Desc: "种子名", Sample: "Dune.2021.2160p.BluRay.x265-FAKE"},
			{Key: "poster", Type: VarString, Desc: "海报地址", Sample: "https://image.tmdb.org/t/p/w500/d5NXSklXo0qyIYkgV94XAgMIckC.jpg"},
		},
		SampleTitle: "入库:沙丘 (2021)",
		SampleText:  "电影\n画质:2160p BluRay\n媒体库:电影\n种子:Dune.2021.2160p.BluRay.x265-FAKE\n海报:https://image.tmdb.org/t/p/w500/d5NXSklXo0qyIYkgV94XAgMIckC.jpg",
	},
	{
		Type: EventLoginExpiring, Name: "站点即将被封号", Desc: "站点很久没有活跃,快到封号期限",
		Vars: []Var{
			{Key: "site", Type: VarString, Desc: "站点", Sample: "hdtime"},
			{Key: "tier", Type: VarString, Desc: "提醒档位", Sample: "d7"},
			{Key: "days_remaining", Type: VarInt, Desc: "还剩几天", Sample: int64(7)},
			{Key: "last_active", Type: VarString, Desc: "上次活跃(本地时间)", Sample: "2026-08-20 10:00:00"},
			{Key: "days_since_active", Type: VarInt, Desc: "距上次活跃几天", Sample: int64(53)},
			{Key: "access_stale", Type: VarBool, Desc: "自动访问刷新不了活跃时间、要手动登录", Sample: false},
		},
		SampleTitle: "[pt-tools] 站点 hdtime 即将被封号",
		SampleText:  "上次活跃 2026-08-20T02:00:00Z(53 天前,依据:站点用户信息);建议访问站点并浏览页面以刷新活跃时间(多数站点仅靠登录不更新 last_access)。剩余 7 天 (tier=d7)。",
	},
	{
		Type: EventProbeFailed, Name: "站点探测失败", Desc: "站点的登录状态探测持续失败",
		Vars: []Var{
			{Key: "site", Type: VarString, Desc: "站点", Sample: "hdtime"},
			{Key: "status", Type: VarString, Desc: "状态", Sample: "cookie_expired"},
			{Key: "status_label", Type: VarString, Desc: "状态说明", Sample: "Cookie 失效"},
			{Key: "since", Type: VarString, Desc: "从什么时候开始失败(本地时间)", Sample: "2026-10-12 06:00:00"},
			{Key: "advice", Type: VarString, Desc: "建议", Sample: "请重新同步 Cookie,或检查 API Key / Passkey 是否仍然有效。"},
			{Key: "diagnosis", Type: VarString, Desc: "诊断信息(可能是空)", Sample: "跳转到了登录页"},
		},
		SampleTitle: "[pt-tools] 站点 hdtime 探测失败",
		SampleText:  "状态:Cookie 失效(cookie_expired),自 2026-10-11T22:00:00Z 起持续失败。请重新同步 Cookie,或检查 API Key / Passkey 是否仍然有效。\n诊断:跳转到了登录页",
	},
	{
		Type: EventProbeRecovered, Name: "站点探测恢复", Desc: "探测失败以后又成功了",
		Vars: []Var{
			{Key: "site", Type: VarString, Desc: "站点", Sample: "hdtime"},
			{Key: "probed_at", Type: VarString, Desc: "最近一次成功的探测(本地时间)", Sample: "2026-10-12 09:00:00"},
		},
		SampleTitle: "[pt-tools] 站点 hdtime 探测已恢复",
		SampleText:  "最近一次探测成功(2026-10-12T01:00:00Z),登录状态已恢复正常。",
	},
	{
		Type: EventLoginTest, Name: "登录提醒测试", Desc: "在站点的登录状态里点了「测试提醒」",
		Vars: []Var{
			{Key: "site", Type: VarString, Desc: "站点", Sample: "hdtime"},
			{Key: "tier", Type: VarString, Desc: "当前提醒档位", Sample: "ok"},
			{Key: "days_remaining", Type: VarInt, Desc: "还剩几天", Sample: int64(60)},
			{Key: "last_active", Type: VarString, Desc: "当前判定的活跃时间(本地时间;不知道就是空)", Sample: "2026-10-12 08:00:00"},
		},
		SampleTitle: "[pt-tools][测试] 站点 hdtime 登录提醒",
		SampleText:  "这是一条测试提醒。当前判定活跃 2026-10-12T00:00:00Z,剩余 60 天 (tier=ok)。若你收到此消息,说明该站点的通知通道配置正常。",
	},
	{
		Type: EventAttendanceDaily, Name: "签到结果", Desc: "当天开启签到的站点都有结果以后的汇总",
		Vars: []Var{
			{Key: "day", Type: VarString, Desc: "日期", Sample: "2026-10-12"},
			{Key: "signed", Type: VarInt, Desc: "签到成功或已签到的站点数", Sample: int64(1)},
			{Key: "failed", Type: VarInt, Desc: "签到失败的站点数", Sample: int64(1)},
			{Key: "items", Type: VarList, Desc: "各站点", Item: []Var{
				{Key: "site", Type: VarString, Desc: "站点"},
				{Key: "status", Type: VarString, Desc: "状态(signed、already、failed、unsupported、pending)"},
				{Key: "status_label", Type: VarString, Desc: "状态说明"},
				{Key: "message", Type: VarString, Desc: "站点返回的说明或失败原因"},
			}, Sample: []map[string]any{
				{"site": "hdtime", "status": "signed", "status_label": "签到成功", "message": "获得 10 魔力"},
				{"site": "hdsky", "status": "failed", "status_label": "签到失败", "message": "站点超时"},
			}},
			more("省略的站点数"),
		},
		SampleTitle: "[pt-tools] 2026-10-12 签到结果",
		SampleText:  "· hdtime:签到成功(获得 10 魔力)\n· hdsky:签到失败(站点超时)",
	},
	{
		Type: EventReportDaily, Name: "每日战报", Desc: "每天定时发的上传、下载与魔力统计(登录异常、签到、刷流几段只在 text 里)",
		Vars: []Var{
			{Key: "day", Type: VarString, Desc: "日期", Sample: "2026-10-12"},
			{Key: "uploaded", Type: VarString, Desc: "今天合计上传", Sample: "120.00 GB"},
			{Key: "downloaded", Type: VarString, Desc: "今天合计下载", Sample: "30.00 GB"},
			{Key: "uploaded_bytes", Type: VarInt, Desc: "今天合计上传(字节)", Sample: int64(128849018880)},
			{Key: "downloaded_bytes", Type: VarInt, Desc: "今天合计下载(字节)", Sample: int64(32212254720)},
			{Key: "bonus", Type: VarFloat, Desc: "今天合计魔力", Sample: float64(1234.5)},
			{Key: "items", Type: VarList, Desc: "各站点(按上传从多到少)", Item: []Var{
				{Key: "site", Type: VarString, Desc: "站点"},
				{Key: "uploaded", Type: VarString, Desc: "上传"},
				{Key: "downloaded", Type: VarString, Desc: "下载"},
				{Key: "uploaded_bytes", Type: VarInt, Desc: "上传(字节)"},
				{Key: "downloaded_bytes", Type: VarInt, Desc: "下载(字节)"},
				{Key: "bonus", Type: VarFloat, Desc: "魔力"},
			}, Sample: []map[string]any{
				{"site": "HDTime", "uploaded": "100.00 GB", "downloaded": "20.00 GB", "uploaded_bytes": int64(107374182400), "downloaded_bytes": int64(21474836480), "bonus": float64(1000)},
				{"site": "HDSky", "uploaded": "20.00 GB", "downloaded": "10.00 GB", "uploaded_bytes": int64(21474836480), "downloaded_bytes": int64(10737418240), "bonus": float64(234.5)},
			}},
			more("省略的站点数"),
		},
		SampleTitle: "📊 每日战报 2026-10-12",
		SampleText:  "今日合计:上传 120.00 GB · 下载 30.00 GB · 魔力 1234.5\n· HDTime:上传 100.00 GB · 下载 20.00 GB · 魔力 1000\n· HDSky:上传 20.00 GB · 下载 10.00 GB · 魔力 234.5",
	},
	{
		Type: EventDeadTorrents, Name: "失效种子", Desc: "下载器助手扫到 tracker 报告未注册或不存在的种子",
		Vars: []Var{
			{Key: "downloader", Type: VarString, Desc: "下载器", Sample: "家里的 qB"},
			{Key: "count", Type: VarInt, Desc: "失效种子数", Sample: int64(2)},
			{Key: "size", Type: VarString, Desc: "合计大小", Sample: "30.0 GiB"},
			{Key: "size_bytes", Type: VarInt, Desc: "合计大小(字节)", Sample: int64(32212254720)},
			{Key: "items", Type: VarList, Desc: "各种子", Item: []Var{
				{Key: "name", Type: VarString, Desc: "种子名"},
				{Key: "size", Type: VarString, Desc: "大小"},
				{Key: "size_bytes", Type: VarInt, Desc: "大小(字节)"},
			}, Sample: []map[string]any{
				{"name": "Old.Movie.2019.1080p", "size": "20.0 GiB", "size_bytes": int64(21474836480)},
				{"name": "Old.Show.S01.720p", "size": "10.0 GiB", "size_bytes": int64(10737418240)},
			}},
			more("省略的种子数"),
		},
		SampleTitle: "下载器助手:发现失效种子",
		SampleText:  "家里的 qB 里有 2 个种子(共 30.0 GiB)的 tracker 报告未注册或不存在。到「下载器助手 → 失效种子」确认后再删除,这里不会自动删。\n· Old.Movie.2019.1080p\n· Old.Show.S01.720p",
	},
	{
		Type: EventRemotePaired, Name: "新设备配对", Desc: "手机 App 扫码配对成功",
		Vars: []Var{
			{Key: "device", Type: VarString, Desc: "设备名", Sample: "我的手机"},
			{Key: "permission", Type: VarString, Desc: "权限(完全控制或只读)", Sample: "完全控制"},
		},
		SampleTitle: "新设备已配对",
		SampleText:  "设备「我的手机」已经配对,权限:完全控制。如果不是你本人操作,请在「系统 → 远程访问」撤销这台设备。",
	},
	{
		Type: EventTest, Name: "通道测试", Desc: "在通知通道里点了「发送测试」",
		Vars: []Var{
			{Key: "channel", Type: VarString, Desc: "通知通道的名称", Sample: "我的 Telegram"},
		},
		SampleTitle: "pt-tools 测试通知",
		SampleText:  "如果你看到此消息,说明通道配置正常。",
	},
}

Events 是事件目录。改这里要同时改对应生产者(events_test 核对两边的变量一致)。

View Source
var WebhookVars = []Var{
	{Key: "channel", Type: VarString, Desc: "通知通道的名称", Sample: "我的 Webhook"},
}

WebhookVars 是只有 Webhook 请求体与地址里才有的变量。

Functions

func CombineDigest

func CombineDigest(items []DigestItem) (title, text string)

CombineDigest 将多个条目合并为单条消息。单条直接透传;多条生成编号摘要。 超过 DigestMaxItems 的部分会被截断并提示剩余数量。

func IsQuietNow

func IsQuietNow(now time.Time, start, end string) bool

IsQuietNow 判断 now 是否落入 [start, end) 的静默窗口(分钟粒度,不含秒)。 start/end 任一为空、格式非法、或两者相等时视为无静默。 当 start > end(如 22:00–08:00)窗口跨越午夜,等价于 [start,24:00) ∪ [00:00,end)。

func ListVars

func ListVars() map[string]bool

ListVars 是可以 range 的变量名。

func NextQuietEnd

func NextQuietEnd(now time.Time, end string) time.Time

NextQuietEnd 返回下一次静默结束时刻(>= now+1ns)。调用方应先确认当前处于静默时段。 当今天的 end 时刻已过,返回明天的 end 时刻;否则返回今天的 end 时刻。

func RegisterChannel

func RegisterChannel(typ string, factory Factory)

func RenderSample

func RenderSample(eventType string, kind tmpl.Kind, src string) (string, error)

RenderSample 用这种事件的示例数据渲染一份模板(变量名写错会报错),返回渲染结果:保存前的校验与预览用。 src 为空时返回内置文字的示例。

Types

type Button

type Button struct {
	Text         string `json:"text"`
	CallbackData string `json:"callback_data,omitempty"`
	URL          string `json:"url,omitempty"`
}

type Channel

type Channel interface {
	Type() string
	Init(ctx context.Context, conf *models.NotificationConf) error
	SupportsInbound() bool
	Send(ctx context.Context, n Notification) error
	OnInbound(handler InboundHandler)
	Close(ctx context.Context) error
	Healthy() bool
}

type ConfLister

type ConfLister interface {
	ListNotificationConfs(ctx context.Context) ([]models.NotificationConf, error)
}

ConfLister lists notification channel configurations available to the router.

type ConfigChecker

type ConfigChecker interface {
	CheckConfig(conf *models.NotificationConf) error
}

ConfigChecker 由能在不做任何 I/O 的前提下检查配置的适配器实现,是可选能力: 保存配置前调用,配置有误时直接拒绝保存,不等热重载时才失败。ConfigJSON 是明文。

type DedupCache

type DedupCache struct {
	// contains filtered or unexported fields
}

func NewDedupCache

func NewDedupCache(capacity int, window time.Duration) *DedupCache

func (*DedupCache) Seen

func (d *DedupCache) Seen(eventType, primaryID string, confID uint) bool

type DigestBuffer

type DigestBuffer struct {
	// contains filtered or unexported fields
}

DigestBuffer 按 NotificationConf ID 缓冲一段时间内的通知,达到 threshold 立即合并、 否则在 window 到期时合并刷写。

func NewDigestBuffer

func NewDigestBuffer(ctx context.Context, flush DigestFlushFunc) *DigestBuffer

func NewDigestBufferWithWindow

func NewDigestBufferWithWindow(ctx context.Context, flush DigestFlushFunc, window time.Duration, threshold int) *DigestBuffer

NewDigestBufferWithWindow 暴露 window/threshold 用于单测。

func (*DigestBuffer) Add

func (b *DigestBuffer) Add(confID uint, item DigestItem)

Add 入列一条 DigestItem。命中阈值立即异步刷写,否则在窗口到期时刷写。

func (*DigestBuffer) FlushAll

func (b *DigestBuffer) FlushAll()

FlushAll 强制刷写所有 pending 条目,用于优雅退出。

type DigestFlushFunc

type DigestFlushFunc func(ctx context.Context, confID uint, items []DigestItem)

DigestFlushFunc 在窗口结束(条数达阈值或定时器到期)时被调用,由实现方完成 实际推送以及 rss_notification_log 行的结果更新。

type DigestItem

type DigestItem struct {
	LogID uint
	Title string
	Text  string
	// Event 是这一条自己的事件:只有一条时照它发,多条时从里面取站点与种子拼成 rss.digest
	Event *Event
}

DigestItem 是 DigestBuffer 内部的一条待合并通知。 LogID 指向触发该条目的 rss_notification_log 行 ID,flush 回调据此更新结果。

type Event

type Event struct {
	Type   string         `json:"type"`
	Time   time.Time      `json:"time"`
	Fields map[string]any `json:"fields,omitempty"`
}

Event 是一条通知对应的事件:类型、发生时间与结构化变量,给消息模板与 Webhook 请求体用。 只能经 NewEvent 建:变量按事件目录里声明的种类规范化(int → int64,float → float64,列表截到 tmpl.MaxList 项), JSON 往返(outbox、RSS 与监控通知的重试)以后用同一个函数规范化,所以实时发送与重试拿到的类型完全一样。

func NewEvent

func NewEvent(typ string, at time.Time, fields map[string]any) *Event

NewEvent 建一个事件。fields 的键要和事件目录里声明的变量一致:没声明的丢掉,没给的按零值补上。

func SampleEvent

func SampleEvent(typ string) (ev *Event, title, text string)

SampleEvent 是这种事件的示例(按目录里各变量的 Sample),与内置标题、正文的示例:预览与保存前的校验用。

func (*Event) Data

func (e *Event) Data(title, text string) map[string]any

Data 是渲染模板用的数据:先放好所有事件的全部变量的零值(Webhook 收到别的事件时不会打出 <no value>), 再放这个事件的变量与公共变量。title、text 是内置的标题与正文。

func (*Event) UnmarshalJSON

func (e *Event) UnmarshalJSON(b []byte) error

UnmarshalJSON 解出持久化的事件,按目录重新规范化。

type EventSpec

type EventSpec struct {
	Type        string `json:"type"`
	Name        string `json:"name"`
	Desc        string `json:"desc"`
	Vars        []Var  `json:"vars"`
	SampleTitle string `json:"sample_title"`
	SampleText  string `json:"sample_text"`
}

EventSpec 是一种事件:名称、说明、专有变量,以及界面上展示的内置标题与正文的示例(按 Sample 生成的样子)。

func Spec

func Spec(typ string) *EventSpec

Spec 是事件类型的说明,不认识的类型返回 nil。

type Factory

type Factory func() Channel

type InboundHandler

type InboundHandler func(ctx context.Context, msg InboundMessage) error

type InboundMessage

type InboundMessage struct {
	ChannelType   string `json:"channel_type"`
	SourceConfID  uint   `json:"source_conf_id"`
	ChannelUserID string `json:"channel_user_id"`
	Username      string `json:"username"`
	ChatID        string `json:"chat_id"`
	MessageType   string `json:"message_type,omitempty"` // "private" | "group" — used by reply routing
	Text          string `json:"text"`
	IsCallback    bool   `json:"is_callback"`
	CallbackData  string `json:"callback_data"`
}

type LinkStater added in v0.48.0

type LinkStater interface {
	LinkState() string
}

LinkStater 由**能判断对端是否真的接上**的适配器实现,是可选能力。 不实现它的适配器,上层只会说「运行中」,不会说「已连接」。

type LiveSender

type LiveSender interface {
	Send(ctx context.Context, confID uint, n Notification) error
}

LiveSender 经运行中的通道实例投递一条通知。

type Notification

type Notification struct {
	// Event 是这条通知对应的事件(消息模板与 Webhook 请求体用);没有时照内置的标题与正文发。
	Event             *Event            `json:"event,omitempty"`
	Title             string            `json:"title"`
	Text              string            `json:"text"`
	Image             string            `json:"image,omitempty"`
	Link              string            `json:"link,omitempty"`
	ChannelType       string            `json:"channel_type,omitempty"`
	SourceConfID      uint              `json:"source_conf_id,omitempty"`
	UserID            string            `json:"user_id,omitempty"`
	Targets           map[string]string `json:"targets,omitempty"`
	Buttons           [][]Button        `json:"buttons,omitempty"`
	DisableWebPreview bool              `json:"disable_web_preview,omitempty"`
	OriginalMessageID string            `json:"original_message_id,omitempty"`
}

type OutboxEnqueuer

type OutboxEnqueuer interface {
	Enqueue(ctx context.Context, confID uint, n Notification, errMsg string) error
}

OutboxEnqueuer persists failed deliveries for asynchronous retry.

type OutboxWorker

type OutboxWorker struct {
	// contains filtered or unexported fields
}

OutboxWorker scans pending notification_outbox rows and retries delivery with bounded exponential backoff.

func NewOutboxWorker

func NewOutboxWorker(db *gorm.DB, registry *Registry, interval time.Duration) *OutboxWorker

NewOutboxWorker creates a worker. interval defaults to 10s when <= 0.

func (*OutboxWorker) SetConfigDecrypter

func (w *OutboxWorker) SetConfigDecrypter(fn func(string) (string, error))

SetConfigDecrypter 设置回退路径解密 ConfigJSON 的函数。须在 Start 之前调用。

func (*OutboxWorker) SetLiveSender

func (w *OutboxWorker) SetLiveSender(s LiveSender)

SetLiveSender 让重试经运行中的通道投递,不再按配置另建实例。须在 Start 之前调用。

func (*OutboxWorker) SetTemplates

func (w *OutboxWorker) SetTemplates(t *Templates)

SetTemplates 设置回退路径套用的全局消息模板。须在 Start 之前调用。

func (*OutboxWorker) Start

func (w *OutboxWorker) Start(ctx context.Context)

Start launches one ticker goroutine. Repeated calls do not start additional workers.

func (*OutboxWorker) Stop

func (w *OutboxWorker) Stop()

Stop cancels the worker and waits up to 1s for the goroutine to exit.

func (*OutboxWorker) Tick

func (w *OutboxWorker) Tick(ctx context.Context) error

Tick performs one scan of due pending rows and attempts delivery.

type Registry

type Registry struct {
	// contains filtered or unexported fields
}

func DefaultRegistry

func DefaultRegistry() *Registry

func NewRegistry

func NewRegistry() *Registry

func (*Registry) Make

func (r *Registry) Make(typ string) (Channel, error)

func (*Registry) Register

func (r *Registry) Register(typ string, factory Factory)

func (*Registry) Types

func (r *Registry) Types() []string

type RouteScope

type RouteScope struct {
	ConfIDs    []uint
	EventType  string
	PrimaryID  string
	SkipDedupe bool
}

type Router

type Router struct {
	// contains filtered or unexported fields
}

Router fans a notification out to enabled channel configurations, while keeping per-configuration channel instances private and reusable.

func NewRouter

func NewRouter(registry *Registry, outbox OutboxEnqueuer, confs ConfLister) *Router

func (*Router) Route

func (r *Router) Route(ctx context.Context, n Notification, scope RouteScope) error

type TemplateSource

type TemplateSource struct {
	EventType string
	Title     string
	Text      string
}

TemplateSource 是一种事件的全局消息模板原文。Title、Text 空着的一项用内置文字。

type Templates

type Templates struct {
	// contains filtered or unexported fields
}

Templates 是内存里的全局消息模板(按事件类型一套):启动时从库里读入,保存或恢复默认时整体替换。并发安全。 发送前经 Apply 套用;模板渲染出错或结果为空时保留内置文字,通知不会因为模板写坏而发不出去。

func NewTemplates

func NewTemplates(logf func(format string, args ...any)) *Templates

NewTemplates 建一个空的模板集(全部用内置文字)。logf 为 nil 时不记警告。

func (*Templates) Apply

func (t *Templates) Apply(n Notification) Notification

Apply 按 n.Event 的类型套用模板,重新生成标题与正文;没有事件、没有模板、渲染出错或结果为空时保留原来的。 按钮等其余字段原样保留。

func (*Templates) Replace

func (t *Templates) Replace(srcs []TemplateSource)

Replace 整体换成 srcs。每份都按防护规则重新解析,不合格的当没有模板并记一条警告(库里的数据可能早于现在的规则)。

type Var

type Var struct {
	Key    string  `json:"key"`
	Type   VarType `json:"type"`
	Desc   string  `json:"desc"`
	Sample any     `json:"sample,omitempty"`
	Item   []Var   `json:"item,omitempty"`
}

Var 是一个模板变量。列表的每一项是对象,字段在 Item 里。

type VarType

type VarType string

VarType 是变量的种类。数字分整数与浮点数:规范化按声明转成 int64 或 float64,不看字面上有没有小数点。

const (
	VarString VarType = "string"
	VarInt    VarType = "int"
	VarFloat  VarType = "float"
	VarBool   VarType = "bool"
	VarList   VarType = "list"
)

Directories

Path Synopsis
adapter
bark
Package bark 是 Bark(iOS 推送)出站通道:POST {server}/push,JSON 里带 device_key;返回 code 为 200 才算成功。
Package bark 是 Bark(iOS 推送)出站通道:POST {server}/push,JSON 里带 device_key;返回 code 为 200 才算成功。
dingtalk
Package dingtalk 是钉钉群自定义机器人出站通道。
Package dingtalk 是钉钉群自定义机器人出站通道。
feishu
Package feishu 是飞书(Lark)群自定义机器人出站通道。
Package feishu 是飞书(Lark)群自定义机器人出站通道。
ntfy
Package ntfy 是 ntfy 出站通道:按 JSON 发布,POST 到服务器根地址 {topic, title, message, priority, click}; 可选 access token(Bearer)或用户名密码(Basic)。
Package ntfy 是 ntfy 出站通道:按 JSON 发布,POST 到服务器根地址 {topic, title, message, priority, click}; 可选 access token(Bearer)或用户名密码(Basic)。
outbound
Package outbound 是只出站的通知适配器(Bark、Server 酱、ntfy、钉钉、飞书、企业微信群机器人)共用的发送工具。
Package outbound 是只出站的通知适配器(Bark、Server 酱、ntfy、钉钉、飞书、企业微信群机器人)共用的发送工具。
qq
serverchan
Package serverchan 是 Server 酱出站通道。
Package serverchan 是 Server 酱出站通道。
telegram
Package telegram implements the notify.Channel adapter for Telegram bots using mymmrac/telego with long-polling.
Package telegram implements the notify.Channel adapter for Telegram bots using mymmrac/telego with long-polling.
Package tmpl 是通知的模板引擎:Go 的 text/template,加上一组受控的函数与防护。
Package tmpl 是通知的模板引擎:Go 的 text/template,加上一组受控的函数与防护。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL