-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
72 lines (64 loc) · 2.37 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
package main
import (
"context"
"encoding/json"
"log"
"net/http"
"regexp"
larkcore "github.com/larksuite/oapi-sdk-go/v3/core"
"github.com/larksuite/oapi-sdk-go/v3/core/httpserverext"
larkevent "github.com/larksuite/oapi-sdk-go/v3/event"
"github.com/larksuite/oapi-sdk-go/v3/event/dispatcher"
larkim "github.com/larksuite/oapi-sdk-go/v3/service/im/v1"
bingdalle3 "github.com/mrchi/bing-dalle3"
"github.com/mrchi/lark-dalle3-bot/internal/botconfig"
cmddispatcher "github.com/mrchi/lark-dalle3-bot/pkg/dispatcher"
larkee "github.com/mrchi/lark-dalle3-bot/pkg/larkee"
"github.com/sashabaranov/go-openai"
)
var (
config *botconfig.BotConfig
bingClient *bingdalle3.BingDalle3
gptClient *openai.Client
)
func init() {
var err error
config, err = botconfig.ReadConfigFromFile("./config.json")
if err != nil {
panic(err)
}
bingClient = bingdalle3.NewBingDalle3(config.BingCookie)
gptClient = openai.NewClient(config.GPTAPIKey)
}
func main() {
var larkeeClient *larkee.LarkClient
if config.IsFeishu {
larkeeClient = larkee.NewFeishuClient(config.LarkAppID, config.LarkAppSecret, larkcore.LogLevel(config.LarkLogLevel))
log.Println("Initialize client for Feishu")
} else {
larkeeClient = larkee.NewLarkClient(config.LarkAppID, config.LarkAppSecret, larkcore.LogLevel(config.LarkLogLevel))
log.Println("Initialize client for Lark")
}
commandDispatcher := cmddispatcher.NewCommandDispatcher(larkeeClient, commandHelpExecute, commandBalance, commandPrompt, commandPromptPro)
larkEventDispatcher := dispatcher.NewEventDispatcher(config.LarkVerificationToken, config.LarkEventEncryptKey)
larkEventDispatcher.OnP2MessageReceiveV1(func(ctx context.Context, event *larkim.P2MessageReceiveV1) error {
// 获取文本消息内容
var msgContent larkee.LarkTextMessage
json.Unmarshal([]byte(*event.Event.Message.Content), &msgContent)
// 过滤 @ 信息
text := regexp.MustCompile(`\s*@_all|@_user_\d+\s*`).ReplaceAllString(msgContent.Text, "")
commandDispatcher.Dispatch(text, *event.Event.Message.MessageId, event.TenantKey())
return nil
},
)
urlPath := "/dalle3"
http.HandleFunc(
urlPath,
httpserverext.NewEventHandlerFunc(
larkEventDispatcher,
larkevent.WithLogLevel(larkcore.LogLevel(config.LarkLogLevel)),
),
)
log.Printf("Start server at %s, url path is %s\n", config.LarkEventServerAddr, urlPath)
http.ListenAndServe(config.LarkEventServerAddr, nil)
}