-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
291 lines (260 loc) · 8.29 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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package main
import (
"fmt"
"log"
"os"
"os/signal"
"regexp"
"strconv"
"strings"
"github.com/docopt/docopt-go"
"github.com/go-telegram-bot-api/telegram-bot-api"
"github.com/noam09/go-sickchill-api"
)
func main() {
// Set up signal catching
sigs := make(chan os.Signal, 1)
// Catch all signals
signal.Notify(sigs)
// signal.Notify(sigs,syscall.SIGQUIT)
// Method invoked on signal receive
go func() {
s := <-sigs
log.Printf("Received signal: %s", s)
AppCleanup()
os.Exit(1)
}()
usage := `ChillBot
Usage:
chillbot --token=<bot> --key=<apikey> -w <chatid>... [--host=<host>] [--port=<port>] [--base=<base>] [--ssl] [-d]
chillbot -h | --help
Options:
-h, --help Show this screen.
-t, --token=<bot> Telegram bot token.
-k, --key=<apikey> API key.
-w, --whitelist=<chatid> Telegram chat ID(s) allowed to communicate with the bot (contact @myidbot).
-o, --host=<host> Hostname or address SickChill runs on [default: 127.0.0.1].
-r, --port=<port> Port SickChill runs on [default: 8081].
-b, --base=<urlbase> Path which should follow the base URL.
-s, --ssl Use TLS/SSL (HTTPS) [default: false].
-d, --debug Debug logging [default: false].`
opts, _ := docopt.ParseDoc(usage)
log.Println(opts)
// Get whitelist
var whitelist []int
w := opts["--whitelist"].([]string)
for _, v := range w {
i, err := strconv.Atoi(v)
if err != nil {
continue
}
whitelist = append(whitelist, i)
}
// Get arguments
token, _ := opts.String("--token")
hostname, _ := opts.String("--host")
port, _ := opts.Int("--port")
apiKey, _ := opts.String("--key")
urlBase, _ := opts.String("--base")
ssl, _ := opts.Bool("--ssl")
debug, _ := opts.Bool("--debug")
// Initialize SC client
sr := sickchill.NewClient(hostname, port, apiKey, urlBase, ssl)
BOT_TOKEN := token
bot, err := tgbotapi.NewBotAPI(BOT_TOKEN)
if err != nil {
log.Panic(err)
}
bot.Debug = debug
log.Printf("Authorized on account %s", bot.Self.UserName)
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
updates, err := bot.GetUpdatesChan(u)
tvdb, _ := regexp.Compile("tvdb ([0-9]{4,10})")
tvdbOnly, _ := regexp.Compile("^#([0-9]{4,10})$")
// emojiStar := "\u2b50\ufe0f"
emojiFilm := "\U0001f4fa"
// emojiSearch := "\U0001f50d"
emojiCancel := "\u274e"
for update := range updates {
if update.Message == nil {
continue
}
// Check if user ID in whitelist
if !intInSlice(update.Message.From.ID, whitelist) {
// log.Println("not me")
continue
}
log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "")
if tvdbOnly.MatchString(strings.ToLower(update.Message.Text)) {
i := 0
_, err := bot.Send(tgbotapi.NewChatAction(update.Message.Chat.ID, tgbotapi.ChatTyping))
if err != nil {
log.Println(err)
}
log.Println("Found #TVDBID")
tvdbId := tvdbOnly.FindStringSubmatch(strings.ToLower(update.Message.Text))
if len(tvdbId) == 2 {
i, err = strconv.Atoi(tvdbId[1])
} else {
continue
}
// i, err := strconv.Atoi(tvdbId)
// fmt.Println("i: ", i)
if err != nil {
fmt.Println(err)
continue
}
val, err := sr.AddNewShow(i, "")
if err != nil {
log.Println(val)
}
msg.Text = fmt.Sprintf("Adding _#%d_ to snatchlist", i)
msg.ParseMode = "markdown"
} else if tvdb.MatchString(strings.ToLower(update.Message.Text)) {
_, err := bot.Send(tgbotapi.NewChatAction(update.Message.Chat.ID, tgbotapi.ChatTyping))
if err != nil {
log.Println(err)
}
log.Println("Found TVDBID")
tvdbId := tvdb.FindString(strings.ToLower(update.Message.Text))
title, _ := regexp.Compile(`(.*?) \[.*?\]`)
movieTitle := title.FindStringSubmatch(update.Message.Text)
if len(movieTitle) > 1 {
t := movieTitle[1]
log.Println(t)
words := strings.Fields(tvdbId)
if len(words) == 2 {
tvdbId = words[1]
}
i, err := strconv.Atoi(tvdbId)
fmt.Println("i: ", i)
if err != nil {
fmt.Println(err)
continue
// os.Exit(2)
}
val, err := sr.AddNewShow(i, "")
if err != nil {
log.Println(val)
}
msg.Text = fmt.Sprintf("Adding _%s_ (%s) to snatchlist", t, tvdbId)
msg.ParseMode = "markdown"
} else {
msg.Text = "Failed adding show to snatchlist"
}
// Remove the custom keyboard if it's still there
msg.ReplyMarkup = tgbotapi.ReplyKeyboardRemove{
RemoveKeyboard: true,
Selective: false,
}
} else if strings.HasPrefix(update.Message.Text, "/q ") || !strings.HasPrefix(update.Message.Text, "/") {
_, err := bot.Send(tgbotapi.NewChatAction(update.Message.Chat.ID, tgbotapi.ChatTyping))
if err != nil {
log.Println(err)
}
split := strings.Split(update.Message.Text, "/q ")
q := ""
if len(split) == 2 {
_, q = split[0], split[1]
} else {
q = update.Message.Text
}
res, err := sr.SearchTVDB(strings.TrimSpace(q))
resultCount := 0
if res.Result == "success" {
fmt.Println("Success")
msgBody := ""
// Make 2D slice with enough rows for the number of search results
// Add one more row for the "/cancel" button
rows := make([][]tgbotapi.KeyboardButton, len(res.Data.Results)+1)
// Loop through results
for _, a := range res.Data.Results {
// Create button text for each search result
button := fmt.Sprintf("%s [Aired: %s] [TVDB %d]", a.Name, a.FirstAired, a.TVDBID)
// In each row, append one column containing a KeyboardButton with the button text
rows[resultCount] = append(rows[resultCount], tgbotapi.NewKeyboardButton(button))
log.Printf("%s [Aired: %s] [TVDBID %d]\n", a.Name, a.FirstAired, a.TVDBID)
// Tally the number of results
resultCount += 1
msgBody += fmt.Sprintf("*%d)* [%s](https://www.tvtime.com/en/show/%d) _(Aired: %s)_ (TVDBID %d)\n",
resultCount, a.Name, a.TVDBID, a.FirstAired, a.TVDBID)
}
// If there is at least one result ready, create the custom keyboard
if resultCount > 0 {
button := fmt.Sprintf("/cancel")
rows[resultCount] = append(rows[resultCount], tgbotapi.NewKeyboardButton(button))
// Init keyboard variable
var kb tgbotapi.ReplyKeyboardMarkup
kb = tgbotapi.ReplyKeyboardMarkup{
ResizeKeyboard: true,
Keyboard: rows,
OneTimeKeyboard: true,
}
// kb.OneTimeKeyboard = true
// Append the custom keyboard to the reply message
msg.ReplyMarkup = kb
// Append a hint to the results
msgBody += "\n\nIf you were expecting more results, try running the same search again"
// Append the list of results to the reply message
msg.Text = msgBody
msg.ParseMode = "markdown"
// Avoid the first TVDB link being resolved for preview
msg.DisableWebPagePreview = true
} else {
msgBody += "Looks like no results were found. "
msgBody += "You might try running the same search again, "
msgBody += "sometimes they get lost on the way back!"
msg.Text = msgBody
}
} else {
log.Println(err)
msgBody := "Looks like no results were found."
msg.Text = msgBody
}
} else {
_, err := bot.Send(tgbotapi.NewChatAction(update.Message.Chat.ID, tgbotapi.ChatTyping))
if err != nil {
log.Println(err)
}
switch strings.TrimSpace(strings.ToLower(update.Message.Text)) {
case "/start":
msg.Text = "Hi! Use /q to start searching"
case "/help", "/h":
msg.Text = fmt.Sprintf("%s /q - TV show search\nOr simply send a query without any commands", emojiFilm)
msg.Text += fmt.Sprintf("\nSend a hashtag and TVDB ID if you know exactly what you want (`#12345`).")
msg.Text += fmt.Sprintf("\n\n%s /c - Cancel current operation", emojiCancel)
msg.ParseMode = "markdown"
case "/q":
msg.Text = fmt.Sprintf("/q should be followed by a search query. Example:\n`/q Who Is America`\n")
msg.ParseMode = "markdown"
case "/cancel", "/c":
// Cancel
msg.Text = "Cancelling"
msg.ReplyMarkup = tgbotapi.ReplyKeyboardRemove{
RemoveKeyboard: true,
Selective: false,
}
default:
msg.Text = ""
msg.ParseMode = "markdown"
}
}
if msg.Text != "" {
bot.Send(msg)
}
continue
} // end updates
} // end main
func intInSlice(a int, list []int) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}
func AppCleanup() {
log.Println("Exiting...")
}