Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add first blood webhook #134

Merged
merged 4 commits into from
Jul 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ coverage = {extras = ["toml"], version = "^5.5"}
Twisted = "20.3.0"
channels-redis = "^3.2.0"
django-clacks = "^0.2.0"
requests = "^2.26.0"
clubby789 marked this conversation as resolved.
Show resolved Hide resolved

[tool.poetry.dev-dependencies]
ipython = "^7.19.0"
Expand Down
1 change: 1 addition & 0 deletions src/backend/settings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
"hide_scoreboard_at": -1,
"setup_wizard_complete": False,
"sensitive_fields": ["sensitive_fields", "enable_force_admin_2fa"],
"firstblood_webhook": "",
}

INSTALLED_APPS = [
Expand Down
24 changes: 24 additions & 0 deletions src/challenge/views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import hashlib
import os
import time
from contextlib import suppress
from typing import Union

import requests
from django.conf import settings
from django.contrib.auth import get_user_model
from django.core.cache import caches
Expand Down Expand Up @@ -289,6 +292,27 @@ def post(self, request):
if challenge.first_blood is None:
challenge.first_blood = user
challenge.save()
hook = config.get("firstblood_webhook")
if hook and hook != "":
challenge_clean = challenge.name.replace("`", "").replace("@", "@\u200b")
team_clean = team.name.replace("`", "").replace("@", "@\u200b")
if "discord.com" in hook and not hook.endswith("/slack"):
hook = hook + "/slack"
challenge_clean = challenge_clean.replace("@", "@\u200b")
team_clean = team_clean.replace("@", "@\u200b")
body = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like this would be better off as a Slack webhook so its not locked to one platform, Discord is compatible via adding /slack on the end of the webhook url. This does remove our ability to use allowed_mentions to sanitize the message but I think the same can be achieved with just replacing all @ with @[zero width space]. Also, this might look better as an embed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went with using Slack by default, and a Discord-style webhook if the url is a Discord one. The /slack endpoint didn't seem to support embeds, I could be wrong.

"username": "First Bloods",
"text": "First blood!",
"attachments": [
{
"text": f"Team {team_clean} has taken first blood on {challenge_clean}",
"color": "#ff0000",
}
],
}

with suppress(requests.exceptions.RequestException):
requests.post(hook, json=body)

user.save()
team.save()
Expand Down