feat: set bot commands

This commit is contained in:
DarkCat09 2024-11-27 18:28:34 +04:00
parent c2735ccf25
commit e53405a060
Signed by: DarkCat09
GPG key ID: BD3CE9B65916CD82

View file

@ -19,8 +19,14 @@ const POLL_TYPES: &str = r#"["message"]"#;
async fn main() -> std::io::Result<()> { async fn main() -> std::io::Result<()> {
let cfg = { let cfg = {
let token = std::env::var("BOT_TOKEN").expect("invalid BOT_TOKEN env var value"); let token = std::env::var("BOT_TOKEN").expect("invalid BOT_TOKEN env var value");
let client = Client::new();
init_cmds(&client, &token)
.await
.expect("could not make request to API");
Config { Config {
client: Client::new(), client,
upd_url: Url::parse(&format!("{BOT_URL}{token}/getUpdates")) upd_url: Url::parse(&format!("{BOT_URL}{token}/getUpdates"))
.unwrap() .unwrap()
.into(), .into(),
@ -57,6 +63,26 @@ async fn main() -> std::io::Result<()> {
Ok(()) Ok(())
} }
async fn init_cmds(client: &Client, token: &str) -> Result<(), reqwest::Error> {
client
.get(format!("{BOT_URL}{token}/setMyCommands"))
.query(&[(
"commands",
serde_json::json!([
{
"command": COMMAND[1..], // cmd name without leading slash
"description": "Get XKCD comic by id",
}
])
.to_string(),
)])
.send()
.await?
.error_for_status()?;
Ok(())
}
async fn handler(cfg: &Config, s: &mut State) -> Result<(), reqwest::Error> { async fn handler(cfg: &Config, s: &mut State) -> Result<(), reqwest::Error> {
let mut updates = cfg let mut updates = cfg
.client .client