first commit

This commit is contained in:
ace 2023-03-05 11:28:28 +03:00
commit ceddfb90e9
Signed by: ace
GPG Key ID: 2C08973DD37A76FD
5 changed files with 1634 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
/target
.telegram-notifier.yaml

1520
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

18
Cargo.toml Normal file
View File

@ -0,0 +1,18 @@
[package]
name = "telegram-notifier"
version = "0.1.0"
authors = ["ace <ace@0xace.cc>"]
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
serde = { version = "1.0", features = ["derive"] }
serde_yaml = "0.9"
tokio = { version = "1", features = ["full"] }
clap = { version = "4", features = ["derive"] }
telegram-bot = { git = "https://github.com/telegram-rs/telegram-bot", rev = "65ad5cfd578e9a1260ce6daac714eb2153c0bec7" }
log = "0.4"
stderrlog = "0.5"
rand = "0.8"
chrono ="0.4"

15
README.md Normal file
View File

@ -0,0 +1,15 @@
# Telegram notifier
## Options
- token - required, Telegram token
- chatid - required, Telegram chat id
- config - file, default config file name ".telegram-notifier.yaml"
## Command line example:
telegram-notifier --token XXXX --chatid YYYY --msg "My message"
## Config .telegram-notifier.yaml example:
token: XXXX
chatid: YYYY

79
src/main.rs Normal file
View File

@ -0,0 +1,79 @@
use serde_yaml;
use serde::{Deserialize, Serialize};
use telegram_bot::*;
use clap::Parser;
use tokio;
use log::*;
use chrono::prelude::*;
#[derive(Debug, PartialEq, Serialize, Deserialize)]
struct Config {
token: String,
chatid: i64,
}
#[derive(Parser, Debug)]
#[clap(author, version, about, long_about = None)]
struct Opts {
/// Config fle
#[clap(long)]
config: Option<String>,
/// Telegram token
#[clap(long)]
token: Option<String>,
/// Telegram chat ID
#[clap(long, allow_hyphen_values(true))]
chatid: Option<i64>,
/// Message to send
#[clap(long)]
msg: String,
/// Verbose level, accept multiple occurrences
#[clap(short, long, action = clap::ArgAction::Count)]
verbose: usize,
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let opts: Opts = Opts::parse();
stderrlog::new()
.module(module_path!())
.verbosity(opts.verbose)
.timestamp(stderrlog::Timestamp::Second)
.quiet(false)
.init()
.unwrap();
if let (Some(t),Some(c)) = (opts.token.clone(), opts.chatid.clone()) {
let config = Config {
token: t,
chatid: c,
};
let _ = telegram_notify(&opts, &config).await?;
} else if let Some(_config) = opts.config.as_ref() {
println!("{} - LOG - Config file name: {}", Local::now().trunc_subsecs(0).to_rfc3339(), &opts.config.as_ref().unwrap());
let f = std::fs::File::open(&opts.config.as_ref().unwrap())?;
let config: Config = serde_yaml::from_reader(f)?;
let _ = telegram_notify(&opts, &config).await?;
} else {
println!("{} - LOG - Config file name: {}", Local::now().trunc_subsecs(0).to_rfc3339(), ".telegram-notifier.yaml");
let f = std::fs::File::open(".telegram-notifier.yaml").expect("Can't openf config file");
let config: Config = serde_yaml::from_reader(f)?;
let _ = telegram_notify(&opts, &config).await?;
}
Ok(())
}
async fn telegram_notify(opts: &Opts, config: &Config) -> Result<(), Box<dyn std::error::Error>> {
debug!("Using token: {}", config.token);
println!("{} - LOG - Using chat id: {}", Local::now().trunc_subsecs(0).to_rfc3339(), config.chatid);
println!("{} - LOG - {}", Local::now().trunc_subsecs(0).to_rfc3339(), opts.msg);
let api = Api::new(&config.token);
let chat = ChatId::new(config.chatid);
let msg = Local::now().format("%Y-%m-%d %H:%M:%S: ").to_string() + &opts.msg.to_string();
let _ = api.send(chat.text(msg)).await?;
Ok(())
}