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]]
name = "arcade-monitor"
version = "0.1.0"
version = "0.1.1"
dependencies = [
"console_error_panic_hook",
"getrandom",

View file

@ -1,6 +1,6 @@
[package]
name = "arcade-monitor"
version = "0.1.0"
version = "0.1.1"
edition = "2021"
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> {
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<String> {
// 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<String> {

View file

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