async_wechat/official_account/
menu.rs

1use crate::OfficialAccount;
2
3use super::core::BasicResponse;
4
5pub(crate) const DELETE_MENU_URL: &str = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=";
6
7#[cfg(test)]
8mod tests {
9    use std::env;
10
11    use crate::{Config, OfficialAccount};
12
13    #[tokio::test]
14    async fn delete_menu() {
15        dotenv::dotenv().ok();
16
17        let appid = env::var("APPID").expect("APPID not set");
18        let app_secret = env::var("APP_SECRET").expect("APP_SECRET not set");
19        let redis_url = env::var("REDIS_URL").expect("REDIS_URL not set");
20
21        let config = Config {
22            appid: appid.clone(),
23            app_secret: app_secret.clone(),
24            token: "wechat".to_string(),
25            encoding_aes_key: None,
26        };
27        let account = OfficialAccount::new(config, redis_url);
28        let result = account.delete_menu().await;
29        println!("url: {:#?}", result);
30    }
31}
32
33impl OfficialAccount {
34    /// [Deletes all custom menus for the official account](https://developers.weixin.qq.com/doc/offiaccount/Custom_Menus/Deleting_Custom-Defined_Menu.html)
35    ///
36    /// # Returns
37    ///
38    /// * A `Result` containing a `String` with the value `"ok"` on success, or a boxed
39    ///   error on failure.
40    ///
41    /// # Errors
42    ///
43    /// * Returns an error if the HTTP request fails or returns a non-success status,
44    ///   or if the response cannot be deserialized into a `BasicResponse`.
45    pub async fn delete_menu(&self) -> Result<String, Box<dyn std::error::Error>> {
46        let token = self.token().await?;
47
48        let url = format!("{}{}", DELETE_MENU_URL, token);
49        let response = self.client.post(url).send().await?;
50        if let Err(err) = response.json::<BasicResponse>().await {
51            println!("json err: {:#?}", err);
52            return Err(err.into());
53        }
54
55        Ok("ok".to_string())
56    }
57}