diff --git a/hypha/apply/activity/messaging.py b/hypha/apply/activity/messaging.py index 07d56b18c26a36e4fae32d564a002514b118a4d0..956aac839aaaf44b507adccf25c036a8f3662671 100644 --- a/hypha/apply/activity/messaging.py +++ b/hypha/apply/activity/messaging.py @@ -2,7 +2,6 @@ import json import logging from collections import defaultdict -import requests from django.conf import settings from django.contrib import messages from django.contrib.auth import get_user_model @@ -10,6 +9,7 @@ from django.db import models from django.template.loader import render_to_string from django.utils import timezone from django.utils.translation import gettext as _ +from django_slack import slack_message from hypha.apply.projects.models.payment import ( APPROVED_BY_FINANCE_1, @@ -727,12 +727,10 @@ class SlackAdapter(AdapterBase): message = ' '.join([recipient, message]).strip() data = { - "room": target_rooms, "message": message, } - response = requests.post(self.destination, json=data) - - return str(response.status_code) + ': ' + response.content.decode() + for room in target_rooms: + slack_message('messages/slack_message.html', data, channel=room) class EmailAdapter(AdapterBase): diff --git a/hypha/apply/activity/templates/messages/slack_message.html b/hypha/apply/activity/templates/messages/slack_message.html new file mode 100644 index 0000000000000000000000000000000000000000..122ced225924de9a743532f18a121e25cccb4cab --- /dev/null +++ b/hypha/apply/activity/templates/messages/slack_message.html @@ -0,0 +1,6 @@ +{% extends django_slack %} +<!--Template required for django-slack. We can customize it for channels, endpoint_url, etc as per the requirements.--> + +{% block text %} +{{ message }} +{% endblock %} diff --git a/hypha/settings/base.py b/hypha/settings/base.py index 10f87df0d0df4900b15d8b49a12ca971721d0a1d..b0bb036f5407c5675291463442def2de974c0ab0 100644 --- a/hypha/settings/base.py +++ b/hypha/settings/base.py @@ -105,6 +105,7 @@ INSTALLED_APPS = [ 'django_bleach', 'django_fsm', 'django_pwned_passwords', + 'django_slack', 'django_otp', 'django_otp.plugins.otp_totp', 'django_otp.plugins.otp_static', @@ -387,6 +388,11 @@ AUTHENTICATION_BACKENDS = ( LOGGING = { 'version': 1, 'disable_existing_loggers': False, + 'filters': { + 'require_debug_false': { + '()': 'django.utils.log.RequireDebugFalse', + }, + }, 'handlers': { # Send logs with at least INFO level to the console. 'console': { @@ -394,6 +400,11 @@ LOGGING = { 'class': 'logging.StreamHandler', 'formatter': 'verbose', }, + 'slack_admins': { + 'level': 'ERROR', + 'filters': ['require_debug_false'], + 'class': 'django_slack.log.SlackExceptionHandler', + } }, 'formatters': { 'verbose': { @@ -426,6 +437,10 @@ LOGGING = { 'level': 'WARNING', 'propagate': False, }, + 'django.slack': { + 'level': 'ERROR', + 'handlers': ['slack_admins'], + }, }, } @@ -529,6 +544,12 @@ SLACK_DESTINATION_ROOM = env.str('SLACK_DESTINATION_ROOM', None) SLACK_DESTINATION_ROOM_COMMENTS = env.str('SLACK_DESTINATION_ROOM_COMMENTS', None) SLACK_TYPE_COMMENTS = env.list('SLACK_TYPE_COMMENTS', []) +# Django Slack settings +SLACK_TOKEN = env.str('SLACK_TOKEN') +SLACK_BACKEND = 'django_slack.backends.UrllibBackend' # CeleryBackend can be used for async +if SLACK_DESTINATION_URL: + SLACK_ENDPOINT_URL = SLACK_DESTINATION_URL + # Automatic transition settings TRANSITION_AFTER_REVIEWS = env.bool('TRANSITION_AFTER_REVIEWS', False) TRANSITION_AFTER_ASSIGNED = env.bool('TRANSITION_AFTER_ASSIGNED', False) diff --git a/requirements.txt b/requirements.txt index c66d1540300e2172c40cbf54f95da9a7aad5f82a..bad1dafa298265c3a425c49f12fa95efe39a375b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -24,6 +24,7 @@ django-redis==5.1.0 django-referrer-policy==1.0 django-salesforce==4.0 django-select2==7.9.0 +django-slack==5.17.7 django-storages==1.12.3 django-tables2==2.4.1 django-tinymce==3.4.0