move to frankenstein for telegram bot api

This commit is contained in:
ace 2023-06-23 21:41:02 +03:00
parent 69689a6238
commit 59fa653889
Signed by: ace
GPG Key ID: 2C08973DD37A76FD
3 changed files with 598 additions and 624 deletions

1190
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -1,17 +1,17 @@
[package]
name = "telegram-notifier"
version = "0.1.0"
version = "0.1.1"
authors = ["ace <ace@0xace.cc>"]
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
frankenstein = { version = "0.26", default-features = false, features = ["async-http-client"] }
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"

View File

@ -1,11 +1,13 @@
use serde_yaml;
use serde::{Deserialize, Serialize};
use telegram_bot::*;
use clap::Parser;
use tokio;
use log::*;
use chrono::prelude::*;
use frankenstein;
use frankenstein::AsyncApi;
use frankenstein::AsyncTelegramApi;
use frankenstein::SendMessageParams;
#[derive(Debug, PartialEq, Serialize, Deserialize)]
struct Config {
@ -30,15 +32,16 @@ struct Opts {
msg: String,
/// Verbose level, accept multiple occurrences
#[clap(short, long, action = clap::ArgAction::Count)]
verbose: usize,
verbose: u8,
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let opts: Opts = Opts::parse();
stderrlog::new()
.module(module_path!())
.verbosity(opts.verbose)
.verbosity(usize::from(opts.verbose))
.timestamp(stderrlog::Timestamp::Second)
.quiet(false)
.init()
@ -71,9 +74,18 @@ async fn telegram_notify(opts: &Opts, config: &Config) -> Result<(), Box<dyn std
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?;
let api = AsyncApi::new(&config.token);
let str = Local::now().format("%Y-%m-%d %H:%M:%S: ").to_string() + &opts.msg.to_string();
let send_message_params: SendMessageParams = SendMessageParams::builder()
.chat_id(config.chatid)
.text(str)
.build();
if let Err(err) = api.send_message(&send_message_params).await {
println!("Failed to send message: {err:?}");
}
Ok(())
}