Add Slack group pings

This commit is contained in:
SkyfallWasTaken 2024-07-23 09:55:48 +01:00
parent 9741cd7cd3
commit b9a3f6d078
3 changed files with 16 additions and 7 deletions

View file

@ -10,11 +10,10 @@
### Variables ### Variables
- `ARCADE_SHOP_URL` - Shop URL to fetch - `ARCADE_SHOP_URL` - Shop URL to fetch
- `NTFY_URL` - URL for ntfy - `NTFY_URL` - URL for ntfy
- `SLACK_GROUP_ID` - ID of the Slack group to ping
## Tech Stack ## Tech Stack
- **Cloudflare Workers** for running the monitor on the edge. - **Cloudflare Workers** for running the monitor on the edge.
- **Rust** for the monitor's code. I love its type safety, as well as libraries such as `serde`. - **Rust** for the monitor's code. I love its type safety, as well as libraries such as `serde`.
_Fun fact: the monitor was originally written in TypeScript, but it turned out to be so buggy I just rewrote it in Rust. The new version took less time to write, has tests (unlike the TypeScript version), and has better formatted messages._ _Fun fact: the monitor was originally written in TypeScript, but it turned out to be so buggy I just rewrote it in Rust. The new version took less time to write, has tests (unlike the TypeScript version), and has better formatted messages._
---

View file

@ -104,7 +104,7 @@ pub fn format_deleted_item(item: &ShopItem) -> String {
} }
} }
pub fn get_slack_body(diffs: &Vec<String>) -> serde_json::Value { pub fn get_slack_body(diffs: &Vec<String>, slack_group_id: String) -> serde_json::Value {
let mut blocks_vec = vec![]; let mut blocks_vec = vec![];
blocks_vec.push(json!({ blocks_vec.push(json!({
"type": "header", "type": "header",
@ -131,7 +131,10 @@ pub fn get_slack_body(diffs: &Vec<String>) -> serde_json::Value {
"elements": [ "elements": [
{ {
"type": "mrkdwn", "type": "mrkdwn",
"text": format!("Arcade Monitor v{}", env!("CARGO_PKG_VERSION").to_string()) // Will never panic, variable is always set by Cargo "text": format!(
"<!subteam^{slack_group_id}> Arcade Monitor v{version}",
version = env!("CARGO_PKG_VERSION").to_string()
) // Will never panic, variable is always set by Cargo
} }
] ]
})); }));
@ -147,9 +150,14 @@ mod slack_tests {
use pretty_assertions::assert_eq; use pretty_assertions::assert_eq;
use serde_json::json; use serde_json::json;
const SLACK_GROUP_ID: &str = "ABCDEFG";
#[test] #[test]
fn slack_body_is_correct() { fn slack_body_is_correct() {
let body = get_slack_body(&vec!["Test 1".into(), "Test 2".into(), "Test 3".into()]); let body = get_slack_body(
&vec!["Test 1".into(), "Test 2".into(), "Test 3".into()],
SLACK_GROUP_ID.into(),
);
assert_eq!( assert_eq!(
body, body,
json!({ json!({
@ -197,7 +205,7 @@ mod slack_tests {
"elements": [ "elements": [
{ {
"type": "mrkdwn", "type": "mrkdwn",
"text": format!("Arcade Monitor v{}", env!("CARGO_PKG_VERSION")) "text": format!("<!subteam^{SLACK_GROUP_ID}> Arcade Monitor v{}", env!("CARGO_PKG_VERSION"))
} }
] ]
} }

View file

@ -36,6 +36,8 @@ async fn run_scrape(env: Env) -> Result<String> {
let slack_webhook_url = env.secret("SLACK_WEBHOOK_URL")?.to_string(); let slack_webhook_url = env.secret("SLACK_WEBHOOK_URL")?.to_string();
let ntfy_url = env.var("NTFY_URL")?.to_string(); let ntfy_url = env.var("NTFY_URL")?.to_string();
let ntfy_auth_token = env.secret("NTFY_AUTH_TOKEN")?.to_string(); let ntfy_auth_token = env.secret("NTFY_AUTH_TOKEN")?.to_string();
let slack_group_id = env.var("SLACK_GROUP_ID")?.to_string();
let client = Client::new(); let client = Client::new();
let kv = env.kv("SHOP_ITEMS")?; let kv = env.kv("SHOP_ITEMS")?;
@ -60,7 +62,7 @@ async fn run_scrape(env: Env) -> Result<String> {
let changes = result.join("\n\n"); let changes = result.join("\n\n");
// slack webhook // slack webhook
let slack_body = format::get_slack_body(&result); let slack_body = format::get_slack_body(&result, slack_group_id);
client client
.post(&slack_webhook_url) .post(&slack_webhook_url)
.body(slack_body.to_string()) .body(slack_body.to_string())