Skip to content

Commit

Permalink
Merge pull request #9 from rpiambulance/dev
Browse files Browse the repository at this point in the history
Merge dev to master to fix #6
  • Loading branch information
ddbruce authored Oct 12, 2019
2 parents a15971b + 2aea6e8 commit 94f3b9e
Show file tree
Hide file tree
Showing 7 changed files with 181 additions and 15 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SLACK_USER_TOKEN=longtokenhere
RPIALERT_URL=https://example.com/alert.js
38 changes: 38 additions & 0 deletions .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
@@ -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.
20 changes: 20 additions & 0 deletions .github/ISSUE_TEMPLATE/feature_request.md
Original file line number Diff line number Diff line change
@@ -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.
21 changes: 21 additions & 0 deletions .github/ISSUE_TEMPLATE/pending-task.md
Original file line number Diff line number Diff line change
@@ -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.
35 changes: 35 additions & 0 deletions .github/ISSUE_TEMPLATE/support-question.md
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
78 changes: 64 additions & 14 deletions server.js
Original file line number Diff line number Diff line change
@@ -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 - <!channel>",
blocks: [
Expand All @@ -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 - <!channel>"
);
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);
});

0 comments on commit 94f3b9e

Please sign in to comment.