-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
120 lines (98 loc) · 2.51 KB
/
main.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package main
import (
"log"
"time"
"fmt"
"os"
"strconv"
"github.com/art-vasilyev/wotd"
tb "gopkg.in/tucnak/telebot.v2"
)
// If anythings fails, wait for this amount before retry.
const retryInterval = 5 * time.Minute
type channel struct {
id string
}
// Implements telebot.v2.Recipient interface.
func (c channel) Recipient() string {
return c.id
}
var (
// Hour after which message should be sent.
submitHour = convertToInt(os.Getenv("submitTime"))
// Telegram channel ID.
notificationChannel = channel{id: os.Getenv("channelID")}
// Telegram bot token.
botToken = os.Getenv("botToken")
)
// Convert string to integer
func convertToInt(string string) int {
hour, err := strconv.ParseInt(string, 10, 32)
if err != nil {
log.Fatal(err)
}
return int(hour)
}
// Notify channel subscribers about new word of the day.
func notifySubscribers(b *tb.Bot) (err error) {
log.Println("Notifying subscribers...")
msg, err := getMessageOfTheDay()
if err != nil {
return err
}
_, err = b.Send(notificationChannel, msg)
if err != nil {
return err
}
return err
}
// Get a new word of the day.
func getMessageOfTheDay() (string, error) {
word, err := wotd.GetWOTD()
return fmt.Sprintln(word), err
}
func findSecondsUntil(future time.Time) time.Duration {
return time.Duration(future.Sub(time.Now()).Seconds()) * time.Second
}
// Runs infinitely and notifies users on daily basis.
func runDailyNotification(bot *tb.Bot) {
for {
err := notifySubscribers(bot)
if err != nil {
log.Println(err)
log.Println("Will retry after", retryInterval)
time.Sleep(retryInterval)
continue // try again
}
now := time.Now()
nextTick := time.Date(
now.Year(), now.Month(), now.Day()+1, submitHour,
0, 0, 0, now.Location())
secondsTillNextTick := findSecondsUntil(nextTick)
log.Printf("Sleeping for %v.", secondsTillNextTick)
time.Sleep(secondsTillNextTick)
}
}
func main() {
b, err := tb.NewBot(tb.Settings{
Token: botToken,
Poller: &tb.LongPoller{Timeout: 10 * time.Second},
})
if err != nil {
log.Fatal(err)
}
log.Printf("%s (@%v) started doing it's job.", b.Me.FirstName, b.Me.Username)
log.Printf("Messages will be sent every day after %d:00 AM.", submitHour)
b.Handle("/ping", func(m *tb.Message) {
b.Send(m.Sender, "♥️")
})
b.Handle("/time", func(m *tb.Message) {
b.Send(m.Sender, time.Now().Format("Mon Jan 2 15:04:05"))
})
b.Handle("/start", func(m *tb.Message) {
b.Send(m.Sender, "Hello, "+m.Sender.FirstName+"!")
})
go runDailyNotification(b)
defer b.Stop()
b.Start()
}