oncall-engine/engine/apps/base/messaging.py
Vadim Stepanov e67d3519fe
Restore email notifications (#621)
* remove email verification related code

* remove email verification related code

* remove sendgrid callback

* remove sendgrid related code

* remove sendgrid related code

* rename sendgrid app to email

* remove email from built-in channels

* remove email from built-in channels

* remove email from built-in channels

* add email backend: https://github.com/grafana/oncall/pull/50

* add email templater

* add email templater

* convert md to html

* add email settings to live settings

* use task to send email, handle some exceptions to create logs

* remove ERROR_NOTIFICATION_MAIL_DELIVERY_FAILED usage

* add email limit logic

* fix tests

* add docs

* remove old email templates

* remove old email templates

* add template_fields to messaging backend

* add messaging backends templates to public api

* add comment for deprecated fields

* fix test

* fix tests

* disable email by default

* don't retry on SMTPException and TimeoutError

* add tests

* bring email back to public api docs

* return ERROR_NOTIFICATION_MAIL_LIMIT_EXCEEDED

* make template_fields tuple

* build_subject_and_title -> build_subject_and_message

* add one more comment about template deprecation

* use 8 as backend id

* add comment about gaierror and BadHeaderError

* add comment on importing in notify_user_async

* edit oss docs
2022-10-19 12:32:56 +01:00

72 lines
2.4 KiB
Python

from django.conf import settings
from django.utils.module_loading import import_string
class BaseMessagingBackend:
backend_id = "SOMEID"
label = "The Backend"
short_label = "Backend"
available_for_use = False
templater = None
template_fields = ("title", "message", "image_url")
def __init__(self, *args, **kwargs):
self.notification_channel_id = kwargs.get("notification_channel_id")
def get_templater_class(self):
if self.templater:
return import_string(self.templater)
def validate_channel_filter_data(self, organization, data):
"""Validate JSON channel data for a channel filter update.
Ensure the required/expected data is provided as needed by the backend.
"""
return data
def generate_channel_verification_code(self, organization):
"""Return a verification code for a channel registration."""
raise NotImplementedError("generate_channel_verification_code method missing implementation")
def generate_user_verification_code(self, user):
"""Return a verification code to link a user with an account."""
raise NotImplementedError("generate_user_verification_code method missing implementation")
def unlink_user(self, user):
"""Remove backend link to user account."""
return
def serialize_user(self, user):
"""Return a serialized backend user representation."""
raise NotImplementedError("serialize_user method missing implementation")
def notify_user(self, user, alert_group, notification_policy):
"""Send user a notification for the given alert group.
The notification policy links to the backend as the notification channel.
"""
raise NotImplementedError("notify_user method missing implementation")
def load_backend(path, *args, **kwargs):
return import_string(path)(*args, **kwargs)
def get_messaging_backends():
global _messaging_backends
if _messaging_backends is None:
_messaging_backends = {}
for (backend_path, notification_channel_id) in settings.EXTRA_MESSAGING_BACKENDS:
backend = load_backend(backend_path, notification_channel_id=notification_channel_id)
_messaging_backends[backend.backend_id] = backend
return _messaging_backends.items()
def get_messaging_backend_from_id(backend_id):
return _messaging_backends.get(backend_id)
_messaging_backends = None