telegram bot works now

This commit is contained in:
rustdesk 2024-06-27 17:36:50 +08:00
parent e79946b4e4
commit 9e851542ec

View File

@ -162,12 +162,13 @@ pub async fn send_2fa_code_to_telegram(text: &str, bot: TelegramBot) -> ResultTy
Ok(()) Ok(())
} }
#[tokio::main(flavor = "current_thread")] pub fn get_chatid_telegram(bot_token: &str) -> ResultType<Option<String>> {
pub async fn get_chatid_telegram(bot_token: &str) -> ResultType<Option<String>> {
let url = format!("https://api.telegram.org/bot{}/getUpdates", bot_token); let url = format!("https://api.telegram.org/bot{}/getUpdates", bot_token);
let resp = crate::post_request(url, "".to_owned(), "") // because caller is in tokio runtime, so we must call post_request_sync in new thread.
.await let handle = std::thread::spawn(move || {
.map_err(|e| anyhow!(e))?; crate::post_request_sync(url, "".to_owned(), "")
});
let resp = handle.join().map_err(|_| anyhow!("Thread panicked"))??;
let value = serde_json::from_str::<serde_json::Value>(&resp).map_err(|e| anyhow!(e))?; let value = serde_json::from_str::<serde_json::Value>(&resp).map_err(|e| anyhow!(e))?;
// Check for an error_code in the response // Check for an error_code in the response
@ -183,9 +184,14 @@ pub async fn get_chatid_telegram(bot_token: &str) -> ResultType<Option<String>>
)); ));
} }
let chat_id = value["result"][0]["message"]["chat"]["id"] let chat_id = &value["result"][0]["message"]["chat"]["id"];
.as_str() let chat_id = if let Some(id) = chat_id.as_i64() {
.map(|x| x.to_owned()); Some(id.to_string())
} else if let Some(id) = chat_id.as_str() {
Some(id.to_owned())
} else {
None
};
if let Some(chat_id) = chat_id.as_ref() { if let Some(chat_id) = chat_id.as_ref() {
let bot = TelegramBot { let bot = TelegramBot {