-
Notifications
You must be signed in to change notification settings - Fork 8
/
telegram_notif.go
50 lines (41 loc) · 1.21 KB
/
telegram_notif.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
package main
import (
"fmt"
"strconv"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
)
type telegramNotificator struct {
ChatId int64
Token string
MinAmount uint64
}
var _ notificator = (*telegramNotificator)(nil)
func NewTelegramNotificator(cfg notificatorConfig) *telegramNotificator {
chatId, _ := strconv.ParseInt(cfg.Params["ChatId"], 10, 64)
return &telegramNotificator{
ChatId: chatId,
Token: cfg.Params["Token"],
MinAmount: cfg.MinAmount,
}
}
func (t *telegramNotificator) Notify(amount uint64, comment string) (err error) {
if amount < t.MinAmount {
return fmt.Errorf("amount is too small, required %d got %d", t.MinAmount, amount)
}
if comment != "" {
comment = fmt.Sprintf("Sender said: \"%s\"", comment)
}
tgBot, err := tgbotapi.NewBotAPI(t.Token)
if err != nil {
log.Warnf("Couldn't create telegram bot api")
return err
}
body := fmt.Sprintf("Subject: %s\n\nYou've received %d sats to your lightning address. %s",
"New lightning address payment", amount, comment)
tgMessage := tgbotapi.NewMessage(t.ChatId, body)
_, err = tgBot.Send(tgMessage)
return err
}
func (t *telegramNotificator) Target() string {
return fmt.Sprintf("ChatId: %s", t.ChatId)
}