-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
206 lines (166 loc) · 4.9 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
package main
import (
"bytes"
"fmt"
"os"
"strings"
"github.com/phin1x/go-ipp"
"github.com/slack-go/slack/socketmode"
"github.com/charmbracelet/log"
"github.com/slack-go/slack"
"github.com/slack-go/slack/slackevents"
)
type PendingPrint struct {
File string
PrinterMessageTs string
}
var PendingPrints map[string]PendingPrint
var MyID string
type Printer struct {
Queue string
DisplayName string
Note string
Reaction string
}
var Printers = []Printer{
{Queue: "KONICA-MINOLTA-magicolor-1690MF", DisplayName: "Farebne", Note: "KONICA MINOLTA MAGICOLOR!", Reaction: "rainbow"},
{Queue: "Hermiona", DisplayName: "Čiernobielo", Note: "Hermiona", Reaction: "black_circle"},
}
func main() {
appToken := os.Getenv("PRINTBOT_APP_TOKEN")
botToken := os.Getenv("PRINTBOT_BOT_TOKEN")
PendingPrints = make(map[string]PendingPrint)
api := slack.New(
botToken,
slack.OptionAppLevelToken(appToken),
slack.OptionLog(log.WithPrefix("slack api").StandardLog()),
)
resp, err := api.AuthTest()
if err != nil {
log.Error("Could not authenticate against Slack API.", "err", err)
os.Exit(1)
}
MyID = resp.UserID
client := socketmode.New(api, socketmode.OptionLog(log.WithPrefix("slack socket").StandardLog()))
handler := socketmode.NewSocketmodeHandler(client)
handler.HandleEvents(slackevents.Message, handleMessage)
handler.HandleEvents(slackevents.ReactionAdded, handleReaction)
err = handler.RunEventLoop()
if err != nil {
log.Error("Error while running event loop.", "err", err)
os.Exit(1)
}
}
func sendMessage(client *socketmode.Client, ch string, msg string) {
_, _, err := client.Client.PostMessage(ch, slack.MsgOptionText(msg, false))
if err != nil {
log.Error("Failed posting message", "err", err)
}
}
func addPendingPrint(channel string, file string, client *socketmode.Client) {
PendingPrints[channel] = PendingPrint{
File: file,
PrinterMessageTs: askPrinter(channel, client),
}
}
func askPrinter(channel string, client *socketmode.Client) string {
printers := []string{}
for _, printer := range Printers {
printers = append(printers, fmt.Sprintf("- :%s: *%s* _(%s)_", printer.Reaction, printer.DisplayName, printer.Note))
}
_, timestamp, err := client.PostMessage(channel, slack.MsgOptionText("Ako to chceš vytlačiť?\n\n"+strings.Join(printers, "\n"), false))
if err != nil {
log.Error("Failed posting message", "err", err)
return ""
}
for _, r := range []string{"rainbow", "black_circle"} {
err = client.AddReaction(r, slack.NewRefToMessage(channel, timestamp))
if err != nil {
log.Error("Could not add reaction.", "err", err)
}
}
return timestamp
}
func handleMessage(evt *socketmode.Event, client *socketmode.Client) {
eventsAPIEvent, ok := evt.Data.(slackevents.EventsAPIEvent)
if !ok {
log.Warn("Ignoring malformed event.")
return
}
client.Ack(*evt.Request)
ev, ok := eventsAPIEvent.InnerEvent.Data.(*slackevents.MessageEvent)
if !ok {
log.Warn("Ignoring malformed event.")
return
}
if ev.User == MyID {
return
}
if ev.SubType != "" && ev.SubType != "file_share" {
return
}
if len(ev.Files) != 1 {
sendMessage(client, ev.Channel, "Pošli mi prosím ťa jedno PDF, ktoré chceš vytlačiť.")
return
}
addPendingPrint(ev.Channel, ev.Files[0].URLPrivateDownload, client)
}
func handleReaction(evt *socketmode.Event, client *socketmode.Client) {
eventsAPIEvent, ok := evt.Data.(slackevents.EventsAPIEvent)
if !ok {
log.Warn("Ignoring malformed event.")
return
}
client.Ack(*evt.Request)
ev, ok := eventsAPIEvent.InnerEvent.Data.(*slackevents.ReactionAddedEvent)
if !ok {
log.Warn("Ignoring malformed event.")
return
}
if ev.User == MyID {
return
}
channel := ev.Item.Channel
print, exists := PendingPrints[channel]
if !exists {
return
}
if ev.Item.Timestamp != print.PrinterMessageTs {
return
}
var printer Printer
for _, p := range Printers {
if p.Reaction == ev.Reaction {
printer = p
}
}
if printer.Reaction == "" {
return
}
_, _, err := client.DeleteMessage(channel, ev.Item.Timestamp)
if err != nil {
log.Error("Could not delete message.", "err", err)
}
var buf bytes.Buffer
err = client.GetFile(print.File, &buf)
if err != nil {
log.Error("Could not download file.", "err", err)
sendMessage(client, channel, "Niečo sa pokazilo. :(")
return
}
doc := ipp.Document{
Document: &buf,
Size: buf.Len(),
Name: "printbot.pdf",
MimeType: "application/pdf",
}
ippClient := ipp.NewIPPClient(os.Getenv("PRINTBOT_CUPS_IP"), 631, "", "", false)
job, err := ippClient.PrintJob(doc, printer.Queue, map[string]interface{}{})
if err != nil {
log.Error("Could not send print job.", "printer", printer.Queue, "err", err)
sendMessage(client, channel, "Niečo sa pokazilo. :(")
return
}
log.Info("Sending print job.", "user", ev.User, "file", print.File, "printer", printer.Queue, "job", job)
sendMessage(client, channel, "Súbor odoslaný na tlač.")
}