mirror of
https://github.com/SkyfallWasTaken/arcade-monitor.git
synced 2024-11-22 00:43:39 +00:00
Send to Slack webhook
This commit is contained in:
parent
984c9c2d49
commit
eb251ae927
1 changed files with 25 additions and 0 deletions
25
src/lib.rs
25
src/lib.rs
|
@ -1,3 +1,5 @@
|
||||||
|
use indoc::formatdoc;
|
||||||
|
use serde_json::json;
|
||||||
use worker::*;
|
use worker::*;
|
||||||
|
|
||||||
mod format;
|
mod format;
|
||||||
|
@ -21,6 +23,7 @@ pub async fn main(req: Request, env: Env, _ctx: worker::Context) -> Result<Respo
|
||||||
|
|
||||||
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 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?;
|
||||||
|
@ -48,6 +51,28 @@ async fn run_scrape(env: Env) -> Result<String> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If there are any updates/new items, send a message to the Slack webhook.
|
||||||
|
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(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?;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue