Skip to content

Commit 05caa3f

Browse files
author
escalopa
committed
refactor: clean duplicated code + add message forwarding on support commands
1 parent eb41674 commit 05caa3f

File tree

8 files changed

+128
-172
lines changed

8 files changed

+128
-172
lines changed

script/innopolis.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ func (n *NodeIterator) Next() *html.Node {
3232
n.current = n.current.NextSibling
3333

3434
return node
35-
3635
}
3736

3837
const (

telegram/internal/handler/admin.go

Lines changed: 17 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package handler
22

33
import (
4-
"context"
54
"strconv"
65

76
objs "github.com/SakoDroid/telego/objects"
@@ -11,11 +10,11 @@ import (
1110
// Respond to a user's feedback or bug report
1211
func (h *Handler) Respond(u *objs.Update) {
1312
var (
14-
chatID = getChatID(u)
15-
chatIDStr = strconv.Itoa(chatID)
13+
chatID = getChatID(u)
14+
ctx = h.getChatCtx(chatID)
1615
)
1716

18-
ch, closer, err := h.registerChannel(chatIDStr)
17+
ch, closer, err := h.registerChannel(chatID)
1918
if err != nil {
2019
log.Errorf("Handler.Respond: register channel [%d] => %v", chatID, err)
2120
return
@@ -28,8 +27,8 @@ func (h *Handler) Respond(u *objs.Update) {
2827
return
2928
}
3029

31-
// Read userChatID, messageID, username from the old message that will be replied to
32-
userChatID, responseMessageID, _, ok := parseUserMessage(u.Message.ReplyToMessage.Text)
30+
// Read userChatID, messageID, from the old message that will be replied to
31+
userChatID, userMessageID, ok := parseUserMessage(u.Message.ReplyToMessage.Text)
3332
if !ok {
3433
h.simpleSend(chatID, respondInvalidMsg)
3534
return
@@ -39,21 +38,16 @@ func (h *Handler) Respond(u *objs.Update) {
3938
messageID := h.simpleSend(chatID, respondStart)
4039
defer h.deleteMessage(chatID, messageID)
4140

42-
// Create new command context
43-
ctx, cancel := context.WithTimeout(h.getChatCtx(chatID), inputTimeout)
44-
defer cancel()
45-
46-
select {
47-
case <-ctx.Done():
41+
u = h.readInput(ctx, ch)
42+
if u == nil {
4843
return
49-
case u = <-ch:
5044
}
5145

5246
if h.isCancelOperation(chatID, u.Message.Text) {
5347
return
5448
}
5549

56-
h.reply(userChatID, u.Message.Text, responseMessageID)
50+
h.reply(userChatID, u.Message.Text, userMessageID)
5751
h.simpleSend(chatID, respondSuccess)
5852
}
5953

@@ -74,11 +68,11 @@ func (h *Handler) GetSubscribers(u *objs.Update) {
7468
// SendAll broadcasts a message to all subscribers
7569
func (h *Handler) SendAll(u *objs.Update) {
7670
var (
77-
chatID = getChatID(u)
78-
chatIDStr = strconv.Itoa(chatID)
71+
chatID = getChatID(u)
72+
ctx = h.getChatCtx(chatID)
7973
)
8074

81-
ch, closer, err := h.registerChannel(chatIDStr)
75+
ch, closer, err := h.registerChannel(chatID)
8276
if err != nil {
8377
log.Errorf("Handler.SendAll: register channel [%d] => %v", chatID, err)
8478
return
@@ -89,13 +83,9 @@ func (h *Handler) SendAll(u *objs.Update) {
8983
messageID := h.simpleSend(chatID, sendAllStart)
9084
defer h.deleteMessage(chatID, messageID)
9185

92-
ctx1, cancel1 := context.WithTimeout(h.getChatCtx(chatID), inputTimeout)
93-
defer cancel1()
94-
95-
select {
96-
case <-ctx1.Done():
86+
u = h.readInput(ctx, ch)
87+
if u == nil {
9788
return
98-
case u = <-ch:
9989
}
10090

10191
var (
@@ -111,19 +101,12 @@ func (h *Handler) SendAll(u *objs.Update) {
111101
messageID = h.simpleSend(chatID, sendAllConfirm)
112102
defer h.deleteMessage(chatID, messageID)
113103

114-
// Wait for confirmation message or timeout after 5 minutes
115-
ctx2, cancel2 := context.WithTimeout(h.getChatCtx(chatID), inputTimeout)
116-
defer cancel2()
117-
118-
select {
119-
case <-ctx2.Done():
104+
u = h.readInput(ctx, ch)
105+
if u == nil {
120106
return
121-
case u = <-ch:
122107
}
123108

124-
// Delete the bot message if the user sends the message or if the context times out
125109
defer h.deleteMessage(chatID, u.Message.MessageId)
126-
127110
if h.isCancelOperation(chatID, u.Message.Text) {
128111
return
129112
}
@@ -138,11 +121,11 @@ func (h *Handler) SendAll(u *objs.Update) {
138121
// Send message to all subscribers asynchronously
139122
go func() {
140123
for _, userChatID := range chatIDs {
141-
if userChatID == chatID {
124+
if userChatID == chatID { // Skip the owner
142125
continue
143126
}
144127
h.simpleSend(userChatID, broadcastMessageText)
145128
}
146-
h.reply(chatID, sendAllSuccess, broadcastMessageID)
129+
h.reply(chatID, sendAllSuccess, broadcastMessageID) // Notify the owner
147130
}()
148131
}

telegram/internal/handler/handler.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"context"
55
"sync"
66

7+
objs "github.com/SakoDroid/telego/objects"
8+
79
"github.com/SakoDroid/telego"
810
app "github.com/escalopa/gopray/telegram/internal/application"
911
"github.com/escalopa/gopray/telegram/internal/domain"
@@ -100,6 +102,19 @@ func (h *Handler) processUnknown() {
100102
}
101103
}
102104

105+
// readInput reads the next input from the input channel or returns nil if the context is done.
106+
func (h *Handler) readInput(ctx context.Context, inputChan <-chan *objs.Update) *objs.Update {
107+
ctx, cancel := context.WithTimeout(ctx, inputTimeout)
108+
defer cancel()
109+
110+
select {
111+
case <-ctx.Done():
112+
return nil
113+
case u := <-inputChan:
114+
return u
115+
}
116+
}
117+
103118
func (h *Handler) Stop() {
104119
h.bot.Stop()
105120
h.uc.Close()

telegram/internal/handler/helper.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package handler
22

33
import (
44
"context"
5+
"strconv"
56
"time"
67

78
objs "github.com/SakoDroid/telego/objects"
@@ -61,13 +62,14 @@ func (h *Handler) replace(chatID int, messageID int) {
6162
}
6263
}
6364

64-
func (h *Handler) registerChannel(chatID string) (chan *objs.Update, func(), error) {
65-
ch, err := h.bot.AdvancedMode().RegisterChannel(chatID, "message")
65+
func (h *Handler) registerChannel(chatID int) (chan *objs.Update, func(), error) {
66+
chatIDStr := strconv.Itoa(chatID)
67+
ch, err := h.bot.AdvancedMode().RegisterChannel(chatIDStr, "message")
6668
closer := func() {
6769
if ch == nil {
6870
return
6971
}
70-
h.bot.AdvancedMode().UnRegisterChannel(chatID, "message")
72+
h.bot.AdvancedMode().UnRegisterChannel(chatIDStr, "message")
7173
}
7274
return *ch, closer, err
7375
}

telegram/internal/handler/prayer.go

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,20 @@ package handler
33
import (
44
"context"
55
"fmt"
6+
"strings"
67
"time"
78

9+
objs "github.com/SakoDroid/telego/objects"
810
log "github.com/catalystgo/logger/cli"
9-
1011
"github.com/escalopa/gopray/telegram/internal/domain"
11-
12-
objs "github.com/SakoDroid/telego/objects"
1312
"github.com/olekukonko/tablewriter"
1413
)
1514

15+
const (
16+
// PrayerTimeFormat is the format of the prayer times
17+
prayerTimeFormat = "15:04"
18+
)
19+
1620
func (h *Handler) GetPrayers(u *objs.Update) {
1721
chatID := getChatID(u)
1822

@@ -92,18 +96,6 @@ func (h *Handler) GetPrayersByDate(u *objs.Update) {
9296
messageID = r.Result.MessageId
9397
}
9498

95-
const (
96-
// PrayerTimeFormat is the format of the prayer times
97-
prayerTimeFormat = "15:04"
98-
)
99-
100-
type prayerTable string
101-
102-
func (t *prayerTable) Write(p []byte) (n int, err error) {
103-
*t += prayerTable(p)
104-
return len(p), nil
105-
}
106-
10799
// prayrify returns a string representation of the prayer times in a Markdown prayerTable format.
108100
// Example output:
109101
// Day 9 November 🕌
@@ -118,8 +110,8 @@ func (h *Handler) prayrify(chatID int, p *domain.PrayerTime) string {
118110
script := h.getChatScript(chatID)
119111

120112
// Create a Markdown prayerTable with the prayer times
121-
t := new(prayerTable)
122-
tw := tablewriter.NewWriter(t)
113+
writer := &strings.Builder{}
114+
tw := tablewriter.NewWriter(writer)
123115

124116
data := [][]string{
125117
{script.Fajr, p.Fajr.Format(prayerTimeFormat)},
@@ -139,10 +131,9 @@ func (h *Handler) prayrify(chatID int, p *domain.PrayerTime) string {
139131
tw.Render()
140132

141133
formattedTable := fmt.Sprintf(prayerText,
142-
script.PrayrifyTableDay,
143134
p.Day.Day(),
144135
script.GetMonthNames()[p.Day.Month()-1],
145-
string(*t),
136+
writer.String(),
146137
)
147138

148139
return formattedTable

0 commit comments

Comments
 (0)