use grammers_client::session::Session;
use grammers_client::{Client, Config, InitParams, ReconnectionPolicy};
use std::ops::ControlFlow;
use std::time::Duration;
use tokio::runtime;
type Result = std::result::Result<(), Box<dyn std::error::Error>>;
struct MyPolicy;
impl ReconnectionPolicy for MyPolicy {
fn should_retry(&self, attempts: usize) -> ControlFlow<(), Duration> {
let duration = u64::pow(2, attempts as _);
ControlFlow::Continue(Duration::from_millis(duration))
}
}
async fn async_main() -> Result {
println!("Connecting to Telegram...");
let client = Client::connect(Config {
session: Session::load_file_or_create("ping.session")?,
api_id: 1, api_hash: "".to_string(),
params: InitParams {
reconnection_policy: &MyPolicy,
..Default::default()
},
})
.await?;
use grammers_client::Update;
loop {
let update = client.next_update().await?;
match update {
Update::NewMessage(message) if !message.outgoing() => {
message.respond(message.text()).await?;
}
_ => {}
}
}
}
fn main() -> Result {
runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap()
.block_on(async_main())
}