Skip to content

Commit

Permalink
allow reading env variable from file
Browse files Browse the repository at this point in the history
handy for docker secrets
also run cargo fmt
  • Loading branch information
Napuu committed Aug 13, 2023
1 parent 681466b commit 0bb5872
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 14 deletions.
11 changes: 3 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use json::JsonValue;
use log::{debug, error, info};
use regex::Regex;
use std::env;
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::oneshot;
use tokio::sync::Semaphore;
use tokio::time;
use log::{debug, error, info};

mod telegram_client;
use telegram_client::*;
Expand Down Expand Up @@ -103,7 +103,7 @@ async fn handle_update(update: &JsonValue) {
if maybe_chat_id.is_none() {
// No idea what causes this but doesn't seem to hurt regular usage
debug!("Encountered update with no message.chat.id object");
return
return;
}
let chat_id = maybe_chat_id.unwrap();
let reply_to_message_id = message["message"]["reply_to_message"]["message_id"].as_i64();
Expand Down Expand Up @@ -206,12 +206,7 @@ async fn slow_poll(token: &str) -> ! {
async fn main() {
env_logger::init();

let maybe_token = env::var("TOKEN");
if maybe_token.is_err() {
error!("Environment variable 'TOKEN' not found");
std::process::exit(1);
}
let token = maybe_token.unwrap();
let token = get_config_value("TELEGRAM_TOKEN").expect("No TELEGRAM_TOKEN found");

info!("Bot running!");
slow_poll(&token).await;
Expand Down
8 changes: 6 additions & 2 deletions src/telegram_client.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use anyhow::Result;
use json::JsonValue;
use log::{debug, error};
use reqwest::{multipart, Body};
use serde::Serialize;
use tokio::fs::File;
use tokio_util::codec::{BytesCodec, FramedRead};
use log::{debug, error};

#[derive(Serialize)]
pub struct DeleteMessage<'a> {
Expand Down Expand Up @@ -68,7 +68,11 @@ where
let response = client.post(api_endpoint).json(payload).send().await?;

if response.status() != reqwest::StatusCode::OK {
error!("Telegram API request {} failed with status code {:?}", method, response.status());
error!(
"Telegram API request {} failed with status code {:?}",
method,
response.status()
);
}

let body = response.text().await?;
Expand Down
23 changes: 19 additions & 4 deletions src/util.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use log::debug;
use std::env;
use std::fs;
use std::process::{Command, Stdio};
use uuid::Uuid;
use log::debug;

pub async fn download_video(url: String) -> Option<String> {
let video_id = Uuid::new_v4();
Expand All @@ -25,9 +26,11 @@ pub async fn download_video(url: String) -> Option<String> {
let output_path = std::fs::canonicalize(&file_path).unwrap();
return Some(output_path.to_string_lossy().to_string());
} else {
debug!("yt-dlp failed with\nstdout: {}\nstderr: {}",
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr));
debug!(
"yt-dlp failed with\nstdout: {}\nstderr: {}",
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr)
);
return None;
}
}
Expand Down Expand Up @@ -74,3 +77,15 @@ pub fn delete_file(file: &str) -> Result<(), std::io::Error> {
fs::remove_file(file)?;
Ok(())
}

pub fn get_config_value(key: &str) -> Option<String> {
if let Ok(env_value) = env::var(key) {
if let Ok(file_content) = fs::read_to_string(&env_value) {
Some(file_content)
} else {
Some(env_value)
}
} else {
None
}
}

0 comments on commit 0bb5872

Please sign in to comment.