-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
367 lines (304 loc) · 8.48 KB
/
Copy pathmain.go
File metadata and controls
367 lines (304 loc) · 8.48 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
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
package main
import (
"context"
"log"
"os"
"os/signal"
"strconv"
"strings"
"sync"
"github.com/pocketbase/pocketbase"
"github.com/pocketbase/pocketbase/apis"
"github.com/pocketbase/pocketbase/core"
"github.com/pocketbase/pocketbase/plugins/jsvm"
"github.com/pocketbase/pocketbase/plugins/migratecmd"
"github.com/go-telegram/bot"
"github.com/go-telegram/bot/models"
escpos "github.com/mect/go-escpos"
)
var printer_lock sync.Mutex
var printer *escpos.Printer = nil
var tg_bot *bot.Bot = nil
var tg_chat_id int64 = 0
func insertAuditlog(
app core.App,
auth *core.Record,
record *core.Record,
event string,
) error {
if record.Collection().Name == "auditlog" {
return nil // ignore audi tlog record changes
}
auditlogCollection, err := app.FindCollectionByNameOrId("auditlog")
if err != nil {
return err
}
var adminId string
var authRecordId string
// get the authenticated admin
if auth.IsSuperuser() {
adminId = auth.Id
} else {
authRecordId = auth.Id
}
auditlog := core.NewRecord(auditlogCollection)
auditlog.Set("collection", record.Collection().Name)
auditlog.Set("record", record.Id)
auditlog.Set("event", event)
auditlog.Set("user", authRecordId)
auditlog.Set("admin", adminId)
auditlog.Set("data", record)
auditlog.Set("printed", false)
auditlog.Set("telegram", false)
err = app.Save(auditlog)
if err != nil {
return err
}
if record.Collection().Name == "week_passes" || record.Collection().Name == "day_passes" {
if tg_bot != nil {
// send a message to the telegram bot
if record.Collection().Name == "week_passes" {
staff := ""
if record.Get("staff") != nil && record.Get("staff").(bool) {
staff = "(Staff)"
}
_, err := tg_bot.SendMessage(context.Background(), &bot.SendMessageParams{
ChatID: tg_chat_id,
Text: "WeekPass " + event + " for Badge: " + record.Get("uid").(string) + staff,
})
if err != nil {
goto telegram_err
}
} else {
dayId := record.Get("day")
dayCollection, err := app.FindCollectionByNameOrId("days")
if err != nil {
goto telegram_err
}
dayRecord, err := app.FindRecordById(dayCollection, dayId.(string))
if err != nil {
goto telegram_err
}
_, err = tg_bot.SendMessage(context.Background(), &bot.SendMessageParams{
ChatID: tg_chat_id,
Text: "DayPass " + event + " for Badge: " + record.Get("uid").(string) + "\nDay: " + dayRecord.Get("name").(string),
})
if err != nil {
goto telegram_err
}
}
telegram_err:
if err != nil {
auditlog.Set("telegram", false)
log.Println("Error sending message to telegram bot:")
log.Println(err)
} else {
auditlog.Set("telegram", true)
log.Println("Message sent to telegram bot!")
}
}
printer_lock.Lock()
defer printer_lock.Unlock()
if printer == nil {
pr, err := escpos.NewUSBPrinterByPath("") // empty string will do a self discovery
if err != nil {
log.Println("Error opening printer:")
log.Println(err)
printer = nil
} else {
printer = pr
err = printer.Init()
if err != nil {
log.Println("Error initializing printer:")
log.Println(err)
printer = nil
}
}
}
if printer != nil {
log.Println("Printing pass...")
// if event == "delete" {
// printer.Size(1, 1).Write("Deleted Pass for Badge: ")
// printer.Bold(true).Size(1, 1).Write(record.Get("uid").(string) + "\n")
// printer.Bold(false).Size(1, 1).Write(" Type: " + record.Collection().Name + "\n")
// }
//
err = printer.Smooth(true)
if err != nil {
goto print_err
}
err = printer.Size(1, 1)
if err != nil {
goto print_err
}
err = printer.PrintLn(event)
if err != nil {
goto print_err
}
if record.Collection().Name == "week_passes" {
err = printer.Print("Week Pass for Badge: ")
if err != nil {
goto print_err
}
err = printer.Bold(true) // bold on
if err != nil {
goto print_err
}
staff := ""
if record.Get("staff") != nil && record.Get("staff").(bool) {
staff = "(Staff)"
}
err = printer.PrintLn(record.Get("uid").(string) + staff)
if err != nil {
goto print_err
}
err = printer.Bold(false) // bold off
if err != nil {
goto print_err
}
} else {
dayId := record.Get("day")
dayCollection, err := app.FindCollectionByNameOrId("days")
if err != nil {
goto print_err
}
dayRecord, err := app.FindRecordById(dayCollection, dayId.(string))
if err != nil {
goto print_err
}
err = printer.Print("Day Pass for Badge: ")
if err != nil {
goto print_err
}
err = printer.Bold(true)
if err != nil {
goto print_err
}
err = printer.PrintLn(record.Get("uid").(string))
if err != nil {
goto print_err
}
err = printer.Bold(false)
if err != nil {
goto print_err
}
err = printer.PrintLn(" Day: " + dayRecord.Get("name").(string))
if err != nil {
goto print_err
}
}
err = printer.Bold(false)
if err != nil {
goto print_err
}
err = printer.PrintLn(" ID: " + record.Id)
if err != nil {
goto print_err
}
err = printer.Feed(1)
if err != nil {
goto print_err
}
err = printer.End()
if err != nil {
goto print_err
}
print_err:
if err != nil {
log.Println("Error printing pass:")
log.Println(err)
log.Println("Marking pass as not printed!")
auditlog.Set("printed", false)
printer = nil
} else {
log.Println("Pass printed!")
auditlog.Set("printed", true)
}
}
}
app.Save(auditlog)
return nil
}
func main() {
log.Println("Starting CoatCheck...")
log.Println("")
var bot_token = os.Getenv("TG_BOT_TOKEN")
if bot_token != "" {
log.Println("Starting Telegram bot...")
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel()
chat_id_raw := os.Getenv("TG_CHAT_ID")
chat_id, err := strconv.ParseInt(chat_id_raw, 10, 64)
if err != nil {
log.Println("Error parsing chat id:")
log.Println(err)
} else {
tg_chat_id = chat_id
}
opts := []bot.Option{
bot.WithDefaultHandler(handler),
}
bot_i, err := bot.New(bot_token, opts...)
if err != nil {
log.Println("Error starting telegram bot:")
log.Println(err)
} else {
log.Println("Telegram bot started!")
tg_bot = bot_i
}
go bot_i.Start(ctx)
}
app := pocketbase.New()
log.Println("Starting PocketBase...")
isGoRun := strings.HasPrefix(os.Args[0], os.TempDir()) // loosly check if running in go run
log.Println("Registering plugins...")
jsvm.MustRegister(app, jsvm.Config{
MigrationsDir: "./pb_migrations",
HooksDir: "./pb_hooks",
HooksWatch: isGoRun,
HooksPoolSize: 25,
})
migratecmd.MustRegister(app, app.RootCmd, migratecmd.Config{
// enable auto creation of migration files when making collection changes in the Admin UI
// (the isGoRun check is to enable it only during development)
TemplateLang: migratecmd.TemplateLangJS,
Automigrate: isGoRun,
Dir: "pb_migrations",
})
app.OnRecordCreateRequest().BindFunc(func(e *core.RecordRequestEvent) error {
err := e.Next()
if err != nil {
return err
}
return insertAuditlog(app, e.Auth, e.Record, "create")
})
app.OnRecordUpdateRequest().BindFunc(func(e *core.RecordRequestEvent) error {
err := e.Next()
if err != nil {
return err
}
return insertAuditlog(app, e.Auth, e.Record, "update")
})
app.OnRecordDeleteRequest().BindFunc(func(e *core.RecordRequestEvent) error {
err := e.Next()
if err != nil {
return err
}
return insertAuditlog(app, e.Auth, e.Record, "delete")
})
app.OnServe().BindFunc(func(e *core.ServeEvent) error {
e.Router.GET("/{path...}", apis.Static(os.DirFS("./pb_public"), false))
return e.Next()
})
if err := app.Start(); err != nil {
log.Fatal(err)
}
}
func handler(ctx context.Context, b *bot.Bot, update *models.Update) {
log.Println("Received message from chat: " + strconv.FormatInt(update.Message.Chat.ID, 10))
log.Println("Message: " + update.Message.Text)
b.SendMessage(ctx, &bot.SendMessageParams{
ChatID: update.Message.Chat.ID,
Text: "Your Chat ID is: " + strconv.FormatInt(update.Message.Chat.ID, 10),
})
}