diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..3a48857 --- /dev/null +++ b/.env.example @@ -0,0 +1,2 @@ +SLACK_USER_TOKEN=longtokenhere +RPIALERT_URL=https://example.com/alert.js diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md new file mode 100644 index 0000000..f3d5c41 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/bug_report.md @@ -0,0 +1,38 @@ +--- +name: Bug report +about: Create a report to help us improve +title: '' +labels: bug +assignees: '' + +--- + +**Describe the bug** +A clear and concise description of what the bug is. + +**To Reproduce** +Steps to reproduce the behavior: +1. Go to '...' +2. Click on '....' +3. Scroll down to '....' +4. See error + +**Expected behavior** +A clear and concise description of what you expected to happen. + +**Screenshots** +If applicable, add screenshots to help explain your problem. + +**Desktop (please complete the following information):** + - OS: [e.g. iOS] + - Browser [e.g. chrome, safari] + - Version [e.g. 22] + +**Smartphone (please complete the following information):** + - Device: [e.g. iPhone6] + - OS: [e.g. iOS8.1] + - Browser [e.g. stock browser, safari] + - Version [e.g. 22] + +**Additional context** +Add any other context about the problem here. diff --git a/.github/ISSUE_TEMPLATE/feature_request.md b/.github/ISSUE_TEMPLATE/feature_request.md new file mode 100644 index 0000000..11fc491 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/feature_request.md @@ -0,0 +1,20 @@ +--- +name: Feature request +about: Suggest an idea for this project +title: '' +labels: enhancement +assignees: '' + +--- + +**Is your feature request related to a problem? Please describe.** +A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] + +**Describe the solution you'd like** +A clear and concise description of what you want to happen. + +**Describe alternatives you've considered** +A clear and concise description of any alternative solutions or features you've considered. + +**Additional context** +Add any other context or screenshots about the feature request here. diff --git a/.github/ISSUE_TEMPLATE/pending-task.md b/.github/ISSUE_TEMPLATE/pending-task.md new file mode 100644 index 0000000..ae2d822 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/pending-task.md @@ -0,0 +1,21 @@ +--- +name: Pending task +about: Log a task to be completed +title: '' +labels: task +assignees: '' + +--- + +**Describe the task** +A clear and concise description of what the task is. + +**Required steps** +Steps needed to complete this task: +1. ... +2. ... +3. ... +4. ... + +**Additional context** +Add any other context about the task here. diff --git a/.github/ISSUE_TEMPLATE/support-question.md b/.github/ISSUE_TEMPLATE/support-question.md new file mode 100644 index 0000000..a7269e2 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/support-question.md @@ -0,0 +1,35 @@ +--- +name: Support question +about: Ask a question or request assistance +title: '' +labels: question +assignees: '' + +--- + +**Describe the question/request** +A clear and concise description of what you need assistance with. + +**To Reproduce** +Steps to reproduce the behavior: +1. Go to '...' +2. Click on '....' +3. Scroll down to '....' +4. See error + +**Screenshots** +If applicable, add screenshots to help explain your problem. + +**Desktop (please complete the following information):** + - OS: [e.g. iOS] + - Browser [e.g. chrome, safari] + - Version [e.g. 22] + +**Smartphone (please complete the following information):** + - Device: [e.g. iPhone6] + - OS: [e.g. iOS8.1] + - Browser [e.g. stock browser, safari] + - Version [e.g. 22] + +**Additional context** +Add any other context about the problem here. diff --git a/package.json b/package.json index 10255cd..c849741 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "rpialert", - "version": "1.0.0", + "version": "1.1.0", "description": "A Slack integration to query RPI's alerting system and provide notifications on Slack", "main": "server.js", "scripts": { diff --git a/server.js b/server.js index ed0c79c..c958c03 100644 --- a/server.js +++ b/server.js @@ -1,41 +1,55 @@ //node packages const axios = require("axios"); +const crypto = require("crypto"); + require("dotenv").config(); //local packages const { app: { client: { + conversations: { history }, chat: { postMessage } } } } = require("./utilities/bolt.js"); //globals -const RPIALERT_URL = "https://alert.rpi.edu/alerts.js"; +const RPIALERT_URL = + process.env.RPIALERT_URL || "https://alert.rpi.edu/alerts.js"; +const ALERTS_CHANNEL = process.env.ALERTS_CHANNEL; const USER_TOKEN = process.env.SLACK_USER_TOKEN; //package config -let text = ""; -let oldtext = ""; +let oldHash; const parseAlertData = body => { - return body.substring( - body.indexOf("alert_content = ") + 17, - body.indexOf("alert_default = ") - 3 - ); + return body + .substring( + body.indexOf("alert_content = ") + 17, + body.indexOf("alert_default = ") - 3 + ) + .trim(); }; const rpialert = async () => { const { data } = await axios.get(RPIALERT_URL); - text = parseAlertData(data); + let text = parseAlertData(data); + if (text === "") { + return; + } + + let hash = crypto + .createHash("md5") + .update(text) + .digest("hex"); - if (text != oldtext && text != "") { + if (hash !== oldHash) { postMessage({ token: USER_TOKEN, - channel: "alerts", + channel: ALERTS_CHANNEL, unfurl_links: false, text: "RPI ALERT - ", blocks: [ @@ -58,15 +72,51 @@ const rpialert = async () => { { type: "mrkdwn", text: " More info: https://alert.rpi.edu" + }, + { + type: "plain_text", + text: hash } ] } ] }); } - oldtext = text; + oldHash = hash; +}; + +const getOldHash = async () => { + let latest = Date.now(); + for (;;) { + let results = await history({ + token: USER_TOKEN, + channel: ALERTS_CHANNEL, + latest: latest + }); + + let messages = results.messages.filter( + x => x.subtype === "bot_message" && x.text === "RPI ALERT - " + ); + if (messages.length > 0) { + // Old style message, does not have hash, allow repost to have it add the hash + if (!messages[0].blocks[1].elements[2]) { + break; + } + oldHash = messages[0].blocks[1].elements[2].text; + break; + } else { + if (results.has_more) { + latest = results.messages[results.messages.length - 1].ts; + } else { + // Could not find a previous bot message + break; + } + } + } }; -setInterval(function() { - rpialert(); -}, 10000); +getOldHash().then(() => { + setInterval(function() { + rpialert(); + }, 10000); +});