init 命令路由
()
| 65 | |
| 66 | // init 命令路由 |
| 67 | func init() { |
| 68 | rssRepo, initErr = domain.NewRssDomain(engine.DataFolder() + "rsshub.db") |
| 69 | if initErr != nil { |
| 70 | logrus.Errorln("RssHub订阅姬:初始化失败", initErr) |
| 71 | panic(initErr) |
| 72 | } |
| 73 | engine.OnFullMatch("rsshub同步", zero.OnlyGroup).SetBlock(true).Handle(func(ctx *zero.Ctx) { |
| 74 | // 群组-频道推送视图 map[群组]推送内容数组 |
| 75 | groupToFeedsMap, err := rssRepo.Sync(context.Background()) |
| 76 | if err != nil { |
| 77 | logrus.Errorln("rsshub同步失败", err) |
| 78 | ctx.SendPrivateMessage(zero.BotConfig.SuperUsers[0], message.Text("rsshub同步失败", err)) |
| 79 | return |
| 80 | } |
| 81 | // 没有更新的[群组-频道推送视图]则不推送 |
| 82 | if len(groupToFeedsMap) == 0 { |
| 83 | logrus.Info("rsshub未发现更新") |
| 84 | return |
| 85 | } |
| 86 | sendRssUpdateMsg(ctx, groupToFeedsMap) |
| 87 | }) |
| 88 | // 添加订阅 |
| 89 | engine.OnRegex(`^添加rsshub订阅-(.+)$`, zero.OnlyGroup).SetBlock(true).Handle(func(ctx *zero.Ctx) { |
| 90 | routeStr := ctx.State["regex_matched"].([]string)[1] |
| 91 | input := regexpForSQL.ReplaceAllString(routeStr, "") |
| 92 | logrus.Debugf("添加rsshub订阅:raw(%s), replaced(%s)", routeStr, input) |
| 93 | rv, _, isSubExisted, err := rssRepo.Subscribe(context.Background(), ctx.Event.GroupID, input) |
| 94 | if err != nil { |
| 95 | ctx.SendChain(message.Text("RssHub订阅姬:添加失败", err.Error())) |
| 96 | return |
| 97 | } |
| 98 | if isSubExisted { |
| 99 | ctx.SendChain(message.Text("RssHub订阅姬:已存在,更新成功")) |
| 100 | } else { |
| 101 | ctx.SendChain(message.Text("RssHub订阅姬:添加成功\n", rv.Source.Title)) |
| 102 | } |
| 103 | // 添加成功,发送订阅源快照 |
| 104 | msg, err := newRssDetailsMsg(ctx, rv) |
| 105 | if len(msg) == 0 || err != nil { |
| 106 | ctx.SendPrivateMessage(zero.BotConfig.SuperUsers[0], message.Text("RssHub推送错误", err)) |
| 107 | return |
| 108 | } |
| 109 | if id := ctx.Send(msg).ID(); id == 0 { |
| 110 | ctx.SendChain(message.Text("ERROR: 发送订阅源快照失败,可能被风控了")) |
| 111 | } |
| 112 | }) |
| 113 | engine.OnRegex(`^删除rsshub订阅-(.+)$`, zero.OnlyGroup).SetBlock(true).Handle(func(ctx *zero.Ctx) { |
| 114 | routeStr := ctx.State["regex_matched"].([]string)[1] |
| 115 | input := regexpForSQL.ReplaceAllString(routeStr, "") |
| 116 | logrus.Debugf("删除rsshub订阅:raw(%s), replaced(%s)", routeStr, input) |
| 117 | err := rssRepo.Unsubscribe(context.Background(), ctx.Event.GroupID, input) |
| 118 | if err != nil { |
| 119 | ctx.SendChain(message.Text("RssHub订阅姬:删除失败 ", err.Error())) |
| 120 | return |
| 121 | } |
| 122 | ctx.SendChain(message.Text(fmt.Sprintf("RssHub订阅姬:删除%s成功", input))) |
| 123 | }) |
| 124 | engine.OnFullMatch("查看rsshub订阅列表", zero.OnlyGroup).SetBlock(true).Handle(func(ctx *zero.Ctx) { |
nothing calls this directly
no test coverage detected