|
8 | 8 | "github.com/go-zoox/chatbot-feishu"
|
9 | 9 | "github.com/go-zoox/core-utils/regexp"
|
10 | 10 | "github.com/go-zoox/core-utils/strings"
|
| 11 | + "github.com/go-zoox/fetch" |
11 | 12 | openaiclient "github.com/go-zoox/openai-client"
|
12 | 13 | "github.com/go-zoox/proxy"
|
13 | 14 | "github.com/go-zoox/proxy/utils/rewriter"
|
@@ -70,6 +71,10 @@ type FeishuBotConfig struct {
|
70 | 71 | ProxyOpenAIAPIPath string
|
71 | 72 | // ProxyOpenAIAPIToken limits auth with Bearer Token
|
72 | 73 | ProxyOpenAIAPIToken string
|
| 74 | + |
| 75 | + // Custom Command with Service |
| 76 | + CustomCommand string |
| 77 | + CustomCommandService string |
73 | 78 | }
|
74 | 79 |
|
75 | 80 | func ServeFeishuBot(cfg *FeishuBotConfig) (err error) {
|
@@ -317,6 +322,54 @@ func ServeFeishuBot(cfg *FeishuBotConfig) (err error) {
|
317 | 322 | },
|
318 | 323 | })
|
319 | 324 |
|
| 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 | + |
320 | 373 | feishuchatbot.OnMessage(func(text string, request *feishuEvent.EventRequest, reply func(content string, msgType ...string) error) error {
|
321 | 374 | // fmt.PrintJSON(request)
|
322 | 375 | if botInfo == nil {
|
|
0 commit comments