-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotifier.go
82 lines (63 loc) · 1.38 KB
/
notifier.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
package pkg
import (
"errors"
"fmt"
"strings"
"time"
)
type EventType int
const (
Today EventType = iota
Tomorrow
)
type Event struct {
What, Where, Start, End string
Who []string
}
type Store interface {
Events(when EventType) ([]Event, error)
}
type Sender interface {
Send(message string) error
}
func TomorrowEvents(store Store) ([]Event, error) {
return store.Events(Tomorrow)
}
func TodayEvents(store Store) ([]Event, error) {
return store.Events(Today)
}
func Send(es []Event, sdr Sender, when EventType) error {
if len(es) == 0 {
return errors.New("pkg Send: events slice length should be more than zero")
}
return sdr.Send(generateMessage(es, when))
}
func generateMessage(es []Event, when EventType) string {
message := &strings.Builder{}
general := `
❗️ %s %s!
%s
`[1:]
tmpl := `
%s
📍 Место: %s
👥 Требуются: %s
▶️ Начало: %s
⏹ Окончание: %s
`[1:]
timeWord := time.Now()
whenWord := "Сегодня"
if when == Tomorrow {
whenWord = "Завтра"
timeWord = timeWord.AddDate(0, 0, 1)
}
what := "репетиция"
if len(es) > 1 {
what = "репетиции"
}
fmt.Fprintf(message, general, whenWord, what, timeWord.Format("02.01.2006"))
for _, e := range es {
fmt.Fprintf(message, tmpl, e.What, e.Where, strings.Join(e.Who, ", "), e.Start, e.End)
}
return message.String()
}