-
Notifications
You must be signed in to change notification settings - Fork 0
/
telegram.go
217 lines (209 loc) Β· 6.88 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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package main
import (
"errors"
"fmt"
"strconv"
"strings"
"time"
"github.com/dipsycat/calendar-telegram-go"
telegram "github.com/go-telegram-bot-api/telegram-bot-api"
)
func (w *woffu) runTelegramBot() error {
bot, err := telegram.NewBotAPI(w.BotToken)
if err != nil {
return err
}
w.Bot = bot
helpMsg := `Supported commands:
/dontCheckIn: Manully add a day in which I won't check in / out.
/skipList: Show the list of manually added days in which I won't check in / out.
/checkInNow: Check in inmediatly. This won't affect the scheduled check in / out operations.
/checkOutNow: Check out inmediatly. This won't affect the scheduled check in / out operations.
/help: Show this message.`
w.sendMessage("I'm online π. Here are my supported commands: \n" + helpMsg)
now := time.Now()
kbrdMonth := now.Month()
kbrdYear := now.Year()
go func() {
u := telegram.NewUpdate(0)
u.Timeout = 60
updates, err := bot.GetUpdatesChan(u)
if err != nil {
w.sendError(err)
}
for update := range updates {
fmt.Println("Message received: ", update)
if update.CallbackQuery != nil && update.CallbackQuery.Message.Chat.ID == w.ChatID {
// Handle keyboard response
splitted := strings.Split(update.CallbackQuery.Data, ".")
var keyboardCalendar telegram.InlineKeyboardMarkup
if update.CallbackQuery.Data == ">" {
// Next month
keyboardCalendar, kbrdYear, kbrdMonth = calendar.HandlerNextButton(kbrdYear, kbrdMonth)
msg := telegram.NewMessage(w.ChatID, "Choose day to add to skip list:")
msg.ReplyMarkup = keyboardCalendar
bot.Send(msg)
} else if update.CallbackQuery.Data == "<" {
// Previous month
keyboardCalendar, kbrdYear, kbrdMonth = calendar.HandlerPrevButton(kbrdYear, kbrdMonth)
msg := telegram.NewMessage(w.ChatID, "Choose day to add to skip list:")
msg.ReplyMarkup = keyboardCalendar
bot.Send(msg)
} else if len(splitted) == 3 {
// Add date to skip list
year, err := strconv.Atoi(splitted[0])
if err != nil {
w.sendError(errors.New("Unexpected message, not adding date"))
continue
}
month, err := strconv.Atoi(splitted[1])
if err != nil {
w.sendError(errors.New("Unexpected message, not adding date"))
continue
}
day, err := strconv.Atoi(splitted[2])
if err != nil {
w.sendError(errors.New("Unexpected message, not adding date"))
continue
}
if year < now.Year() ||
year == now.Year() && month < int(now.Month()) ||
year == now.Year() && month == int(now.Month()) && day < now.Day() {
w.sendMessage("β This date has already passed, choose a date in the future β ")
continue
}
// Check if not already added
found := false
for _, existingDate := range w.SkipList {
if existingDate == update.CallbackQuery.Data {
w.sendMessage("β " + update.CallbackQuery.Data + " is already added in the /skipList β ")
found = true
break
}
}
if !found {
// Add to skip list
w.SkipList = append(w.SkipList, update.CallbackQuery.Data)
// Message skip item added
w.sendMessage("Added π " + update.CallbackQuery.Data + " π to the skip list. You can edit the list with the command /skipList")
}
} else if len(splitted) == 4 && splitted[0] == "delete" {
deleteDate := splitted[1] + "." + splitted[2] + "." + splitted[3]
found := false
for i, storedDate := range w.SkipList {
if storedDate == deleteDate {
w.SkipList[i] = w.SkipList[len(w.SkipList)-1]
w.SkipList = w.SkipList[:len(w.SkipList)-1]
w.sendMessage("β " + deleteDate + " deleted from list β")
found = true
break
}
}
if !found {
w.sendMessage("β " + deleteDate + " not found. Check the updated /skipList β ")
}
continue
}
}
if update.Message == nil { // ignore any non-Message Updates
continue
}
if update.Message.Chat.ID != w.ChatID { // message from unexpected chat
w.sendMessage(
"I've received a message from an unexpected chat.\nChat id: `" +
strconv.Itoa(int(update.Message.Chat.ID)) +
"`\nMessage: `" + update.Message.Text + "`",
)
continue
}
// TODO: handle {add holyday, change check in/out}
if update.Message.IsCommand() {
switch update.Message.Command() {
case "help":
w.sendMessage(helpMsg)
case "dontCheckIn":
now = time.Now()
kbrdMonth = now.Month()
kbrdYear = now.Year()
keyboardCalendar := calendar.GenerateCalendar(kbrdYear, kbrdMonth)
msg := telegram.NewMessage(w.ChatID, "Choose day to add to skip list:")
msg.ReplyMarkup = keyboardCalendar
bot.Send(msg)
case "checkInNow":
if err := w.check(); err != nil {
w.sendError(err)
} else {
w.sendMessage("Checked in successfuly")
}
case "checkOutNow":
if err := w.check(); err != nil {
w.sendError(err)
} else {
w.sendMessage("Checked out successfuly")
}
case "skipList":
// Clean list (past days)
today := getCurrentDate()
tmpList := []string{}
for _, date := range w.SkipList {
fmt.Println(date)
if date >= today {
fmt.Println("yas")
tmpList = append(tmpList, date)
}
}
w.SkipList = tmpList
if len(w.SkipList) == 0 {
w.sendMessage("There are no dates in the skip list. You can add days in which I won't check in / out with the command /dontCheckIn")
} else {
keyboard := telegram.InlineKeyboardMarkup{}
// Create keyboard that allows day deletion
for _, date := range w.SkipList {
row := []telegram.InlineKeyboardButton{}
row = append(row, telegram.NewInlineKeyboardButtonData(date, "foo"))
row = append(row, telegram.NewInlineKeyboardButtonData("β", "delete."+date))
keyboard.InlineKeyboard = append(keyboard.InlineKeyboard, row)
}
msg := telegram.NewMessage(w.ChatID, "Days in which I won't check in / out:")
msg.ReplyMarkup = keyboard
if _, err := bot.Send(msg); err != nil {
w.sendError(err)
}
}
default:
w.sendMessage("I don't know that command. Do you need /help?")
}
}
}
w.sendMessage("I'm offline π")
}()
return nil
}
func (w *woffu) sendMessage(msg string) error {
if w.Bot == nil {
fmt.Println(msg)
return nil
}
fmt.Println("Sending message:", msg)
_, err := w.Bot.Send(telegram.NewMessage(w.ChatID, msg))
if err != nil {
fmt.Println(err)
}
return err
}
func (w *woffu) sendError(err error) error {
return w.sendMessage("Something went wrong π°π±:\n\nπ₯π₯π£ " + err.Error() + " π£π₯π₯")
}
func getCurrentDate() string {
currentTime := time.Now()
year := strconv.Itoa(currentTime.Year())
month := strconv.Itoa(int(currentTime.Month()))
day := strconv.Itoa(currentTime.Day())
if len(month) == 1 {
month = "0" + month
}
if len(day) == 1 {
day = "0" + day
}
return year + "." + month + "." + day
}