Fix Slack webhook

This commit is contained in:
SkyfallWasTaken 2024-07-07 09:07:50 +01:00
parent 9abdff79f5
commit 39026e7a31
4 changed files with 21 additions and 23 deletions

2
Cargo.lock generated
View file

@ -32,7 +32,7 @@ dependencies = [
[[package]] [[package]]
name = "arcade-monitor" name = "arcade-monitor"
version = "0.1.0" version = "0.1.1"
dependencies = [ dependencies = [
"console_error_panic_hook", "console_error_panic_hook",
"getrandom", "getrandom",

View file

@ -1,6 +1,6 @@
[package] [package]
name = "arcade-monitor" name = "arcade-monitor"
version = "0.1.0" version = "0.1.1"
edition = "2021" edition = "2021"
authors = ["SkyfallWasTaken <mahadkalam1@proton.me>"] authors = ["SkyfallWasTaken <mahadkalam1@proton.me>"]

View file

@ -31,7 +31,7 @@ pub async fn scheduled(event: ScheduledEvent, env: Env, _ctx: ScheduleContext) {
async fn run_scrape(env: Env) -> Result<String> { async fn run_scrape(env: Env) -> Result<String> {
let shop_url = Url::parse(&env.var("ARCADE_SHOP_URL")?.to_string())?; 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 kv = env.kv("SHOP_ITEMS")?;
let available_items = items::try_fetch(shop_url).await?; let available_items = items::try_fetch(shop_url).await?;
@ -44,16 +44,16 @@ async fn run_scrape(env: Env) -> Result<String> {
// Compare the old items with the new items. // Compare the old items with the new items.
let result = diff_old_new_items(&old_items, &available_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() { if result.is_empty() {
return Ok("No changes detected".into()); return Ok("No changes detected".into());
} else { }
// If there are any updates/new items, send a message to the Slack webhook.
let message = formatdoc! { let message = formatdoc! {
"*Changes detected in the shop:* "*Changes detected in the shop:*
{changes}", {changes}",
changes = result.join("\n\n"), changes = result.join("\n\n"),
}; };
let request = Request::new_with_init( let request = Request::new_with_init(
&webhook_url, &webhook_url,
RequestInit::new() RequestInit::new()
@ -62,14 +62,12 @@ async fn run_scrape(env: Env) -> Result<String> {
)?)) )?))
.with_method(Method::Post), .with_method(Method::Post),
)?; )?;
Fetch::Request(request).send().await?; Fetch::Request(request).send().await?;
}
// Now, let's persist the items to the KV store. // Now, let's persist the items to the KV store.
kv.put("items", &available_items)?.execute().await?; 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<String> { fn diff_old_new_items(old_items: &ShopItems, new_items: &ShopItems) -> Vec<String> {

View file

@ -13,4 +13,4 @@ ARCADE_SHOP_URL = "https://hackclub.com/arcade/shop/"
[[kv_namespaces]] [[kv_namespaces]]
binding = "SHOP_ITEMS" binding = "SHOP_ITEMS"
id = "5307e6b7af8b4d86ac6ad26a593ca4d2" id = "076e9724967c49619e34d180ebfb5e6a"