-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
87 lines (69 loc) · 2.36 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
package main
import (
"flag"
"fmt"
"github.com/bells307/gitlab-watcher/config"
"github.com/bells307/gitlab-watcher/internal/adapters/template"
"log"
"github.com/bells307/gitlab-watcher/internal/adapters/sender"
tm "github.com/and3rson/telemux/v2"
"github.com/bells307/gitlab-watcher/internal/application"
"github.com/bells307/gitlab-watcher/internal/infrastructure/delivery/http"
"github.com/bells307/gitlab-watcher/internal/infrastructure/delivery/tg"
"github.com/gin-gonic/gin"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
func main() {
cfgPath := flag.String("config", "./config.yml", "config file path")
flag.Parse()
log.Printf("loading configuration ...")
cfg, err := config.LoadConfig(*cfgPath)
if err != nil {
log.Fatal("can't load configuration:", err)
}
var hookServices []http.HookService
if cfg.Telegram != nil {
hookServices = append(hookServices, createTelegramService(cfg.Telegram, cfg.SendTemplateErrors))
}
router := gin.Default()
hookHandler := http.NewHookHandler(hookServices)
hookHandler.Register(router)
log.Printf("start listening on %s ...", cfg.Listen)
if err = router.Run(cfg.Listen); err != nil {
log.Fatalf("error starting http listener: %v", err)
}
}
// TODO: вынести из main.go
func createTelegramService(cfg *config.TelegramConfig, sendTemplateErrors bool) http.HookService {
api, err := tgbotapi.NewBotAPI(cfg.Token)
if err != nil {
log.Fatal("can't create telegram bot api:", err)
}
if cfg.Debug {
api.Debug = true
}
telegramBot := sender.NewTelegramBot(api, cfg.Chats, cfg.ParseMode)
updateHandler := tg.NewUpdateHandler(telegramBot)
mux := tm.NewMux()
updateHandler.Register(api, mux)
telegramHookTemplater := template.NewHookTextTemplater(cfg.Templates)
telegramHookTemplater.AddFunc("mapUser", func(gitlabUserName string) (string, error) {
if user, ok := cfg.Users[gitlabUserName]; ok {
return user, nil
} else {
return "", fmt.Errorf("can't map user %s", gitlabUserName)
}
})
srv := application.NewHookService(telegramBot, telegramHookTemplater, sendTemplateErrors)
go runTelegramBot(api, mux)
return srv
}
func runTelegramBot(api *tgbotapi.BotAPI, mux *tm.Mux) {
log.Println("starting telegram bot ...")
updConfig := tgbotapi.NewUpdate(0)
updConfig.Timeout = 60
updChan := api.GetUpdatesChan(updConfig)
for upd := range updChan {
mux.Dispatch(api, upd)
}
}