summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTor Andersson <tor@ccxvii.net>2023-03-16 12:21:21 +0100
committerTor Andersson <tor@ccxvii.net>2023-03-17 13:20:56 +0100
commitbd835413cf9767c35aa3b0e7c55e200a67127fb8 (patch)
treef1a63e79332dc30ba51bc55f9bed5d12b10e22c5
parent33c9e87286ccaf9dbf5bc89ed55381c7b03630b4 (diff)
downloadserver-bd835413cf9767c35aa3b0e7c55e200a67127fb8.tar.gz
Update Webhooks to allow custom JSON property for message content.
Support Slack and others.
-rw-r--r--schema.sql1
-rw-r--r--server.js24
-rw-r--r--views/webhook.pug28
3 files changed, 43 insertions, 10 deletions
diff --git a/schema.sql b/schema.sql
index c8ea5a9..b0f5570 100644
--- a/schema.sql
+++ b/schema.sql
@@ -57,6 +57,7 @@ create table if not exists last_notified (
create table if not exists webhooks (
user_id integer primary key,
url text,
+ format text,
prefix text,
error text
);
diff --git a/server.js b/server.js
index 3fad7d5..b73221f 100644
--- a/server.js
+++ b/server.js
@@ -281,8 +281,8 @@ const SQL_UPDATE_USER_LAST_SEEN = SQL("INSERT OR REPLACE INTO user_last_seen (us
const SQL_UPDATE_USER_IS_BANNED = SQL("update users set is_banned=? where name=?")
const SQL_SELECT_WEBHOOK = SQL("SELECT * FROM webhooks WHERE user_id=?")
-const SQL_SELECT_WEBHOOK_SEND = SQL("SELECT url, prefix FROM webhooks WHERE user_id=? AND error is null")
-const SQL_UPDATE_WEBHOOK = SQL("INSERT OR REPLACE INTO webhooks (user_id, url, prefix, error) VALUES (?,?,?,null)")
+const SQL_SELECT_WEBHOOK_SEND = SQL("SELECT url, format, prefix FROM webhooks WHERE user_id=? AND error is null")
+const SQL_UPDATE_WEBHOOK = SQL("INSERT OR REPLACE INTO webhooks (user_id, url, format, prefix, error) VALUES (?,?,?,?,null)")
const SQL_UPDATE_WEBHOOK_ERROR = SQL("UPDATE webhooks SET error=? WHERE user_id=?")
const SQL_DELETE_WEBHOOK = SQL("DELETE FROM webhooks WHERE user_id=?")
@@ -585,7 +585,8 @@ app.post("/delete-webhook", must_be_logged_in, function (req, res) {
app.post("/update-webhook", must_be_logged_in, function (req, res) {
let url = req.body.url
let prefix = req.body.prefix
- SQL_UPDATE_WEBHOOK.run(req.user.user_id, url, prefix)
+ let format = req.body.format
+ SQL_UPDATE_WEBHOOK.run(req.user.user_id, url, format, prefix)
const webhook = SQL_SELECT_WEBHOOK_SEND.get(req.user.user_id)
if (webhook)
send_webhook(req.user.user_id, webhook, "Test message!")
@@ -1423,7 +1424,6 @@ app.get('/rematch/:old_game_id', must_be_logged_in, function (req, res) {
let old_game_id = req.params.old_game_id | 0
let magic = "\u{1F503} " + old_game_id
let new_game_id = 0
- console.log("FOO", old_game_id, magic)
let info = SQL_INSERT_REMATCH.run({user_id: req.user.user_id, game_id: old_game_id, magic: magic})
if (info.changes === 1) {
new_game_id = info.lastInsertRowid
@@ -1701,7 +1701,7 @@ app.get('/api/replay/:game_id', function (req, res) {
* WEBHOOK NOTIFICATIONS
*/
-const webhook_options = {
+const webhook_json_options = {
method: "POST",
timeout: 6000,
headers: {
@@ -1709,6 +1709,14 @@ const webhook_options = {
}
}
+const webhook_text_options = {
+ method: "POST",
+ timeout: 6000,
+ headers: {
+ "Content-Type": "text/plain"
+ }
+}
+
function on_webhook_success(user_id) {
console.log("WEBHOOK SENT", user_id)
}
@@ -1720,8 +1728,10 @@ function on_webhook_error(user_id, error) {
function send_webhook(user_id, webhook, message) {
try {
- const data = JSON.stringify({ content: webhook.prefix + " " + message })
- const req = https.request(webhook.url, webhook_options, res => {
+ const text = webhook.prefix + " " + message
+ const data = webhook.format ? JSON.stringify({ [webhook.format]: text }) : text
+ const options = webhook.format ? webhook_json_options : webhook_text_options
+ const req = https.request(webhook.url, options, res => {
if (res.statusCode === 200 || res.statusCode === 204)
on_webhook_success(user_id)
else
diff --git a/views/webhook.pug b/views/webhook.pug
index bf7673f..ad6c75f 100644
--- a/views/webhook.pug
+++ b/views/webhook.pug
@@ -11,6 +11,7 @@ html(lang="en")
h1 Webhook
- var url = webhook && webhook.url || ""
+ - var format = webhook && webhook.format || ""
- var prefix = webhook && webhook.prefix || ""
form(action="/update-webhook" method="post")
@@ -19,6 +20,10 @@ html(lang="en")
p Webhook URL:
br
input#url(type="text" name="url" size=120 placeholder="https://discord.com/api/webhooks/..." value=url required)
+ p Webhook format:
+ br
+ input#format(type="text" name="format" size=40 placeholder="content" value=format)
+
p Message prefix:
br
input#prefix(type="text" name="prefix" size=40 placeholder="<@123456789>" value=prefix)
@@ -38,9 +43,26 @@ html(lang="en")
ol
li Create your own server or use an existing server where you have administrator privileges.
- li <a href="https://support.discord.com/hc/en-us/articles/228383668-Intro-to-Webhooks">Get the webhook URL</a> for the Discord channel where you want notifications to be sent. Enter it into the Webhook URL field.
- li Find your <a href="https://support.playhive.com/discord-user-id/">Discord User ID</a>. Enter it into the Message prefix field as "&lt;@UserID&gt;"
+ li <a href="https://support.discord.com/hc/en-us/articles/228383668-Intro-to-Webhooks">Get the webhook URL</a> for the Discord channel and enter it into the Webhook URL field.
+ li Enter "content" into the Webhook format field.
+ li Find your <a href="https://support.playhive.com/discord-user-id/">Discord User ID</a>. This is not your username, it is a number.
+ li Enter your Discord User ID into the Message prefix field as "&lt;@12345&gt;".
+
+ h2 Slack Notifications
+
+ p You can send notifications to a Slack workspace.
+
+ ol
+ li Join or set up a Slack workspace with a webhook integration.
+ li Find the Webhook URL and enter it into the Webhook URL field.
+ li Enter "text" into the Webhook format field.
+ li Find your <a href="">Slack User ID</a>. This is a number with "U" in front of it.
+ li Enter your Slack User ID into the Message prefix field as "&lt;@U12345&gt;".
h2 Custom Notifications
- p You can integrate with any server that accepts inbound webhooks by setting the webhook URL to the appropriate endpoint. The webhook payload is a JSON object with a "content" property containing the notification message.
+ p.
+ You can integrate with any server that accepts inbound webhooks by setting the webhook URL to the appropriate endpoint.
+ If the format field is blank, the payload is sent as plain text.
+ Otherwise, the payload is a JSON object where the format field specifies which JSON property holds the message.
+ Use "content" for Discord and "text" for Slack.