From 39026e7a31a7cb7f6aff6de4a4afdd2410e8b6d0 Mon Sep 17 00:00:00 2001 From: SkyfallWasTaken Date: Sun, 7 Jul 2024 09:07:50 +0100 Subject: [PATCH] Fix Slack webhook --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/lib.rs | 38 ++++++++++++++++++-------------------- wrangler.toml | 2 +- 4 files changed, 21 insertions(+), 23 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a4b2739..fbf4e66 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -32,7 +32,7 @@ dependencies = [ [[package]] name = "arcade-monitor" -version = "0.1.0" +version = "0.1.1" dependencies = [ "console_error_panic_hook", "getrandom", diff --git a/Cargo.toml b/Cargo.toml index 1cc21d0..7577454 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "arcade-monitor" -version = "0.1.0" +version = "0.1.1" edition = "2021" authors = ["SkyfallWasTaken "] diff --git a/src/lib.rs b/src/lib.rs index 5b63e3e..efc5128 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -31,7 +31,7 @@ pub async fn scheduled(event: ScheduledEvent, env: Env, _ctx: ScheduleContext) { async fn run_scrape(env: Env) -> Result { let shop_url = Url::parse(&env.var("ARCADE_SHOP_URL")?.to_string())?; - let webhook_url = env.var("SLACK_WEBHOOK_URL")?.to_string(); + let webhook_url = env.secret("SLACK_WEBHOOK_URL")?.to_string(); let kv = env.kv("SHOP_ITEMS")?; let available_items = items::try_fetch(shop_url).await?; @@ -44,32 +44,30 @@ async fn run_scrape(env: Env) -> Result { // Compare the old items with the new items. let result = diff_old_new_items(&old_items, &available_items); - // If there are any updates/new items, send a message to the Slack webhook. + // Check if there are any updates. if result.is_empty() { return Ok("No changes detected".into()); - } else { - let message = formatdoc! { - "*Changes detected in the shop:* - {changes}", - changes = result.join("\n\n"), - }; - - let request = Request::new_with_init( - &webhook_url, - RequestInit::new() - .with_body(Some(serde_wasm_bindgen::to_value( - &json!({ "text": message }), - )?)) - .with_method(Method::Post), - )?; - - Fetch::Request(request).send().await?; } + // If there are any updates/new items, send a message to the Slack webhook. + let message = formatdoc! { + "*Changes detected in the shop:* + {changes}", + changes = result.join("\n\n"), + }; + let request = Request::new_with_init( + &webhook_url, + RequestInit::new() + .with_body(Some(serde_wasm_bindgen::to_value( + &json!({ "text": message }), + )?)) + .with_method(Method::Post), + )?; + Fetch::Request(request).send().await?; // Now, let's persist the items to the KV store. kv.put("items", &available_items)?.execute().await?; - Ok(result.join("\n\n")) + Ok(message) } fn diff_old_new_items(old_items: &ShopItems, new_items: &ShopItems) -> Vec { diff --git a/wrangler.toml b/wrangler.toml index 44d3fe1..61e4402 100644 --- a/wrangler.toml +++ b/wrangler.toml @@ -13,4 +13,4 @@ ARCADE_SHOP_URL = "https://hackclub.com/arcade/shop/" [[kv_namespaces]] binding = "SHOP_ITEMS" -id = "5307e6b7af8b4d86ac6ad26a593ca4d2" +id = "076e9724967c49619e34d180ebfb5e6a"