Skip to content

Commit

Permalink
feat(alert): log failed alert error
Browse files Browse the repository at this point in the history
If we fail to send the message to Telegram log the reason why from their
response.
  • Loading branch information
alextes committed Oct 9, 2023
1 parent 3c935b5 commit 76cdf03
Showing 1 changed file with 23 additions and 9 deletions.
32 changes: 23 additions & 9 deletions src/phoenix/alert.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use anyhow::Result;
use anyhow::{anyhow, bail, Result};

Check warning on line 1 in src/phoenix/alert.rs

View workflow job for this annotation

GitHub Actions / Check

unused import: `bail`

Check warning on line 1 in src/phoenix/alert.rs

View workflow job for this annotation

GitHub Actions / Test Suite

unused import: `bail`

Check failure on line 1 in src/phoenix/alert.rs

View workflow job for this annotation

GitHub Actions / Lints

unused import: `bail`
use axum::http::{HeaderMap, HeaderValue};
use reqwest::StatusCode;
use serde::Deserialize;
use serde_json::json;
use tracing::debug;

use crate::{env::Env, phoenix::env::APP_CONFIG};

Expand All @@ -10,19 +12,31 @@ pub async fn send_telegram_alert(message: &str) -> Result<()> {
"https://api.telegram.org/bot{}/sendMessage",
&APP_CONFIG.telegram_api_key
);
reqwest::Client::new()
let response = reqwest::Client::new()
.get(&url)
.query(&[
("chat_id", &APP_CONFIG.telegram_channel_id),
("text", &message.to_string()),
("parse_mode", &"MarkdownV2".to_string()),
("disable_web_page_preview", &"true".to_string()),
("chat_id", APP_CONFIG.telegram_channel_id.as_str()),
("text", message),
("parse_mode", "MarkdownV2"),
("disable_web_page_preview", "true"),
])
.send()
.await?
.error_for_status()?;
.await?;

Ok(())
match response.status() {
StatusCode::OK => {
debug!("sent telegram alert: {}", message);
Ok(())
}
StatusCode::BAD_REQUEST => {
let body = response.text().await?;
Err(anyhow!("failed to send telegram alert: {}", body))
}
_ => Err(anyhow!(
"failed to send telegram alert, status: {:?}",
response.status()
)),
}
}

#[derive(Deserialize)]
Expand Down

0 comments on commit 76cdf03

Please sign in to comment.