Organise Slack code in format

This commit is contained in:
SkyfallWasTaken 2024-07-23 09:31:21 +01:00
parent 4516fcb337
commit d377973c32
2 changed files with 31 additions and 27 deletions

View file

@ -1,4 +1,5 @@
use indoc::formatdoc; use indoc::formatdoc;
use serde_json::json;
use crate::items::ShopItem; use crate::items::ShopItem;
@ -83,6 +84,35 @@ pub fn format_deleted_item(item: &ShopItem) -> String {
} }
} }
pub fn get_slack_blocks(diffs: &Vec<String>) -> serde_json::Value {
let mut blocks_vec = vec![];
for diff in diffs {
blocks_vec.push(json!({
"type": "section",
"text": {
"type": "mrkdwn",
"text": diff
}
}));
blocks_vec.push(json!({
"type": "divider"
}));
}
blocks_vec.push(json!({
"type": "context",
"elements": [
{
"type": "mrkdwn",
"text": format!("Arcade Monitor v{}", env!("CARGO_PKG_VERSION").to_string()) // Will never panic, variable is always set by Cargo
}
]
}));
json!({
"blocks": blocks_vec,
})
}
#[cfg(test)] #[cfg(test)]
mod format_new_tests { mod format_new_tests {
use super::*; use super::*;

View file

@ -1,6 +1,5 @@
use items::ShopItems; use items::ShopItems;
use reqwest::Client; use reqwest::Client;
use serde_json::json;
use worker::*; use worker::*;
mod format; mod format;
@ -61,32 +60,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 mut blocks_vec = vec![]; let slack_body = format::get_slack_blocks(&result);
for diff in &result {
blocks_vec.push(json!({
"type": "section",
"text": {
"type": "mrkdwn",
"text": diff
}
}));
blocks_vec.push(json!({
"type": "divider"
}));
}
blocks_vec.push(json!({
"type": "context",
"elements": [
{
"type": "mrkdwn",
"text": format!("Arcade Monitor v{}", env.var("CARGO_PKG_VERSION")?.to_string())
}
]
}));
let slack_body = &json!({
"blocks": blocks_vec,
});
client client
.post(&slack_webhook_url) .post(&slack_webhook_url)
.body(slack_body.to_string()) .body(slack_body.to_string())