-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
149 lines (130 loc) · 3.21 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
package main
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"os"
"strings"
"github.com/gin-gonic/gin"
"github.com/go-lark/lark"
"github.com/go-lark/lark-gin"
)
const (
playgroundCompileURL = "https://play.golang.org/compile"
playgroundFmtURL = "https://play.golang.org/fmt"
)
type codeSession struct {
chatID int
code string
result string
}
type compileEvent struct {
Delay int `json:"Delay"`
Message string `json:"Message"`
Kind string `json:"Kind"`
}
type compileBody struct {
Errors string `json:"Errors"`
Events []compileEvent `json:"Events"`
}
type fmtBody struct {
Body string `json:"Body"`
Error string `json:"Error"`
}
var client *http.Client
func main() {
var (
appID = os.Getenv("LARK_APP_ID")
appSecret = os.Getenv("LARK_APP_SECRET")
)
client = http.DefaultClient
r := gin.Default()
middleware := larkgin.NewLarkMiddleware()
r.Use(middleware.LarkChallengeHandler())
r.Use(middleware.LarkMessageHandler())
bot := lark.NewChatBot(appID, appSecret)
bot.GetAccessTokenInternal(true)
bot.StartHeartbeat()
r.POST("/go", func(c *gin.Context) {
message, ok := middleware.GetMessage(c)
if !ok {
return
}
c.JSON(200, gin.H{
"status": "ok",
})
text := message.Event.RealText
cmd, code := parseToken(text)
var result string
if cmd == "run" {
resp, err := runWithPlayground(code)
if err != nil {
bot.PostTextMention("Run failed", message.Event.OpenID, lark.WithChatID(message.Event.OpenChatID))
return
}
if len(resp.Errors) != 0 {
bot.PostTextMention(resp.Errors, message.Event.OpenID, lark.WithChatID(message.Event.OpenChatID))
return
}
for _, event := range resp.Events {
result += fmt.Sprintf("Delay: %d [%s] %s\n", event.Delay, event.Kind, event.Message)
}
if result == "" {
result = "Done without errors or outputs."
}
} else if cmd == "fmt" {
resp, err := fmtWithPlayground(code)
if err != nil {
bot.PostTextMention("Fmt failed", message.Event.OpenID, lark.WithChatID(message.Event.OpenChatID))
return
}
if len(resp.Error) != 0 {
bot.PostTextMention(resp.Error, message.Event.OpenID, lark.WithChatID(message.Event.OpenChatID))
return
}
result = resp.Body
} else {
result = "run\nfmt\nhelp"
}
bot.PostTextMention(result, message.Event.OpenID, lark.WithChatID(message.Event.OpenChatID))
})
r.Run(":9967")
}
func parseToken(text string) (string, string) {
tokens := strings.SplitN(text, "\n", 2)
if len(tokens) == 0 {
return "", ""
}
token := strings.Trim(tokens[0], " ")
code := ""
if len(tokens) > 1 {
code = tokens[1]
}
return token, code
}
func runWithPlayground(code string) (*compileBody, error) {
resp, err := client.PostForm(playgroundCompileURL, url.Values{
"version": {"2"},
"body": {code},
})
if err != nil {
return nil, err
}
defer resp.Body.Close()
var result compileBody
err = json.NewDecoder(resp.Body).Decode(&result)
return &result, err
}
func fmtWithPlayground(code string) (*fmtBody, error) {
resp, err := client.PostForm(playgroundFmtURL, url.Values{
"body": {code},
})
if err != nil {
return nil, err
}
defer resp.Body.Close()
var result fmtBody
err = json.NewDecoder(resp.Body).Decode(&result)
return &result, err
}