/
telegram.go
79 lines (64 loc) · 1.48 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package telegram
import (
"bytes"
tg "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"github.com/revelaction/ical-git/config"
"github.com/revelaction/ical-git/notify"
"text/template"
"time"
)
type Notifier interface {
Notify(message string, level string) error
}
type message struct {
msg string
timeStamp time.Time
}
// Telegram
type Telegram struct {
bot *tg.BotAPI
conf config.Config
}
func New(conf config.Config) *Telegram {
bot, err := tg.NewBotAPI(conf.Telegram.Token)
if err != nil {
// TODO
return nil
}
return &Telegram{
bot: bot,
conf: conf,
}
}
func (t *Telegram) Notify(n notify.Notification) error {
message, err := renderNotification(n)
if err != nil {
return err
}
msg := tg.NewMessage(t.conf.Telegram.ChatId, message)
msg.ParseMode = "markdown"
_, err = t.bot.Send(msg)
if err != nil {
return err
}
return nil
}
func renderNotification(n notify.Notification) (string, error) {
const tpl = `
**{{.Summary}}**
📅 **{{.EventTime.Format "Monday, 2006-01-02"}} {{.EventTime.Format "🕒 15:04"}}** 🌍 {{.TimeZone}}
📌 Location: **{{.Location}}**
📝 Description: {{.Description}}
🚦 Status: **{{.Status}}**
`
// Confirmed: ✅, Postponed: 🔄Cancelled: ❌Pending: ⌛Tentative: 🤔Not Attending: 🚫
t, err := template.New("notification").Parse(tpl)
if err != nil {
return "", err
}
var buf bytes.Buffer
if err := t.Execute(&buf, n); err != nil {
return "", err
}
return buf.String(), nil
}