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 3c12ef1e8d4c88d3069df6f3e852ca1ac46deb17..d8bc5941e06b4a3db70ba0705d2dc72c0ea557cd 100644 --- a/hypha/settings/base.py +++ b/hypha/settings/base.py @@ -104,6 +104,7 @@ INSTALLED_APPS = [ 'django_bleach', 'django_fsm', 'django_pwned_passwords', + 'django_slack', 'django_otp', 'django_otp.plugins.otp_totp', 'django_otp.plugins.otp_static', @@ -382,6 +383,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': { @@ -389,6 +395,11 @@ LOGGING = { 'class': 'logging.StreamHandler', 'formatter': 'verbose', }, + 'slack_admins': { + 'level': 'ERROR', + 'filters': ['require_debug_false'], + 'class': 'django_slack.log.SlackExceptionHandler', + } }, 'formatters': { 'verbose': { @@ -421,6 +432,10 @@ LOGGING = { 'level': 'WARNING', 'propagate': False, }, + 'django.slack': { + 'level': 'ERROR', + 'handlers': ['slack_admins'], + }, }, } @@ -524,6 +539,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 ae395c233b0995025c6537b685735319a1bd60e0..d6a98b9e3dc984d0772d58c19ad7cdb3ba3381e7 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