/
bot.go
63 lines (53 loc) · 1.28 KB
/
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
package telegram
import (
"context"
"github.com/go-telegram-bot-api/telegram-bot-api/v5"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"go.uber.org/zap"
)
var (
messageCounter = promauto.NewCounter(prometheus.CounterOpts{
Name: "twa_api_message_counter",
Help: "Number of telegram messages sent",
})
)
// UserID is an identifier of a telegram user.
type UserID int64
type bot struct {
logger *zap.Logger
bot *tgbotapi.BotAPI
}
func NewBot(logger *zap.Logger, botSecretKey string) (*bot, error) {
tgbot, err := tgbotapi.NewBotAPI(botSecretKey)
if err != nil {
return nil, err
}
return &bot{logger: logger, bot: tgbot}, nil
}
type Message struct {
UserID UserID
Text string
}
func (b *bot) Run(ctx context.Context) chan Message {
ch := make(chan Message)
go func() {
for {
select {
case <-ctx.Done():
return
case msg := <-ch:
go b.sendTextMessage(msg.UserID, msg.Text)
}
}
}()
return ch
}
func (b *bot) sendTextMessage(userID UserID, text string) {
messageCounter.Inc()
message := tgbotapi.NewMessage(int64(userID), text)
if _, err := b.bot.Send(message); err != nil {
// TODO: maybe we should retry sending a message?
b.logger.Error("failed to send message", zap.Error(err))
}
}