summaryrefslogtreecommitdiffstats
path: root/src/main.rs
blob: 0a6b256f0d196d78a4a94ab57d6bf06ab00cedf2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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() {
    dotenv::dotenv().ok();
    env_logger::init();
    let tg_log: Option<i32> = env::var("TG_LOG")
        .ok()
        .and_then(|var| var.parse().ok());

    let mut updater = update::UpdateRouter::new(
        tokio::runtime::Handle::try_current().expect("Must be in runtime")
    );

    let update_auth_state = |tg: client::Client, val: serde_json::Value| async move {
        let cache = env::current_dir().unwrap().join("cache");
        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);
            },
            "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);

    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));
}