Skip to content

Commit 9d6b2ba

Browse files
ijbhxhuJARVIS-coding-Agent
authored andcommitted
feat(gateway): address review comments for multimodal inbound
1 parent 1e8ca37 commit 9d6b2ba

3 files changed

Lines changed: 19 additions & 8 deletions

File tree

gateway/src/adapters/line.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,9 @@ pub async fn webhook(
107107
let mut attachments = Vec::new();
108108
if is_image || is_audio {
109109
if let Some(ref access_token) = state.line_access_token {
110-
let client = reqwest::Client::new();
110+
let client = &state.client;
111111
let att_type = if is_image { "image" } else { "audio" };
112-
if let Some(att) = download_line_media(&client, access_token, &msg.id, att_type).await {
112+
if let Some(att) = download_line_media(client, access_token, &msg.id, att_type).await {
113113
attachments.push(att);
114114
}
115115
} else {
@@ -321,7 +321,7 @@ async fn download_line_media(
321321
.headers()
322322
.get(reqwest::header::CONTENT_TYPE)
323323
.and_then(|h| h.to_str().ok())
324-
.unwrap_or(if attachment_type == "image" { "image/jpeg" } else { "audio/x-m4a" })
324+
.unwrap_or(if attachment_type == "image" { "image/jpeg" } else { "audio/mp4" })
325325
.to_string();
326326

327327
let bytes = resp.bytes().await.ok()?;
@@ -346,6 +346,7 @@ async fn download_line_media(
346346

347347
use base64::Engine;
348348
let b64_data = base64::engine::general_purpose::STANDARD.encode(&data_bytes);
349+
info!(message_id, size = data_bytes.len(), "LINE {} download successful", attachment_type);
349350

350351
Some(Attachment {
351352
attachment_type: attachment_type.into(),

gateway/src/adapters/telegram.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,29 +122,29 @@ pub async fn webhook(
122122
let mut attachments = Vec::new();
123123
if is_photo || is_document || is_voice || is_audio {
124124
if let Some(ref token) = state.telegram_bot_token {
125-
let client = reqwest::Client::new();
125+
let client = &state.client;
126126
if is_photo {
127127
// Take the largest photo
128128
if let Some(largest) = msg.photo.iter().max_by_key(|p| p.width * p.height) {
129129
if let Some(att) =
130-
download_telegram_media(&client, token, &largest.file_id, "image").await
130+
download_telegram_media(client, token, &largest.file_id, "image").await
131131
{
132132
attachments.push(att);
133133
}
134134
}
135135
} else if let Some(doc) = msg.document {
136136
let file_name = doc.file_name.unwrap_or_else(|| "unknown.txt".to_string());
137137
if let Some(att) =
138-
download_telegram_document(&client, token, &doc.file_id, &file_name).await
138+
download_telegram_document(client, token, &doc.file_id, &file_name).await
139139
{
140140
attachments.push(att);
141141
}
142142
} else if let Some(voice) = msg.voice {
143-
if let Some(att) = download_telegram_media(&client, token, &voice.file_id, "audio").await {
143+
if let Some(att) = download_telegram_media(client, token, &voice.file_id, "audio").await {
144144
attachments.push(att);
145145
}
146146
} else if let Some(audio) = msg.audio {
147-
if let Some(att) = download_telegram_media(&client, token, &audio.file_id, "audio").await {
147+
if let Some(att) = download_telegram_media(client, token, &audio.file_id, "audio").await {
148148
attachments.push(att);
149149
}
150150
}
@@ -409,6 +409,7 @@ async fn download_telegram_media(
409409

410410
use base64::Engine;
411411
let b64_data = base64::engine::general_purpose::STANDARD.encode(&data_bytes);
412+
info!(file_id, size = data_bytes.len(), "Telegram {} download successful", attachment_type);
412413

413414
Some(Attachment {
414415
attachment_type: attachment_type.into(),
@@ -482,6 +483,7 @@ async fn download_telegram_document(
482483
let text = String::from_utf8_lossy(&bytes);
483484
use base64::Engine;
484485
let data = base64::engine::general_purpose::STANDARD.encode(text.as_bytes());
486+
info!(file_id, file_name, size = bytes.len(), "Telegram document download successful");
485487

486488
Some(Attachment {
487489
attachment_type: "text_file".into(),

gateway/src/main.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,11 @@ pub struct AppState {
6161
/// the first client to `remove()` a token wins the free Reply API call;
6262
/// other clients for the same event naturally fall back to Push API.
6363
pub reply_token_cache: ReplyTokenCache,
64+
/// Shared HTTP client for media downloads and API calls
65+
pub client: reqwest::Client,
6466
}
6567

68+
6669
// --- WebSocket handler (OAB connects here) ---
6770

6871
async fn ws_handler(
@@ -356,6 +359,10 @@ async fn main() -> Result<()> {
356359
warn!("no adapters configured — set TELEGRAM_BOT_TOKEN, LINE_CHANNEL_ACCESS_TOKEN, TEAMS_APP_ID + TEAMS_APP_SECRET, FEISHU_APP_ID + FEISHU_APP_SECRET, GOOGLE_CHAT_ENABLED=true, and/or WECOM_CORP_ID + WECOM_SECRET + WECOM_TOKEN + WECOM_ENCODING_AES_KEY + WECOM_AGENT_ID");
357360
}
358361

362+
let client = reqwest::Client::builder()
363+
.timeout(std::time::Duration::from_secs(30))
364+
.build()?;
365+
359366
let state = Arc::new(AppState {
360367
telegram_bot_token,
361368
telegram_secret_token,
@@ -369,6 +376,7 @@ async fn main() -> Result<()> {
369376
ws_token,
370377
event_tx,
371378
reply_token_cache,
379+
client,
372380
});
373381

374382
// Background task: sweep expired reply tokens every REPLY_TOKEN_TTL_SECS

0 commit comments

Comments
 (0)