-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
154 lines (122 loc) · 3.77 KB
/
main.go
File metadata and controls
154 lines (122 loc) · 3.77 KB
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
package main
import (
"errors"
"fmt"
"log"
"net/url"
"os"
"os/signal"
"path/filepath"
"strings"
"syscall"
"time"
TBot "gopkg.in/tucnak/telebot.v2"
)
func initialize(token string) *TBot.Bot {
log.Println("INFO - Initializing bot...")
bot, err := TBot.NewBot(TBot.Settings{
Token: token,
Poller: &TBot.LongPoller{Timeout: 10 * time.Second},
})
if err != nil {
log.Fatal(err)
}
log.Println("INFO - Initialize successful")
return bot
}
func sendVideo(bot *TBot.Bot, recipient *TBot.Chat, filename string) {
log.Printf("[Video] %-13s requested by %s", filename, recipient.Username)
file := &TBot.Video{File: TBot.FromDisk("./dabs/" + filename)}
_, _ = bot.Send(recipient, file)
}
func sendPicture(bot *TBot.Bot, recipient *TBot.Chat, filename string) {
log.Printf("[Picture] %-13s requested by %s", filename, recipient.Username)
file := &TBot.Photo{File: TBot.FromDisk("./dabs/" + filename)}
_, _ = bot.Send(recipient, file)
}
func loadFiles(bot *TBot.Bot) {
files, err := os.ReadDir("./dabs/")
if err != nil {
log.Fatal("ERROR - Could not read dab dir.")
}
for i, file := range files {
log.Printf("DEBUG - Loading dab %d: %s", i, file.Name())
fileExt := filepath.Ext(file.Name())
filename := strings.TrimSuffix(file.Name(), fileExt)
switch fileExt {
case ".mp4":
bot.Handle(fmt.Sprintf("/%s", filename), func(m *TBot.Message) {
sendVideo(bot, m.Chat, fmt.Sprintf("%s%s", filename, fileExt))
})
log.Printf("DEBUG - Video '/%s' loaded and registered", filename)
case ".jpg":
bot.Handle(fmt.Sprintf("/%s", filename), func(m *TBot.Message) {
sendPicture(bot, m.Chat, fmt.Sprintf("%s%s", filename, fileExt))
})
log.Printf("DEBUG - Picture '/%s' loaded and registered", filename)
default:
log.Printf("ERROR - file not loaded, file extension is not recognised but '%s'", fileExt)
}
}
}
func getToken() (string, error) {
tokenEnv := os.Getenv("TOKEN")
tokenFileEnv := os.Getenv("TOKEN_FILE")
// TOKEN set, use that
if tokenEnv != "" {
if tokenFileEnv != "" {
log.Print("WARNING - TOKEN and TOKEN_FILE env set, TOKEN will take precedence.")
}
return tokenEnv, nil
}
// TOKEN and TOKEN_FILE not set, no token -> crash
if tokenFileEnv == "" {
return "", errors.New("no TOKEN or TOKEN_FILE environment var set")
}
token, err := os.ReadFile(tokenFileEnv)
if err != nil {
return "", errors.New(fmt.Sprintf("TOKEN_FILE env - %v", err))
}
if string(token) == "" {
return "", errors.New("token read from TOKEN_FILE file is empty")
}
return string(token), nil
}
func main() {
token, err := getToken()
if err != nil {
log.Fatalf("ERROR - %v", err)
}
bot := initialize(token)
// Load and register files to handle
loadFiles(bot)
// Handle poster requests
bot.Handle("/poster", func(m *TBot.Message) {
args := strings.Split(m.Payload, ".")
if len(args) != 2 {
_, _ = bot.Send(m.Chat, fmt.Sprintf("Wrong number of arguments. Required: 2. Found: %v", len(args)))
return
}
log.Printf("[Poster] request received by %s with args \"%s\"", m.Sender.Username, args)
args[0] = url.PathEscape(strings.TrimSpace(args[0]))
args[1] = url.PathEscape(strings.TrimSpace(args[1]))
escapedUrl := fmt.Sprintf("https://punkt.felunka.de/generate.php?text=%s&text2=%s&color=c", url.QueryEscape(args[0]), url.QueryEscape(args[1]))
file := &TBot.Photo{File: TBot.FromURL(escapedUrl)}
_, err := bot.Send(m.Chat, file)
if err != nil {
log.Println("ERROR - Poster could not be sent. See error below.")
log.Println(err)
}
})
// Register listener for term signal and gracefully shut down
c := make(chan os.Signal, 2)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
log.Println("INFO - Term signal received. Shutting down...")
bot.Stop()
os.Exit(0)
}()
log.Print("INFO - Starting bot")
bot.Start()
}