Skip to content

Commit 7d16ec6

Browse files
committed
2 parents 0b61cf8 + e5eb1d5 commit 7d16ec6

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

main.go

+12
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,16 @@ func main() {
129129
Usage: "Sets the proxy tokens for OpenAI API",
130130
EnvVars: []string{"PROXY_OPENAI_API_TOKEN"},
131131
},
132+
&cli.StringFlag{
133+
Name: "custom-command",
134+
Usage: "Custom command, such as: doc => trigger /doc",
135+
EnvVars: []string{"CUSTOM_COMMAND"},
136+
},
137+
&cli.StringFlag{
138+
Name: "custom-command-service",
139+
Usage: "Custom command service, such as: https://example.com/api/doc",
140+
EnvVars: []string{"CUSTOM_COMMAND_SERVICE"},
141+
},
132142
},
133143
})
134144

@@ -156,6 +166,8 @@ func main() {
156166
OpenAIAPIServer: ctx.String("openai-api-server"),
157167
ProxyOpenAIAPIPath: ctx.String("proxy-openai-api-path"),
158168
ProxyOpenAIAPIToken: ctx.String("proxy-openai-api-token"),
169+
CustomCommand: ctx.String("custom-command"),
170+
CustomCommandService: ctx.String("custom-command-service"),
159171
})
160172
})
161173

server.go

+53
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/go-zoox/chatbot-feishu"
99
"github.com/go-zoox/core-utils/regexp"
1010
"github.com/go-zoox/core-utils/strings"
11+
"github.com/go-zoox/fetch"
1112
openaiclient "github.com/go-zoox/openai-client"
1213
"github.com/go-zoox/proxy"
1314
"github.com/go-zoox/proxy/utils/rewriter"
@@ -70,6 +71,10 @@ type FeishuBotConfig struct {
7071
ProxyOpenAIAPIPath string
7172
// ProxyOpenAIAPIToken limits auth with Bearer Token
7273
ProxyOpenAIAPIToken string
74+
75+
// Custom Command with Service
76+
CustomCommand string
77+
CustomCommandService string
7378
}
7479

7580
func ServeFeishuBot(cfg *FeishuBotConfig) (err error) {
@@ -317,6 +322,54 @@ func ServeFeishuBot(cfg *FeishuBotConfig) (err error) {
317322
},
318323
})
319324

325+
if cfg.CustomCommand != "" && cfg.CustomCommandService != "" {
326+
feishuchatbot.OnCommand(cfg.CustomCommand, &chatbot.Command{
327+
ArgsLength: 1,
328+
Handler: func(args []string, request *feishuEvent.EventRequest, reply chatbot.MessageReply) error {
329+
if len(args) != 1 {
330+
return fmt.Errorf("invalid args: %v", args)
331+
}
332+
333+
question := args[0]
334+
logger.Debugf("[custom command: %s, service: %s] question: %s", cfg.CustomCommand, cfg.CustomCommandService, question)
335+
336+
response, err := fetch.Post(cfg.CustomCommandService, &fetch.Config{
337+
Headers: fetch.Headers{
338+
"Content-Type": "application/json",
339+
"Accept": "application/json",
340+
"User-Agent": fmt.Sprintf("go-zoox_fetch/%s chatgpt-for-chatbot-feishu/%s", fetch.Version, Version),
341+
},
342+
Body: map[string]interface{}{
343+
"question": args[0],
344+
},
345+
})
346+
if err != nil {
347+
return fmt.Errorf("failed to request from custom command service(%s): %v", cfg.CustomCommandService, err)
348+
}
349+
350+
if response.Status != http.StatusOK {
351+
return fmt.Errorf("failed to request from custom command service(%s): %s", cfg.CustomCommandService, response.Status)
352+
}
353+
354+
answer := response.Get("answer").String()
355+
if answer == "" {
356+
logger.Error("failed to request from custom command service(%s): empty answer (response: %s)", cfg.CustomCommandService, response.String())
357+
if err := replyText(reply, fmt.Sprintf("no answer found, unexpected response from custom command service(response: %s)", response.String())); err != nil {
358+
return fmt.Errorf("failed to reply: %v", err)
359+
}
360+
361+
return nil
362+
}
363+
364+
if err := replyText(reply, answer); err != nil {
365+
return fmt.Errorf("failed to reply: %v", err)
366+
}
367+
368+
return nil
369+
},
370+
})
371+
}
372+
320373
feishuchatbot.OnMessage(func(text string, request *feishuEvent.EventRequest, reply func(content string, msgType ...string) error) error {
321374
// fmt.PrintJSON(request)
322375
if botInfo == nil {

0 commit comments

Comments
 (0)