mirror of
https://github.com/SkyfallWasTaken/arcade-monitor.git
synced 2024-11-22 08:53:40 +00:00
Fix Slack webhook
This commit is contained in:
parent
9abdff79f5
commit
39026e7a31
4 changed files with 21 additions and 23 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -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",
|
||||||
|
|
|
@ -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>"]
|
||||||
|
|
||||||
|
|
38
src/lib.rs
38
src/lib.rs
|
@ -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,32 +44,30 @@ 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 {
|
|
||||||
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.
|
// 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> {
|
||||||
|
|
|
@ -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"
|
||||||
|
|
Loading…
Reference in a new issue