/
subscribe.go
107 lines (99 loc) · 3.32 KB
/
subscribe.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
package handler
import (
"fmt"
objs "github.com/SakoDroid/telego/objects"
log "github.com/sirupsen/logrus"
)
func (h *Handler) notifySubscribers() {
h.u.Notify(
// notifySoon
func(id int, prayer, time string) {
if err := h.setScript(id); err != nil {
log.WithFields(log.Fields{"error": err}).Warn("failed to send notify message, user language not set on prayer soon /notify")
return
}
message := fmt.Sprintf(h.userScript[id].PrayerSoon, h.userScript[id].GetPrayerByName(prayer), time)
r, err := h.b.SendMessage(id, message, "HTML", 0, false, false)
if err != nil {
log.WithFields(log.Fields{"error": err}).Warn("failed to send message on notifySoon")
return
}
go h.replace(id, r.Result.MessageId)
},
// notifyNow
func(id int, prayer string) {
if err := h.setScript(id); err != nil {
log.WithFields(log.Fields{"error": err}).Warn("failed to send notify message, user language not set on prayer arrived /notify")
return
}
message := fmt.Sprintf(h.userScript[id].PrayerArrived, h.userScript[id].GetPrayerByName(prayer))
r, err := h.b.SendMessage(id, message, "HTML", 0, false, false)
if err != nil {
log.WithFields(log.Fields{"error": err}).Warn("failed to send message on notifyNow, Error")
return
}
go h.replace(id, r.Result.MessageId)
},
// notifyGomaa
func(id int, time string) {
if err := h.setScript(id); err != nil {
log.WithFields(log.Fields{"error": err}).Warn("failed to send notify message, user language not set on gomaa /notify")
return
}
message := fmt.Sprintf(h.userScript[id].GomaaDay, time)
r, err := h.b.SendMessage(id, message, "HTML", 0, false, false)
if err != nil {
log.WithFields(log.Fields{"error": err}).Warn("failed to send message on notifyGomaa")
return
}
go h.replace(id, r.Result.MessageId)
},
)
}
func (h *Handler) Subscribe(u *objs.Update) {
chatID := u.Message.Chat.Id
err := h.u.Subscribe(h.c, chatID)
if err != nil {
h.simpleSend(chatID, h.userScript[chatID].SubscriptionError, 0)
return
}
_, err = h.b.SendMessage(chatID, h.userScript[chatID].SubscriptionSuccess, "HTML", 0, false, false)
if err != nil {
log.WithFields(log.Fields{"error": err}).Warn("failed to send subscribe message")
return
}
}
func (h *Handler) Unsubscribe(u *objs.Update) {
chatID := u.Message.Chat.Id
err := h.u.Unsubscribe(h.c, chatID)
if err != nil {
h.simpleSend(chatID, h.userScript[chatID].UnsubscriptionError, 0)
return
}
_, err = h.b.SendMessage(chatID, h.userScript[chatID].UnsubscriptionSuccess, "HTML", 0, false, false)
if err != nil {
log.WithFields(log.Fields{"error": err}).Warn("failed to send unsubscribe message")
return
}
}
// replace is a helper function to delete the last message id and store the new one's id in the database.
func (h *Handler) replace(id int, message int) {
// Delete the last message id.
if err := h.c.Err(); err != nil {
return
}
lastMessageId, err := h.u.GetPrayerMessageID(h.c, id)
if err != nil {
log.WithFields(log.Fields{"error": err}).Warn("failed to remove last message id /notify")
} else {
h.deleteMessage(id, lastMessageId)
}
// Store the new message id.
if err = h.c.Err(); err != nil {
return
}
err = h.u.StorePrayerMessageID(h.c, id, message)
if err != nil {
log.WithFields(log.Fields{"error": err}).Warn("failed to replace message id /notify")
}
}