/
message.go
157 lines (122 loc) · 4.13 KB
/
message.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
package bot
import (
"fmt"
"html"
"regexp"
"sort"
"strings"
"github.com/stjohnjohnson/reddit-watcher/internal/matcher"
)
var helpText = `
You can subscribe or unsubscribe to events by using the following commands:
/buying <keyword> - something being bought
/selling <keyword> - something being sold
/vendor <keyword> - updates from vendors
/artisan <keyword> - updates from artisans
/groupbuy <keyword> - updates about group buys
/interestcheck <keyword> - feedback about a design
/giveaway <keyword> - something being given away
Other options:
/items - returns list of watched items
/stats - returns stats about the current bot
/help - gets this help message
`
var startText = `
Let's say you are looking for a new Tada68, well you can ask me to look for people selling one by saying:
/selling tada68
If you want to be notified about the next Fugu sale, just tell me:
/artisan fugu
What if you want to be notified about ALL artisan posts, send me this:
/artisan
Unsubscribe at anytime by sending the same message (e.g. /selling tada68). Learn more with /help`
// /COMMAND OPTIONALDATA
var cmdRex = regexp.MustCompile(`(?i)^/(\w+)(?:\s(.+))?$`)
func (b *Handler) incomingMessage(userID int64, message string) error {
fields := cmdRex.FindStringSubmatch(message)
if fields == nil {
return nil
}
var resp string
switch cmd := fields[1]; cmd {
case matcher.Buying, matcher.Selling, matcher.Artisan, matcher.Vendor,
matcher.GroupBuy, matcher.InterestCheck, matcher.Giveaway:
resp = b.handleSubscribe(userID, cmd, fields[2])
case "items":
resp = b.handleWatchlist(userID)
case "stats":
resp = b.handleStats()
case "start":
resp = b.handleStart()
case "help":
resp = b.handleHelp()
default:
resp = "That command doesn't look like anything to me."
}
err := b.chat.SendMessage(userID, resp)
if err != nil {
return fmt.Errorf("Unable to send message: %v", err)
}
return nil
}
func (b *Handler) handleSubscribe(userID int64, cmd, keyword string) string {
if keyword == "" {
keyword = "*"
}
if b.data[cmd].Exists(userID, keyword) {
err := b.data[cmd].Remove(userID, keyword)
if err != nil {
b.logger.Println("Unable to remove keyword: ", err)
}
return fmt.Sprintf("I'm no longer watching for <b>%s</b> posts that match <b>%s</b>", html.EscapeString(cmd), html.EscapeString(keyword))
}
err := b.data[cmd].Add(userID, keyword)
if err != nil {
b.logger.Println("Unable to add keyword: ", err)
}
// @TODO better message for ALL events
return fmt.Sprintf("Okay, I'm going to watch for <b>%s</b> posts that match <b>%s</b>", html.EscapeString(cmd), html.EscapeString(keyword))
}
func (b *Handler) handleWatchlist(userID int64) string {
resp := []string{}
for _, t := range matcher.Types {
crit := b.data[t].Get(userID)
if len(crit) == 0 {
continue
}
keys := make([]string, 0)
for k := range crit {
keys = append(keys, k)
}
sort.Strings(keys)
resp = append(resp, fmt.Sprintf("<b>%s:</b>", strings.ToUpper(t)))
for _, keyword := range keys {
resp = append(resp, fmt.Sprintf(" - %v <i>(%d hits)</i>", html.EscapeString(keyword), crit[keyword]))
}
resp = append(resp, "")
}
if len(resp) > 0 {
return fmt.Sprintf("These are your current watch items:\n%v", strings.Join(resp, "\n"))
}
return "There are no items on your watch list"
}
func (b *Handler) handleHelp() string {
return fmt.Sprintf(`Hi, I'm <a href="https://github.com/stjohnjohnson/reddit-watcher">reddit-watcher@%v</a>. I watch /r/mechmarket for specific keywords%s`, b.version, html.EscapeString(helpText))
}
func (b *Handler) handleStart() string {
return fmt.Sprintf(`Hi, I'm <a href="https://github.com/stjohnjohnson/reddit-watcher">reddit-watcher@%v</a>. I watch /r/mechmarket for specific keywords%s`, b.version, html.EscapeString(startText))
}
func (b *Handler) handleStats() string {
resp := []string{
"<b>Interesting Statistics:</b>",
}
stats := b.stats.GetAll()
keys := make([]string, 0)
for k := range stats {
keys = append(keys, k)
}
sort.Strings(keys)
for _, field := range keys {
resp = append(resp, fmt.Sprintf(" - %v <i>(%s)</i>", html.EscapeString(field), stats[field]))
}
return strings.Join(resp, "\n")
}