-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
110 lines (99 loc) · 2.65 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
package main
import (
"fmt"
"github.com/Skycoin/git-telegram-bot/internal/config"
"github.com/Skycoin/git-telegram-bot/pkg/errutil"
"github.com/Skycoin/git-telegram-bot/pkg/githandler"
tb "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"log"
"time"
)
func main() {
cfg, err := config.NewBotConfig()
if err != nil {
log.Fatal(err)
}
bot, err := tb.NewBotAPI(cfg.TgBotToken)
if err != nil {
log.Fatal(errutil.ErrCreatingBot.Desc(err))
}
bot.Debug = true
updateConfig := tb.NewUpdate(0)
updateConfig.Timeout = 10
updates := bot.GetUpdatesChan(updateConfig)
chatId := cfg.TargetGroupChatId
stopCh := make(chan struct{})
var previousEventId string
var currentEventId string
ticker := time.NewTicker(61 * time.Second)
for update := range updates {
userIsAdmin := false
chatConfig := update.Message.Chat.ChatConfig()
admins, adminErr := bot.GetChatAdministrators(tb.ChatAdministratorsConfig{ChatConfig: chatConfig})
if adminErr != nil {
continue
}
for _, admin := range admins {
if update.Message.From.ID == admin.User.ID {
userIsAdmin = true
}
}
if !userIsAdmin {
continue
}
if update.Message.IsCommand() {
switch update.Message.Command() {
case "startpoll": // starts the poller
msg := tb.NewMessage(chatId, "starting Skycoin poll github events...")
if _, e := bot.Send(msg); err != nil {
fmt.Printf("error sending start message: %v", e)
continue
}
go func() {
for {
select {
case <-stopCh:
ticker.Stop()
break
case <-ticker.C:
previousEventId, err = githandler.HandleStartCommand(
previousEventId,
currentEventId,
cfg.TargetOrgUrl,
func(s string) error {
msg = tb.NewMessage(chatId, s)
if _, e := bot.Send(msg); err != nil {
return e
}
return nil
},
)
if err != nil {
fmt.Print(err)
continue
}
}
}
}()
case "stoppoll": // stops it
stopCh <- struct{}{}
msg := tb.NewMessage(chatId, "stopping bot, you can use /reset then /start command to start it again")
if _, err = bot.Send(msg); err != nil {
fmt.Printf("error sending message: %v", err)
}
case "helppoll": // displays help message
msg := tb.NewMessage(chatId, `
Hi, here's my list of commands:
/startpoll: starts polling events from github
/stoppoll: stops the poller
/resetpoll: resets the poller, use with /start after /stop to restart polling event.
`)
if _, err = bot.Send(msg); err != nil {
fmt.Printf("error sending message: %v", err)
}
case "resetpoll":
ticker = time.NewTicker(61 * time.Second)
}
}
}
}