-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Telegram error logs #115
Telegram error logs #115
Conversation
@coderabbitai review |
Actions performedReview triggered.
|
WalkthroughThe recent updates introduce Telegram integration into the bot, allowing it to send messages to a specified chat. This involves adding environment variables for Telegram configuration, updating message templates, incorporating the Changes
Sequence Diagram(s) (Beta)sequenceDiagram
participant Bot as Bot
participant TelegramSubscriber as TelegramSubscriber
participant TelegramAPI as Telegram API
Bot->>TelegramSubscriber: Initialize with bot_token and chat_id
TelegramSubscriber->>TelegramAPI: Send message on event
TelegramAPI-->>TelegramSubscriber: Acknowledge receipt
TelegramSubscriber-->>Bot: Log event sent confirmation
Poem
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (invoked as PR comments)
Additionally, you can add CodeRabbit Configration File (
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files ignored due to path filters (1)
Cargo.lock
is excluded by!**/*.lock
Files selected for processing (6)
- .env.example (1 hunks)
- Messages.toml (1 hunks)
- bot/Cargo.toml (1 hunks)
- bot/src/api/mod.rs (1 hunks)
- bot/src/api/telegram.rs (1 hunks)
- bot/src/main.rs (4 hunks)
Files skipped from review due to trivial changes (4)
- .env.example
- Messages.toml
- bot/Cargo.toml
- bot/src/api/mod.rs
Additional comments not posted (4)
bot/src/api/telegram.rs (2)
6-8
: TheTelegramSubscriber
struct is well-defined and appropriate for the intended functionality.
28-38
: The initialization function forTelegramSubscriber
is correctly implemented and effectively sets up the message sending task.bot/src/main.rs (2)
25-26
: The addition oftelegram_token
andtelegram_chat_id
to theEnv
struct is correctly implemented.
49-52
: The integration ofTelegramSubscriber
into the application's logging system is correctly implemented.
impl<S: Subscriber> tracing_subscriber::Layer<S> for TelegramSubscriber { | ||
fn on_event(&self, event: &Event<'_>, _ctx: tracing_subscriber::layer::Context<'_, S>) { | ||
let mut visitor = MessageVisitor::default(); | ||
event.record(&mut visitor); | ||
|
||
let message = format!("{}", visitor); | ||
|
||
let level = event.metadata().level(); | ||
if level <= &Level::WARN { | ||
self.send_to_telegram(&message, level); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider adding a comment explaining why only events with a level of WARN or higher are sent to Telegram.
+ // Send messages to Telegram only for warnings and more severe events
if level <= &Level::WARN {
self.send_to_telegram(&message, level);
}
Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.
impl<S: Subscriber> tracing_subscriber::Layer<S> for TelegramSubscriber { | |
fn on_event(&self, event: &Event<'_>, _ctx: tracing_subscriber::layer::Context<'_, S>) { | |
let mut visitor = MessageVisitor::default(); | |
event.record(&mut visitor); | |
let message = format!("{}", visitor); | |
let level = event.metadata().level(); | |
if level <= &Level::WARN { | |
self.send_to_telegram(&message, level); | |
} | |
} | |
} | |
impl<S: Subscriber> tracing_subscriber::Layer<S> for TelegramSubscriber { | |
fn on_event(&self, event: &Event<'_>, _ctx: tracing_subscriber::layer::Context<'_, S>) { | |
let mut visitor = MessageVisitor::default(); | |
event.record(&mut visitor); | |
let message = format!("{}", visitor); | |
let level = event.metadata().level(); | |
// Send messages to Telegram only for warnings and more severe events | |
if level <= &Level::WARN { | |
self.send_to_telegram(&message, level); | |
} | |
} | |
} |
async fn sender_task( | ||
mut reader: mpsc::UnboundedReceiver<(String, Level)>, | ||
client: Client, | ||
bot_token: String, | ||
chat_id: String, | ||
) { | ||
while let Some((message, level)) = reader.recv().await { | ||
let url = format!("https://api.telegram.org/bot{}/sendMessage", bot_token); | ||
let message = format!("*{}*: `{message}`", level.as_str()); | ||
let params = [ | ||
("chat_id", chat_id.as_str()), | ||
("text", &message), | ||
("parse_mode", "MarkdownV2"), | ||
]; | ||
let _ = client.post(&url).form(¶ms).send().await; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider enhancing error handling for the HTTP request to Telegram. Currently, failures are silently ignored.
- let _ = client.post(&url).form(¶ms).send().await;
+ match client.post(&url).form(¶ms).send().await {
+ Ok(response) if response.status().is_success() => {},
+ Ok(response) => eprintln!("Failed to send message: Received HTTP {}", response.status()),
+ Err(e) => eprintln!("Failed to send message: {}", e),
+ }
Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.
async fn sender_task( | |
mut reader: mpsc::UnboundedReceiver<(String, Level)>, | |
client: Client, | |
bot_token: String, | |
chat_id: String, | |
) { | |
while let Some((message, level)) = reader.recv().await { | |
let url = format!("https://api.telegram.org/bot{}/sendMessage", bot_token); | |
let message = format!("*{}*: `{message}`", level.as_str()); | |
let params = [ | |
("chat_id", chat_id.as_str()), | |
("text", &message), | |
("parse_mode", "MarkdownV2"), | |
]; | |
let _ = client.post(&url).form(¶ms).send().await; | |
} | |
} | |
async fn sender_task( | |
mut reader: mpsc::UnboundedReceiver<(String, Level)>, | |
client: Client, | |
bot_token: String, | |
chat_id: String, | |
) { | |
while let Some((message, level)) = reader.recv().await { | |
let url = format!("https://api.telegram.org/bot{}/sendMessage", bot_token); | |
let message = format!("*{}*: `{message}`", level.as_str()); | |
let params = [ | |
("chat_id", chat_id.as_str()), | |
("text", &message), | |
("parse_mode", "MarkdownV2"), | |
]; | |
match client.post(&url).form(¶ms).send().await { | |
Ok(response) if response.status().is_success() => {}, | |
Ok(response) => eprintln!("Failed to send message: Received HTTP {}", response.status()), | |
Err(e) => eprintln!("Failed to send message: {}", e), | |
} | |
} | |
} |
@race-of-sloths score 3 |
❌ Self-scoring is not permitted.Let's keep it fair! 👍 |
🔄 The PR has been merged.Important This pull request is a part of the Race of Sloths and has not been scored yet. Scoring will close in 24 hours! 🕰️ |
✅ PR is finalized!Your contribution is much appreciated with a final score of 0. Check out the updates in your profile! 🎊 |
@race-of-sloths
Summary by CodeRabbit
New Features
Improvements
Logging