| 1 | package feishu |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "strings" |
| 7 | |
| 8 | "reasonix/internal/bot" |
| 9 | |
| 10 | larkim "github.com/larksuite/oapi-sdk-go/v3/service/im/v1" |
| 11 | ) |
| 12 | |
| 13 | // EditMessage 原地编辑一条已发送的消息(Im.Message.Patch,仅对 interactive |
| 14 | // card 消息有效)。bot 渲染器用它实现回合中的流式输出。 |
| 15 | func (a *adapter) EditMessage(ctx context.Context, messageID string, msg bot.OutboundMessage) error { |
| 16 | messageID = strings.TrimSpace(messageID) |
| 17 | if messageID == "" { |
| 18 | return fmt.Errorf("feishu edit: empty message id") |
| 19 | } |
| 20 | content, err := buildMarkdownCard(msg.Text) |
| 21 | if err != nil { |
| 22 | return err |
| 23 | } |
| 24 | client, err := a.sdkClient() |
| 25 | if err != nil { |
| 26 | return err |
| 27 | } |
| 28 | return withTransientRetry(ctx, a.logger, "patch message", func(ctx context.Context) error { |
| 29 | req := larkim.NewPatchMessageReqBuilder(). |
| 30 | MessageId(messageID). |
| 31 | Body(larkim.NewPatchMessageReqBodyBuilder().Content(content).Build()). |
| 32 | Build() |
| 33 | resp, err := client.Im.Message.Patch(ctx, req) |
| 34 | if err != nil { |
| 35 | return err |
| 36 | } |
| 37 | if resp == nil { |
| 38 | return fmt.Errorf("feishu patch error: empty response") |
| 39 | } |
| 40 | if !resp.Success() { |
| 41 | return fmt.Errorf("feishu patch error: %s", feishuCodeError(resp.Code, resp.Msg)) |
| 42 | } |
| 43 | return nil |
| 44 | }) |
| 45 | } |
| 46 |