Skip to content

Commit 4e0785e

Browse files
nolikclaude
andauthored
refactor: make RUST_CHAT configurable and de-swallow errors (#606)
§4 cleanup: - The hardcoded `RUST_CHAT` id becomes a `rust_chat_id` field on `MentionParameters`, read from the `RUST_CHAT_ID` env var with the old value as the default (behavior unchanged when unset; a non-numeric value logs a warning and falls back). Threaded into `handle_rust_matched_mention` via the dispatcher. - The best-effort `.map_err(|err| error!(...)).ok()` sites in chat_gpt_handler (Redis context writes), url_summary_handler and rust_mention_handler (Telegram sends, mention insert) now use `.inspect_err(|err| warn!(...)).ok()`: warn (not error) since these are non-fatal, and `inspect_err` keeps the value/error type so call sites are greppable. Also fixes a mislabeled "Can't insert mention" log that actually guarded the latest-mention-time query. No happy-path behavior change. Local gate green (fmt, clippy -D warnings, unit tests, integration tests compile). Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 172ef66 commit 4e0785e

4 files changed

Lines changed: 34 additions & 16 deletions

File tree

src/boot.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
use std::env;
12
use std::sync::{Arc, LazyLock};
23

34
use chrono::Duration;
4-
use log::error;
5+
use log::{error, warn};
56
use redis::aio::ConnectionManager;
67
use regex::Regex;
78
use sqlx::{PgPool, Pool, Postgres};
@@ -26,6 +27,10 @@ const CHAT_GPT_REGEX: &str = r"(?i)(fedor|ф[её]дор|федя|felix|фели
2627
const URL_REGEX: &str = r#"https?://[^\s<>"{}|\\^`\[\]]*"#;
2728
const MIN_TIME_DIFF: i64 = 15;
2829

30+
/// Chat where rust mentions are counted but not announced. Overridable via the
31+
/// `RUST_CHAT_ID` env var; the default preserves the historically hardcoded id.
32+
const DEFAULT_RUST_CHAT_ID: i64 = -1001228598755;
33+
2934
pub const DEFAULT_OPENAI_BASE_URL: &str = "https://api.openai.com/v1/chat/completions";
3035

3136
// Compile each mention regex exactly once. The patterns are compile-time
@@ -58,6 +63,7 @@ pub struct MentionParameters {
5863
pub chat_gpt_regex: Regex,
5964
pub url_regex: Regex,
6065
pub req_time_diff: Duration,
66+
pub rust_chat_id: i64,
6167
}
6268

6369
impl Default for MentionParameters {
@@ -69,10 +75,21 @@ impl Default for MentionParameters {
6975
chat_gpt_regex: CHAT_GPT_RE.clone(),
7076
url_regex: URL_RE.clone(),
7177
req_time_diff: Duration::minutes(MIN_TIME_DIFF),
78+
rust_chat_id: rust_chat_id_from_env(),
7279
}
7380
}
7481
}
7582

83+
fn rust_chat_id_from_env() -> i64 {
84+
match env::var("RUST_CHAT_ID") {
85+
Ok(raw) => raw.parse().unwrap_or_else(|_| {
86+
warn!("RUST_CHAT_ID='{raw}' is not a valid i64; using default {DEFAULT_RUST_CHAT_ID}");
87+
DEFAULT_RUST_CHAT_ID
88+
}),
89+
Err(_) => DEFAULT_RUST_CHAT_ID,
90+
}
91+
}
92+
7693
pub struct AppDeps {
7794
pub bot: Bot,
7895
pub db_pool: PgPool,
@@ -121,6 +138,7 @@ pub fn build_handler() -> UpdateHandler<RequestError> {
121138
msg,
122139
db_pool,
123140
mention_parameters.req_time_diff,
141+
mention_parameters.rust_chat_id,
124142
)
125143
.await
126144
}

src/chat_gpt_handler.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::chat_gpt_handler::BotProfile::{Fedor, Felix, Ferris};
55
use crate::chat_gpt_handler::ChatMessageRole::{System, User};
66
use crate::gpt_service::{ChatMessage, ChatMessageRole};
77
use crate::{chat_repository, gpt_service, AppError, GptParameters};
8-
use log::{error, info};
8+
use log::{error, info, warn};
99
use redis::aio::ConnectionManager;
1010
use regex::Regex;
1111
use serde::{Deserialize, Serialize};
@@ -151,7 +151,7 @@ async fn update_bot_context_and_identifiers(
151151
context_update,
152152
)
153153
.await
154-
.map_err(|err| error!("Can't update context in Redis: {err:?}"))
154+
.inspect_err(|err| warn!("Can't update context in Redis: {err:?}"))
155155
.ok();
156156
let chat_key = &format!("chat:{:#?}", bot_reply_msg.chat.id.0);
157157
chat_repository::push_bot_msg_identifier(
@@ -161,7 +161,7 @@ async fn update_bot_context_and_identifiers(
161161
bot_profile,
162162
)
163163
.await
164-
.map_err(|err| error!("Can't update context in Redis: {err:?}"))
164+
.inspect_err(|err| warn!("Can't update context in Redis: {err:?}"))
165165
.ok();
166166
}
167167
}

src/rust_mention_handler.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
use chrono::{DateTime, Duration, TimeZone, Utc};
2-
use log::{error, info, warn};
2+
use log::{info, warn};
33
use sqlx::PgPool;
44
use teloxide::prelude::*;
55
use teloxide::types::{InputFile, MessageId, ReplyParameters, ThreadId, User};
66

77
use crate::{mention_repository, AppError};
88

9-
const RUST_CHAT: i64 = -1001228598755;
109
const STICKERS: &[&str; 5] = &[
1110
"CAACAgEAAx0CTdy33AAD3mQO6sc3rzklybqG4MMI4MLXpXJIAAKCAQACaXoxBT0NGBN6KJNELwQ",
1211
"CAACAgEAAx0CTdy33AACAQFkF6KoodtDg4KfcPHlUk_7SRFN7QACkQEAAml6MQW86C1JCZcTkS8E",
@@ -22,6 +21,7 @@ pub async fn handle_rust_matched_mention(
2221
message: Message,
2322
db_pool: PgPool,
2423
req_time_diff: Duration,
24+
rust_chat_id: i64,
2525
) -> Result<(), AppError> {
2626
let message_date = message.date.timestamp();
2727
let Some(curr_date) = DateTime::from_timestamp(message_date, 0) else {
@@ -48,13 +48,13 @@ pub async fn handle_rust_matched_mention(
4848
if let Ok(last_mention_time) =
4949
mention_repository::lead_earliest_mention_time(&db_pool, chat_id.0)
5050
.await
51-
.map_err(|err| error!("Can't insert mention: {:?}", err))
51+
.inspect_err(|err| warn!("Can't fetch latest mention time: {err:?}"))
5252
{
5353
let last_update_time = Utc.from_utc_datetime(&last_mention_time);
5454
info!("latest update time: {}", last_update_time);
5555

5656
let time_diff = curr_date.signed_duration_since(last_update_time);
57-
if time_diff > req_time_diff && chat_id.0 != RUST_CHAT {
57+
if time_diff > req_time_diff && chat_id.0 != rust_chat_id {
5858
let message_ids = (message.id, chat_id, message.thread_id);
5959
send_rust_mention_response(bot, message_ids, time_diff, &username).await;
6060
}
@@ -68,7 +68,7 @@ pub async fn handle_rust_matched_mention(
6868
.map_or_else(|| chat_id.0, |id| id.0 .0 as i64),
6969
)
7070
.await
71-
.map_err(|err| error!("Can't insert mention: {:?}", err))
71+
.inspect_err(|err| warn!("Can't insert mention: {err:?}"))
7272
.ok();
7373
}
7474
}
@@ -99,27 +99,27 @@ async fn send_rust_mention_response(
9999
reply_msg
100100
.message_thread_id(thread_id)
101101
.await
102-
.map_err(|err| error!("Can't send reply: {:?}", err))
102+
.inspect_err(|err| warn!("Can't send reply: {err:?}"))
103103
.ok();
104104
bot.send_sticker(
105105
message_ids.1,
106106
InputFile::file_id(fetch_sticker_id(time_diff)),
107107
)
108108
.message_thread_id(thread_id)
109109
.await
110-
.map_err(|err| error!("Can't send a sticker: {:?}", err))
110+
.inspect_err(|err| warn!("Can't send a sticker: {err:?}"))
111111
.ok();
112112
} else {
113113
reply_msg
114114
.await
115-
.map_err(|err| error!("Can't send reply: {:?}", err))
115+
.inspect_err(|err| warn!("Can't send reply: {err:?}"))
116116
.ok();
117117
bot.send_sticker(
118118
message_ids.1,
119119
InputFile::file_id(fetch_sticker_id(time_diff)),
120120
)
121121
.await
122-
.map_err(|err| error!("Can't send a sticker: {:?}", err))
122+
.inspect_err(|err| warn!("Can't send a sticker: {err:?}"))
123123
.ok();
124124
}
125125
}

src/url_summary_handler.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::gpt_service::ChatMessage;
22
use crate::gpt_service::ChatMessageRole::{System, User};
33
use crate::{gpt_service, AppError, GptParameters};
4-
use log::{error, info};
4+
use log::{info, warn};
55
use regex::Regex;
66
use reqwest::Client;
77
use std::time::Duration;
@@ -59,12 +59,12 @@ pub async fn handle_url_summary(
5959
reply_msg
6060
.message_thread_id(thread_id)
6161
.await
62-
.map_err(|err| error!("Can't send reply: {:?}", err))
62+
.inspect_err(|err| warn!("Can't send reply: {err:?}"))
6363
.ok();
6464
} else {
6565
reply_msg
6666
.await
67-
.map_err(|err| error!("Can't send reply: {:?}", err))
67+
.inspect_err(|err| warn!("Can't send reply: {err:?}"))
6868
.ok();
6969
}
7070
Ok(())

0 commit comments

Comments
 (0)