-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
142 lines (116 loc) · 3.76 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package main
import (
"log"
"os"
"time"
"rss2telegram/internal/bot"
"rss2telegram/internal/config"
"rss2telegram/internal/rss"
"rss2telegram/internal/storage"
"rss2telegram/internal/stats"
)
type App struct {
bot *bot.Bot
rssManager *rss.Manager
config *config.Config
db *storage.Storage
}
func NewApp(cfg *config.Config, db *storage.Storage, stats *stats.Stats) (*App, error) {
bot, err := bot.NewBot(cfg.Telegram.BotToken, cfg.Telegram.Users, cfg.Telegram.Channels, db, cfg, "/app/config/config.yaml", stats)
if err != nil {
return nil, err
}
rssConfigs := make([]rss.Config, len(cfg.RSS))
for i, rssCfg := range cfg.RSS {
rssConfigs[i] = rss.Config{
URL: rssCfg.URL,
Interval: rssCfg.Interval,
Keywords: rssCfg.Keywords,
Group: rssCfg.Group,
}
}
rssManager := rss.NewManager(rssConfigs, db)
app := &App{
bot: bot,
rssManager: rssManager,
config: cfg,
db: db,
}
bot.SetMessageHandler(app.handleMessage)
bot.SetUpdateRSSHandler(app.updateRSS)
rssManager.SetMessageHandler(app.handleMessage)
return app, nil
}
func (app *App) handleMessage(title, url, group string, pubDate time.Time, matchedKeywords []string) error {
return app.bot.SendMessage(title, url, group, pubDate, matchedKeywords)
}
func (app *App) updateRSS() {
rssConfigs := make([]rss.Config, len(app.config.RSS))
for i, rssCfg := range app.config.RSS {
rssConfigs[i] = rss.Config{
URL: rssCfg.URL,
Interval: rssCfg.Interval,
Keywords: rssCfg.Keywords,
Group: rssCfg.Group,
}
}
app.rssManager.UpdateFeeds(rssConfigs)
log.Println("RSS订阅已更新")
}
func (app *App) Start() {
go app.bot.Start()
go app.rssManager.Start()
go app.watchConfig()
}
func (app *App) watchConfig() {
ticker := time.NewTicker(1 * time.Minute)
defer ticker.Stop()
for {
select {
case <-ticker.C:
newCfg, err := config.Load("/app/config/config.yaml")
if err != nil {
log.Printf("加载配置失败: %v", err)
continue
}
if !app.config.Equal(newCfg) {
log.Println("检测到配置变更,正在更新...")
app.config = newCfg
app.bot.UpdateConfig(newCfg)
app.updateRSS()
}
}
}
}
func main() {
log.SetFlags(log.LstdFlags | log.Lshortfile)
log.SetOutput(os.Stdout)
log.Println("启动 RSS 到 Telegram 机器人")
var cfg *config.Config
var err error
// 首先尝试从环境变量加载配置
cfg = config.LoadFromEnv()
// 如果环境变量中没有足够的配置信息,则尝试从配置文件加载
if cfg.Telegram.BotToken == "" || len(cfg.Telegram.Users) == 0 {
log.Println("环境变量中配置不完整,尝试从配置文件加载")
cfg, err = config.Load("/app/config/config.yaml")
if err != nil {
log.Fatalf("加载配置失败: %v", err)
}
}
// 打印加载的配置(注意不要打印敏感信息如 bot token)
log.Printf("加载的配置: Users: %v, Channels: %v", cfg.Telegram.Users, cfg.Telegram.Channels)
db := storage.NewStorage("/app/data/sent_items.txt")
stats, err := stats.NewStats("/app/data/stats.yaml")
if err != nil {
log.Fatalf("创建统计失败: %v", err)
}
app, err := NewApp(cfg, db, stats)
if err != nil {
log.Fatalf("创建应用失败: %v", err)
}
app.Start()
log.Println("机器人现在正在运行")
// 保持应用运行
select {}
}