-
Notifications
You must be signed in to change notification settings - Fork 31
/
telegram_bot.go
99 lines (85 loc) · 2.1 KB
/
telegram_bot.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
package telegram
import (
"context"
"fmt"
"strings"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
koolo "github.com/hectorgimenez/koolo/internal"
"github.com/hectorgimenez/koolo/internal/event/stat"
"go.uber.org/zap"
)
type Bot struct {
bot *tgbotapi.BotAPI
chatID int64
logger *zap.Logger
sueprvisor koolo.Supervisor
}
func NewBot(token string, chatID int64, logger *zap.Logger, supervisor koolo.Supervisor) (*Bot, error) {
bot, err := tgbotapi.NewBotAPI(token)
if err != nil {
return nil, err
}
return &Bot{
bot: bot,
chatID: chatID,
logger: logger,
sueprvisor: supervisor,
}, nil
}
func (b *Bot) Start(_ context.Context) error {
offset, err := b.getLatestOffset()
if err != nil {
return err
}
u := tgbotapi.NewUpdate(offset)
u.Timeout = 5
updates := b.bot.GetUpdatesChan(u)
for update := range updates {
if update.Message != nil && update.Message.Chat.ID == b.chatID { // If we got a message
switch strings.ToLower(update.Message.Text) {
case "stats":
if err := b.publishStats(); err != nil {
b.logger.Error("error sending telegram message", zap.Error(err))
}
case "start":
// TODO: Implement
case "stop":
b.sueprvisor.Stop()
}
}
}
return nil
}
func (b *Bot) getLatestOffset() (int, error) {
// Discard previous messages setting the offset to last msg + 1
upds, err := b.bot.GetUpdates(tgbotapi.NewUpdate(-1))
if err != nil {
return 0, err
}
offset := 0
if len(upds) > 0 {
offset = upds[0].UpdateID + 1
}
return offset, nil
}
func (b *Bot) publishStats() error {
msg := "Run | Items | Deaths | Chickens | Merc Chickens | Errors | Healing Pots | Mana Pots | Reju Pots | Merc Pots \n"
for run, st := range stat.Status.RunStats {
msg += fmt.Sprintf(
"%s | %d | %d | %d | %d | %d | %d | %d| %d | %d | %d \n",
run,
len(st.ItemsFound),
st.Kills,
st.Deaths,
st.Chickens,
st.MerChicken,
st.Errors,
st.HealingPotionsUsed,
st.ManaPotionsUsed,
st.RejuvPotionsUsed,
st.MercHealingPotionsUsed,
)
}
_, err := b.bot.Send(tgbotapi.NewMessage(b.chatID, msg))
return err
}