diff --git a/Cargo.lock b/Cargo.lock index 8be7f0b..e59fc01 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1971,7 +1971,7 @@ dependencies = [ [[package]] name = "wechat-cli" -version = "0.3.0" +version = "0.4.0" dependencies = [ "aes", "anyhow", diff --git a/Cargo.toml b/Cargo.toml index f54b289..85ff890 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "wechat-cli" -version = "0.3.0" +version = "0.4.0" edition = "2024" rust-version = "1.85" description = "A CLI tool to interact with a Wechat iLink bot." diff --git a/README.md b/README.md index d746a44..5d7df6c 100644 --- a/README.md +++ b/README.md @@ -80,10 +80,21 @@ wechat-cli account delete --account ### Get Context Token -Wait for the next incoming message and print the `context_token`: +Wait for the next incoming message and print the `context_token`. + +Using a saved account: + +```bash +wechat-cli get-context-token --account +``` + +Using explicit credentials: ```bash -wechat-cli get-context-token [--user-id ] +wechat-cli get-context-token \ + --bot-token \ + --user-id \ + [--route-tag ] ``` ### Send diff --git a/src/cli.rs b/src/cli.rs index 3887c9b..4c831ce 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -85,8 +85,7 @@ pub struct AccountDeleteArgs { --bot-token --user-id [--route-tag ] Usage Rules: - - Omit all auth params to use the first saved account. - - Or use exactly one of the modes above. + - You must use exactly one of the modes above. - --account cannot be combined with --bot-token or --user-id. - In Explicit mode, both --bot-token and --user-id are required.")] pub struct GetContextTokenArgs { diff --git a/src/commands/account.rs b/src/commands/account.rs index 66c5ec2..c23e742 100644 --- a/src/commands/account.rs +++ b/src/commands/account.rs @@ -1,22 +1,10 @@ -use anyhow::{Context, Result, anyhow, bail}; +use anyhow::{Result, bail}; use crate::{ storage::{self, AccountData, load_accounts}, wechat::api::WeixinApiClient, }; -pub fn resolve_user_id(explicit: Option<&str>) -> Result { - if let Some(user_id) = explicit { - return Ok(user_id.to_string()); - } - - let user_ids = storage::get_account_ids().context("failed to load saved users")?; - user_ids - .into_iter() - .next() - .ok_or_else(|| anyhow!("no saved user found, run `wechat-cli login` first")) -} - pub fn build_client(data: &AccountData) -> WeixinApiClient { WeixinApiClient::new(&data.bot_token, data.route_tag.clone()) } diff --git a/src/commands/get_context_token.rs b/src/commands/get_context_token.rs index d9d9b18..b223aae 100644 --- a/src/commands/get_context_token.rs +++ b/src/commands/get_context_token.rs @@ -1,9 +1,7 @@ -use anyhow::{Context, Result, bail}; +use anyhow::{Result, bail}; use crate::{ - commands::account::{build_client, resolve_user_id}, commands::send::{SendTarget, resolve_send_target}, - storage::{self}, wechat::api::is_session_expired, wechat::models::InboundMessage, }; @@ -14,17 +12,7 @@ pub async fn run( bot_token: Option<&str>, route_tag: Option<&str>, ) -> Result<()> { - let target = if account.is_none() && user_id.is_none() && bot_token.is_none() { - let resolved_id = resolve_user_id(None)?; - let session = storage::get_account_data(&resolved_id) - .with_context(|| format!("failed to load account data for `{resolved_id}`"))?; - SendTarget::Saved { - user_id: resolved_id, - client: build_client(&session), - } - } else { - resolve_send_target(account, user_id, bot_token, route_tag)? - }; + let target = resolve_send_target(account, user_id, bot_token, route_tag)?; let (user_id, client) = match target { SendTarget::Saved { user_id, client } => (user_id, client), diff --git a/src/storage.rs b/src/storage.rs index 7bfbe66..7b612c6 100644 --- a/src/storage.rs +++ b/src/storage.rs @@ -71,27 +71,6 @@ fn save_accounts_file(accounts: &AccountsFile) -> Result<()> { Ok(()) } -/// Returns the list of saved stable user IDs from local storage. -pub fn get_account_ids() -> Result> { - let accounts = load_accounts_file()?; - Ok(accounts - .accounts - .into_iter() - .map(|account| account.user_id) - .filter(|id| id.ends_with("@im.wechat")) - .collect()) -} - -/// Loads the saved credentials for the given stable user ID. -pub fn get_account_data(account_id: &str) -> Result { - let accounts = load_accounts_file()?; - accounts - .accounts - .into_iter() - .find(|account| account.user_id == account_id) - .ok_or_else(|| anyhow!("account `{account_id}` not found")) -} - /// Loads the saved credentials for the given account index. pub fn load_account(account_idx: usize) -> Result { let mut accounts = load_accounts_file()?;