-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatreply.go
201 lines (173 loc) · 5.15 KB
/
chatreply.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
package main
import (
"bufio"
"flag"
"fmt"
"log"
"log/slog"
"os"
"os/signal"
"path/filepath"
"strings"
"syscall"
"github.com/erykksc/chatreply/pkg/configuration"
"github.com/erykksc/chatreply/pkg/providers"
"github.com/erykksc/chatreply/pkg/utils"
)
// Variables used for command line parameters
var (
ConfigPath string
Separator string
MsgSeparator string
OutSeparator string
SkipReplies bool
WatchEmoji string
TextOnlyMsgs bool
RepliesCount int
Verbose bool
)
func init() {
defaultConfPath := "$XDG_CONFIG_HOME/chatreply/conf.toml"
flag.StringVar(&ConfigPath, "f", defaultConfPath, "Filepath of the config .toml file")
flag.StringVar(&Separator, "s", ":", "Separator between message and emoji")
flag.StringVar(&MsgSeparator, "msg-sep", "\n", "Separator between messages")
flag.StringVar(&OutSeparator, "out-sep", "\n", "Separator between output messages")
flag.BoolVar(&SkipReplies, "skip-replies", false, "Do not wait for replies, just send the messages")
flag.StringVar(&WatchEmoji, "watch-emoji", "👀", "Emoji used to indicate the program is watching the message for a reply")
flag.BoolVar(&TextOnlyMsgs, "text-only", false, "Make all messages text only, disable trying to parse messages as multimedia")
flag.IntVar(&RepliesCount, "replies-count", 1, "Number of replies to wait per message for before exiting, -1 will wait indefinitely, allowing multiple replies per message")
flag.BoolVar(&Verbose, "v", false, "Sets logging level to Debug")
flag.Parse()
if ConfigPath == defaultConfPath {
xdgConfigHome := os.Getenv("XDG_CONFIG_HOME")
ConfigPath = filepath.Join(xdgConfigHome, "chatreply", "conf.toml")
}
if RepliesCount == 0 {
slog.Error("replies-count can't be 0")
os.Exit(-1)
}
}
type UnresolvedMsg struct {
MsgContent string
RepliesCollected int
}
// Map: MessageID -> UnresolvedMsg
var unresolvedMsgs = make(map[string]*UnresolvedMsg)
func main() {
// Setup logging
logOptions := slog.HandlerOptions{}
if Verbose {
logOptions.Level = slog.LevelDebug
} else {
logOptions.Level = slog.LevelWarn
}
logger := slog.New(slog.NewTextHandler(os.Stderr, &logOptions))
slog.SetDefault(logger)
config, err := configuration.LoadConfiguration(ConfigPath)
if err != nil {
log.Fatalf("error loading configuration: %s", err)
}
provider, err := providers.CreateProvider(config)
if err != nil {
log.Fatalf("error creating provider: %s", err)
}
provider.Init()
defer provider.Close()
slog.Info("Provider initialized")
// Send messages and add watch reactions
scanner := bufio.NewScanner(os.Stdin)
scanner.Split(utils.SplitBySeparator([]byte(MsgSeparator)))
for scanner.Scan() {
line := scanner.Text()
if len(line) == 0 {
continue
}
msgID, err := provider.SendMessage(line, TextOnlyMsgs)
if err != nil {
log.Fatalf("error sending message: %s", err)
}
if SkipReplies {
continue
}
err = provider.AddReaction(msgID, WatchEmoji)
if err != nil {
log.Fatalf("error adding reaction: %s", err)
}
unresolvedMsgs[msgID] = &UnresolvedMsg{
MsgContent: line,
}
}
// Don't wait for replies if the flag is on
if SkipReplies {
return
}
slog.Info("Bot is now running. Press CTRL-C to exit.")
var sc = make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
// Wait here until:
// * All messages get reactions
// * CTRL-C or other term signal is received.
EventsLoop:
for len(unresolvedMsgs) > 0 {
select {
case msg := <-provider.MessagesChannel():
onReply(provider, Reply{
RefMsgID: msg.ReferencedMsgID,
Content: msg.Content,
})
case reaction := <-provider.ReactionsChannel():
onReply(provider, Reply{
RefMsgID: reaction.MessageID,
Content: reaction.Content,
})
case <-sc:
slog.Info("shutting down...")
break EventsLoop
}
}
// Cleanup unresolved Messages
for messageID := range unresolvedMsgs {
provider.RemoveReaction(messageID, WatchEmoji)
}
}
type Reply struct {
RefMsgID string // ID of the message the reply was to
Content string
}
func (r Reply) String() string {
return fmt.Sprintf("RefMsgID: %s, Content: %s", r.RefMsgID, r.Content)
}
// onReply is run when an unresolved message receives a response
func onReply(p providers.MsgProvider, reply Reply) {
slog.Debug("handling reply", "reply", reply)
// Check if referenced
if reply.RefMsgID == "" {
slog.Info("message does not reference another message, skipping", "reply", reply)
return
}
unresolvedMsg, ok := unresolvedMsgs[reply.RefMsgID]
if !ok {
slog.Info("message not found in unresolved messages, skipping", "reply", reply)
return
}
b := strings.Builder{}
b.WriteString(unresolvedMsg.MsgContent)
b.WriteString(Separator)
b.WriteString(reply.Content)
b.WriteString(OutSeparator)
fmt.Fprint(os.Stdout, b.String())
unresolvedMsg.RepliesCollected++
// Check if collect replies indefinitely
if RepliesCount == -1 {
return
}
// If not collected enough replies
if unresolvedMsg.RepliesCollected < RepliesCount {
return
}
delete(unresolvedMsgs, reply.RefMsgID)
err := p.RemoveReaction(reply.RefMsgID, WatchEmoji)
if err != nil {
slog.Error("error removing reaction", "error", err, "messageID", reply.RefMsgID, "reaction", WatchEmoji)
}
}