-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
60 lines (49 loc) · 1.44 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
package main
import (
"context"
"encoding/json"
"fmt"
"strings"
larkcore "github.com/larksuite/oapi-sdk-go/v3/core"
larkevent "github.com/larksuite/oapi-sdk-go/v3/event"
"github.com/larksuite/oapi-sdk-go/v3/event/dispatcher"
larkws "github.com/larksuite/oapi-sdk-go/v3/ws"
)
var commandQueue = &CommandQueue{}
func main() {
eventHandler := dispatcher.NewEventDispatcher("", "").
OnCustomizedEvent("im.message.receive_v1", HandleMessage)
cli := larkws.NewClient(AppID, AppSecret,
larkws.WithEventHandler(eventHandler),
larkws.WithLogLevel(larkcore.LogLevelInfo),
)
ctx := context.Background()
go processCommands(ctx, commandQueue)
err := cli.Start(ctx)
if err != nil {
panic(err)
}
}
func HandleMessage(ctx context.Context, event *larkevent.EventReq) error {
var eventBody EventBody
err := json.Unmarshal([]byte(event.Body), &eventBody)
if err != nil {
return err
}
message := eventBody.Event.Message.Content.Text
// Start with a slash, it's a command
if message[0] == '/' {
parts := strings.Fields(eventBody.Event.Message.Content.Text)
if len(parts) == 0 {
return nil
}
command := parts[0]
args := parts[1:]
fmt.Println("Received command:", command, "Args:", args)
commandQueue.Enqueue(Command{Type: command, Args: args, Event: eventBody.Event})
} else {
fmt.Println("Received message:", message)
handleReply(ctx, Command{Type: message, Args: make([]string, 0), Event: eventBody.Event})
}
return nil
}