summaryrefslogtreecommitdiffstats
path: root/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs138
1 files changed, 106 insertions, 32 deletions
diff --git a/src/main.rs b/src/main.rs
index 289ad02..0a6b256 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,10 +1,21 @@
use std::env;
use tokio;
+use log::{ info, error };
mod client;
//mod auth;
mod update;
//mod message;
+/*
+struct UpdateHandler;
+
+impl update::Handler for UpdateHandler {
+ fn handle(&self, _client: client::Client, req: serde_json::Value) -> futures::future::BoxFuture<'static, ()> {
+ Box::pin(async move {
+ info!("update: {:#}", req)
+ })
+ }
+}*/
#[tokio::main]
async fn main() {
@@ -14,41 +25,104 @@ async fn main() {
.ok()
.and_then(|var| var.parse().ok());
- let mut tg = client::Client::new(tg_log);
+ let mut updater = update::UpdateRouter::new(
+ tokio::runtime::Handle::try_current().expect("Must be in runtime")
+ );
- tokio::spawn(async move {
- let _auth_state = tg.send(
- &json::object!{
- "@type": "getAuthorizationState"
- }
- ).await;
+ let update_auth_state = |tg: client::Client, val: serde_json::Value| async move {
let cache = env::current_dir().unwrap().join("cache");
- let resp = tg.send(&json::object!{
- "@type": "setTdlibParameters",
- "parameters": {
- "use_test_dc": false,
- "api_id": env::var("API_ID").unwrap(),
- "api_hash": env::var("API_HASH").unwrap(),
- "device_model": "mbia",
- "system_version": "Catalina",
- "application_version": "0.1",
- "system_language_code": "en",
- "database_directory": cache.join("database").to_str().unwrap(),
- "use_message_database": false,
- "files_directory": cache.join("files").to_str().unwrap(),
- "use_secret_chats": false,
+ info!("auth update: val: {:#}", val);
+ match val.pointer("/authorization_state/@type").unwrap()
+ .as_str().unwrap() {
+ "authorizationStateWaitTdlibParameters" => {
+ let resp = tg.send(&serde_json::json!({
+ "@type": "setTdlibParameters",
+ "parameters": {
+ "use_test_dc": true,
+ "api_id": env::var("API_ID").unwrap(),
+ "api_hash": env::var("API_HASH").unwrap(),
+ "device_model": "mbia",
+ "system_version": "Catalina",
+ "application_version": "0.1",
+ "system_language_code": "en",
+ "database_directory": cache.join("database").to_str().unwrap(),
+ "use_message_database": false,
+ "files_directory": cache.join("files").to_str().unwrap(),
+ "use_secret_chats": false,
+ },
+ })).await;
+ info!("settdlib: {}", resp);
},
- }).await;
- println!("resp: {}", resp);
- });
- std::thread::sleep(std::time::Duration::new(1, 0));
-}
+ "authorizationStateWaitEncryptionKey" => {
+ let resp = tg.send(&serde_json::json!({
+ "@type": "setDatabaseEncryptionKey",
+ "encryption_key": "sup3rs3cr3t",
+ })).await;
+ info!("setenckey: {}", resp);
+ },
+ "authorizationStateWaitPhoneNumber" => {
+ let resp = tg.send(&serde_json::json!({
+ "@type": "setAuthenticationPhoneNumber",
+ "phone_number": "+79859053875",
+ "settings": {
+ "allow_flash_call": false,
+ "is_current_phone_number": false,
+ "allow_sms_retriever_api": false,
+ }
+ })).await;
+ info!("setphone: {}", resp);
+ },
+ "authorizationStateWaitRegistration" => {
+ let resp = tg.send(&serde_json::json!({
+ "@type": "registerUser",
+ "first_name": "John",
+ "last_name": "Doe",
+ })).await;
+ info!("reg: {}", resp);
+ },
+ "authorizationStateWaitCode" => {
+ let code = {
+ let mut s = String::new();
+ std::io::stdin().read_line(&mut s).expect("Could not read line");
+ s
+ };
+ let resp = tg.send(&serde_json::json!({
+ "@type": "checkAuthenticationCode",
+ "code": code,
+ })).await;
+ info!("checkcode: {}", resp);
+ },
+ auth_state => {
+ error!("Unknown auth state update: {} / {:#}", auth_state, val);
+ }
+ };
+ };
+ updater.add_handler("updateAuthorizationState", update_auth_state);
-/*
-fn main() {
- runtime::Runtime::new(|arc_msg, tx| {
- tx.send(std::sync::Arc::new(runtime::Task{})).unwrap();
- }).run();
+ let tg = client::Client::new(tg_log, updater);
+ std::thread::sleep(std::time::Duration::new(2, 0));
+ let get_chats = tg.send(&serde_json::json!({
+ "@type": "getChats",
+ "chat_list": {
+ "@type": "chatListMain"
+ },
+ "offset_order": "9223372036854775807",
+ "offset_chat_id": "9223372036854775807",
+ "limit": 5
+ })).await;
+ error!("send msg: {}", get_chats);
+ let chat = get_chats.pointer("/chat_ids/0").unwrap_or(&serde_json::json!(0)).as_u64().unwrap();
+ let get_chat = tg.send(&serde_json::json!({
+ "@type": "getChat",
+ "chat_id": chat,
+ })).await;
+ error!("get_chat: {:#}", get_chat);
+ let me = tg.send(&serde_json::json!({
+ "@type": "getMe",
+ })).await;
+ error!("me: {:#}", me);
+
+
+ std::thread::sleep(std::time::Duration::new(200, 0));
}
-*/