-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
130 lines (115 loc) · 2.72 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
package main
import (
"io"
"log"
"net/http"
"os"
"os/exec"
"strings"
"github.com/slack-go/slack"
"gopkg.in/yaml.v2"
)
var (
OAUTH_TOKEN string
api *slack.Client
CHANNEL_ID string
SIGNING_SECRET string
PORT string
)
func init() {
parseYamlFile()
api = slack.New(OAUTH_TOKEN)
}
type configs struct {
OAUTH_TOKEN string `yaml:"OAUTH_TOKEN"`
CHANNEL_ID string `yaml:"CHANNEL_ID"`
SIGNING_SECRET string `yaml:"SIGNING_SECRET"`
PORT string `yaml:"PORT"`
}
func (c *configs) Parse(data []byte) error {
return yaml.Unmarshal(data, c)
}
func parseYamlFile() {
data, err := os.ReadFile("config.yaml")
checkErr(err)
var config configs
err = config.Parse(data)
checkErr(err)
OAUTH_TOKEN = config.OAUTH_TOKEN
CHANNEL_ID = config.CHANNEL_ID
SIGNING_SECRET = config.SIGNING_SECRET
PORT = config.PORT
}
func checkErr(err error) {
if err != nil {
log.Fatal(err)
}
}
func deleteAllMessages() {
info, _ := api.GetConversationHistory(&slack.GetConversationHistoryParameters{ChannelID: CHANNEL_ID})
for _, message := range info.Messages {
log.Println(api.DeleteMessage(CHANNEL_ID, message.Msg.Timestamp))
}
}
func sendMessage(message string) {
_, _, err := api.PostMessage(
CHANNEL_ID,
slack.MsgOptionText(message, false),
slack.MsgOptionAsUser(false),
)
checkErr(err)
}
func handler(w http.ResponseWriter, r *http.Request) {
verifier, err := slack.NewSecretsVerifier(r.Header, SIGNING_SECRET)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
r.Body = io.NopCloser(io.TeeReader(r.Body, &verifier))
s, err := slack.SlashCommandParse(r)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
if err = verifier.Ensure(); err != nil {
w.WriteHeader(http.StatusUnauthorized)
return
}
log.Println(s.Command)
switch s.Command {
case "/sh":
if s.Text == "del" {
deleteAllMessages()
} else if s.Text != "" {
command := strings.Split(s.Text, " ")
var out []byte
var err error
if len(command) > 1 {
out, err = exec.Command(command[0], command[1:]...).CombinedOutput()
} else {
out, err = exec.Command(command[0]).CombinedOutput()
}
if len(out) > 0 {
message := string(out)
if err != nil {
message += "\n" + err.Error()
}
sendMessage(message)
} else if err != nil {
sendMessage(err.Error())
} else {
sendMessage("Executed, but didn't get response from command yet.")
}
} else {
sendMessage("I need an argument to the `/sh` command!\nExample: `/sh del`")
}
default:
w.WriteHeader(http.StatusInternalServerError)
return
}
}
func main() {
http.HandleFunc("/", handler)
log.Println("[INFO] Server listening on port: " + PORT)
http.ListenAndServe(":"+PORT, nil)
}