-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
58 lines (48 loc) · 1.15 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
package main
import (
"context"
"github.com/Ccheers/wxbot/internal/biz"
"github.com/Ccheers/wxbot/internal/cron"
"github.com/Ccheers/wxbot/internal/handler"
"github.com/Ccheers/wxbot/internal/handler/middleware/event"
"github.com/Ccheers/wxbot/wcbot"
"github.com/sirupsen/logrus"
)
var (
bot *wcbot.WcBot
)
func newHandler(bot *wcbot.WcBot, jobUseCase *biz.JobUseCase, c *cron.Cron) *handler.WeChatBot {
h := handler.NewWeChatBot(
bot,
handler.WithMiddleware(
event.NewEventServer(jobUseCase, bot),
),
)
jobs, err := jobUseCase.GetAllJobs(context.Background())
if err != nil {
panic(err)
}
for _, job := range jobs {
cid, err := c.AddCron(job.CronExpress, jobUseCase.WithCronFunc(job.ID))
if err != nil {
logrus.Error(err)
continue
}
job.CronID = cid
_, err = jobUseCase.UpdateJob(context.Background(), job)
if err != nil {
logrus.Error(err)
continue
}
logrus.Infof("add cron %+v", job)
}
return h
}
func main() {
bot = wcbot.New()
bot.Debug = true
bot.QrCodeInTerminal() //默认在 wxqr 目录生成二维码,调用此函数,在终端打印二维码
h := initBot(bot)
bot.AddHandler(h)
bot.Run()
}