From 10de956256f75a7e53286db8aefe671be9614c9e Mon Sep 17 00:00:00 2001 From: Tanner Collin Date: Sat, 29 Jul 2023 01:51:49 +0000 Subject: [PATCH] Message hosting offer to "Find a Host" chat --- apiserver/apiserver/api/utils.py | 19 ++++++++++++++++++- apiserver/apiserver/api/views.py | 14 ++++++++++++++ apiserver/apiserver/secrets.py.example | 6 ++++++ 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/apiserver/apiserver/api/utils.py b/apiserver/apiserver/api/utils.py index 9b3442e..1d54865 100644 --- a/apiserver/apiserver/api/utils.py +++ b/apiserver/apiserver/api/utils.py @@ -22,7 +22,7 @@ from django.core.cache import cache from django.utils.timezone import now, pytz from . import models, serializers, utils_ldap, utils_stats, utils_auth, utils, utils_email -from .. import settings +from .. import settings, secrets STATIC_FOLDER = 'data/static/' @@ -42,6 +42,23 @@ def alert_tanner(message): except BaseException as e: logger.error('Problem alerting Tanner: ' + str(e)) +def spaceporter_host(message): + logger.info('Spaceporter bot sending to host chat: ' + message) + + if secrets.SPACEPORTER_HOST_TOKEN: + url = 'https://forum.protospace.ca/chat/hooks/{}.json'.format( + secrets.SPACEPORTER_HOST_TOKEN, + ) + else: + logger.info('Aborting Spaceporter bot message, no token.') + return + + try: + data = dict(text=message) + requests.post(url, json=data, timeout=4) + except BaseException as e: + logger.error('Problem with bot: ' + str(e)) + def num_months_spanned(d1, d2): ''' Return number of month thresholds two dates span. diff --git a/apiserver/apiserver/api/views.py b/apiserver/apiserver/api/views.py index 4c74ee9..c855f38 100644 --- a/apiserver/apiserver/api/views.py +++ b/apiserver/apiserver/api/views.py @@ -1794,6 +1794,20 @@ class HostingViewSet(Base): logging.info('No current hosting for that user, new hosting #%s created.', h.id) + try: # TODO: remove try / except + # send a message to Spacebar + message = 'A member just offered to host for {} hours from now until {}!'.format( + hours, + h.finished_at.astimezone(utils.TIMEZONE_CALGARY).strftime('%-I:%M %p'), + ) + if hosting_user.member.discourse_username: + message += ' Tag @{} here to get their attention.'.format( + hosting_user.member.discourse_username, + ) + utils.spaceporter_host(message) + except: + pass + # update "open until" time hosting = models.Hosting.objects.order_by('-finished_at').first() closing = dict( diff --git a/apiserver/apiserver/secrets.py.example b/apiserver/apiserver/secrets.py.example index 7a887dd..99bf622 100644 --- a/apiserver/apiserver/secrets.py.example +++ b/apiserver/apiserver/secrets.py.example @@ -80,6 +80,12 @@ PINBALL_API_TOKEN = '' # head /dev/urandom | base32 | head -c 40 ALARM_API_TOKEN = '' +# Spaceporter bot webhook URL token +# Posts to the "find a host" chat +# Found in /admin/plugins/chat URL section +# should look like '37f5e8d9c64064c86109b7ea' +SPACEPORTER_HOST_TOKEN = '' + # Protospace general info DOOR_CODE = '' WIFI_PASS = ''