oncall-engine/engine/apps/phone_notifications/models/sms.py
Yulya Artyukhina 35ddfab0e4
Add method to send notification bundle by SMS (#4624)
# What this PR does
Adds method to render and send notification bundle by sms.

Example of SMS message:
```
Grafana OnCall: Alert groups #1, #2, #3 and 2 more 
from stack: TestOrganization, 
integrations: Grafana Alerting and 1 more.
```

Should be merged with https://github.com/grafana/oncall/pull/4457

## Which issue(s) this PR closes
https://github.com/grafana/oncall-private/issues/2713

## Checklist

- [x] Unit, integration, and e2e (if applicable) tests updated
- [x] Documentation added (or `pr:no public docs` PR label added if not
required)
- [x] Added the relevant release notes label (see labels prefixed w/
`release:`). These labels dictate how your PR will
    show up in the autogenerated release notes.
2024-07-16 14:20:16 +00:00

88 lines
2.8 KiB
Python

from django.db import models
# Duplicate to avoid circular import to provide values for status field
class TwilioSMSstatuses:
"""
https://www.twilio.com/docs/sms/tutorials/how-to-confirm-delivery-python?code-sample=code-handle-a-sms-statuscallback&code-language=Python&code-sdk-version=5.x#receive-status-events-in-your-web-application
https://www.twilio.com/docs/sms/api/message-resource#message-status-values
"""
ACCEPTED = 10
QUEUED = 20
SENDING = 30
SENT = 40
FAILED = 50
DELIVERED = 60
UNDELIVERED = 70
RECEIVING = 80
RECEIVED = 90
READ = 100
CHOICES = (
(ACCEPTED, "accepted"),
(QUEUED, "queued"),
(SENDING, "sending"),
(SENT, "sent"),
(FAILED, "failed"),
(DELIVERED, "delivered"),
(UNDELIVERED, "undelivered"),
(RECEIVING, "receiving"),
(RECEIVED, "received"),
(READ, "read"),
)
class SMSRecord(models.Model):
class Meta:
db_table = "twilioapp_smsmessage"
exceeded_limit = models.BooleanField(null=True, default=None)
represents_alert = models.ForeignKey(
"alerts.Alert", on_delete=models.SET_NULL, null=True, default=None
) # deprecated
represents_alert_group = models.ForeignKey("alerts.AlertGroup", on_delete=models.SET_NULL, null=True, default=None)
notification_policy = models.ForeignKey(
"base.UserNotificationPolicy", on_delete=models.SET_NULL, null=True, default=None
)
represents_bundle_uuid = models.CharField(max_length=100, null=True, default=None, db_index=True)
receiver = models.ForeignKey("user_management.User", on_delete=models.CASCADE, null=True, default=None)
grafana_cloud_notification = models.BooleanField(default=False)
created_at = models.DateTimeField(auto_now_add=True)
# deprecated. It's here for backward compatibility for sms sent during or shortly before migration.
# Should be removed soon after migration
status = models.PositiveSmallIntegerField(
blank=True,
null=True,
choices=TwilioSMSstatuses.CHOICES,
)
sid = models.CharField(
blank=True,
max_length=50,
)
class ProviderSMS(models.Model):
"""
ProviderSMS is an interface between SMSRecord and call data returned from PhoneProvider.
Concrete provider sms be inherited from ProviderSMS.
The idea is same as for ProviderCall - to save provider specific data without exposing them to PhoneBackend.
"""
class Meta:
abstract = True
sms_record = models.OneToOneField(
"phone_notifications.SMSRecord",
on_delete=models.CASCADE,
related_name="%(app_label)s_%(class)s_related",
related_query_name="%(app_label)s_%(class)ss",
null=False,
)
def link_and_save(self, sms_record: SMSRecord):
self.sms_record = sms_record
self.save()