-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
89 lines (71 loc) · 1.91 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
package bot
import (
"fmt"
"log"
"os"
"regexp"
"strings"
c "github.com/chrispruitt/go-slackbot/lib/config"
"github.com/robfig/cron/v3"
"github.com/slack-go/slack"
"github.com/slack-go/slack/slackevents"
)
var (
SlackClient *slack.Client
config *Config
scripts map[string]Script
periodicScripts map[string]PeriodicScript
cronJobs *cron.Cron
)
type ScriptFunction func(*ScriptContext)
type Script struct {
Name string
Matcher Matcher
Description string
Function ScriptFunction
}
func init() {
SlackClient = slack.New(
c.SlackBotToken,
slack.OptionAppLevelToken(c.SlackAppToken),
slack.OptionDebug(true),
slack.OptionLog(log.New(os.Stdout, "api: ", log.Lshortfile|log.LstdFlags)),
)
}
func RegisterScript(script Script) {
if scripts == nil {
scripts = make(map[string]Script)
}
scripts[script.Name] = script
}
func PostMessage(channelID string, message string) (string, string, error) {
if c.ShellMode && c.ShellModeChannel == "" {
fmt.Println(message)
return "", "", nil
} else {
return SlackClient.PostMessage(channelID, slack.MsgOptionText(message, false))
}
}
func HandleMessageEvent(event *slackevents.MessageEvent) {
if strings.HasPrefix(event.Text, fmt.Sprintf("%s ", c.BotName)) {
// Strip out BotName
re := regexp.MustCompile(fmt.Sprintf(`^%s *`, c.BotName))
event.Text = re.ReplaceAllString(event.Text, "")
for _, script := range scripts {
if match(script.Matcher.toRegex(), event.Text) {
ScriptContext := &ScriptContext{
SlackEvent: event,
}
ScriptContext.Arguments = script.Matcher.getArguments(event.Text)
// Execute scripts asynchronously so bot can keep listening
go script.Function(ScriptContext)
return
}
}
PostMessage(event.Channel, "Sorry, I don't know that command.")
}
}
func match(matcher string, content string) bool {
re := regexp.MustCompile(matcher)
return re.MatchString(content)
}