-
Notifications
You must be signed in to change notification settings - Fork 0
/
telegram.go
57 lines (48 loc) · 1.3 KB
/
telegram.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
package main
import (
"fmt"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
type Telegram interface {
Init() error
NotifyEvent(event CalendarEvent) error
}
func NewTelegram(cfg Config) Telegram {
return &telegram{
cfg: cfg,
}
}
type telegram struct {
cfg Config
bot *tgbotapi.BotAPI
}
func (t *telegram) Init() error {
bot, err := tgbotapi.NewBotAPI(t.cfg.TelegramToken)
if err != nil {
return err
}
t.bot = bot
return nil
}
func (t *telegram) NotifyEvent(event CalendarEvent) error {
msgBody, err := prepareMessageBody(event)
if err != nil {
return err
}
msg := tgbotapi.NewMessage(t.cfg.TelegramChatId, msgBody)
msg.ParseMode = "markdown"
_, err = t.bot.Send(msg)
return err
}
func prepareMessageBody(event CalendarEvent) (string, error) {
switch event.Status {
case StatusCreated:
return fmt.Sprintf("🗓️ *%s*\n\n*התחלה:* %s\n*סיום:* %s", event.Title, event.Start, event.End), nil
case StatusUpdated:
return fmt.Sprintf("️✍🏻 *עדכון: %s*\n\n*התחלה:* %s\n*סיום:* %s", event.Title, event.Start, event.End), nil
case StatusCanceled:
return fmt.Sprintf("️🆇 *בוטל: %s*\n\n*התחלה:* %s\n*סיום:* %s", event.Title, event.Start, event.End), nil
default:
return "", fmt.Errorf("unexpected status: %d", event.Status)
}
}