fix: truncate long text content before sending to Feishu

Feishu API rejects messages with content exceeding ~64KB with error
230001 "invalid message content". Added truncation at 60,000 characters
to prevent this, with a notice appended to truncated content.
This commit is contained in:
xiaoski 2026-04-08 08:42:56 +08:00
parent 02a7fa68c6
commit 075b92f231

View File

@ -489,7 +489,16 @@ impl FeishuChannel {
async fn send_message_to_feishu(&self, receive_id: &str, receive_id_type: &str, content: &str) -> Result<(), ChannelError> { async fn send_message_to_feishu(&self, receive_id: &str, receive_id_type: &str, content: &str) -> Result<(), ChannelError> {
let token = self.get_tenant_token().await?; let token = self.get_tenant_token().await?;
let text_content = serde_json::json!({ "text": content }).to_string(); // Feishu text messages have content limits (~64KB).
// Truncate if content is too long to avoid API error 230001.
const MAX_TEXT_LENGTH: usize = 60_000;
let truncated = if content.len() > MAX_TEXT_LENGTH {
format!("{}...\n\n[Content truncated due to length limit]", &content[..MAX_TEXT_LENGTH])
} else {
content.to_string()
};
let text_content = serde_json::json!({ "text": truncated }).to_string();
let resp = self.http_client let resp = self.http_client
.post(format!("{}/im/v1/messages?receive_id_type={}", FEISHU_API_BASE, receive_id_type)) .post(format!("{}/im/v1/messages?receive_id_type={}", FEISHU_API_BASE, receive_id_type))
@ -906,11 +915,17 @@ impl Channel for FeishuChannel {
// Build content with media references // Build content with media references
let mut content_parts = Vec::new(); let mut content_parts = Vec::new();
// Add text content if present // Add text content if present (truncate if too long for Feishu)
if !msg.content.is_empty() { if !msg.content.is_empty() {
const MAX_TEXT_LENGTH: usize = 60_000;
let truncated_text = if msg.content.len() > MAX_TEXT_LENGTH {
format!("{}...\n\n[Content truncated due to length limit]", &msg.content[..MAX_TEXT_LENGTH])
} else {
msg.content.clone()
};
content_parts.push(serde_json::json!({ content_parts.push(serde_json::json!({
"tag": "text", "tag": "text",
"text": msg.content "text": truncated_text
})); }));
} }