# District notifications

Send each alert to the district by email and SMS and return the date of the notification.

This page follows [Thresholds](https://zato.io/docs/dev/healthcare/dhis2/outbreak-alerts/thresholds.html) in the [Outbreak alerts](https://zato.io/docs/dev/healthcare/dhis2/outbreak-alerts/index.html) series. The service below sends one alert to the district it concerns, by email and by SMS, and returns the date of the notification, which the 7-1-7 register keeps.

## Email and SMS {#email-and-sms}

An email is an `SMTPMessage` sent through the `District Alerts` connection. An SMS is a request to the provider's API through the `SMS Gateway` connection, with a payload in the provider's format - the one below has a recipient and a text field:

```python
# -*- coding: utf-8 -*-

# stdlib
from datetime import date

# Zato
from zato.common import SMTPMessage
from zato.server.service import Service

class NotifyDistrict(Service):
    name = 'dhis2.alerts.notify-district'
    input = 'disease', 'district', 'district_name', 'week', 'cases', 'detected'

    def send_email(self, address:'str', text:'str') -> 'None':

        message = SMTPMessage()
        message.subject = text
        message.to = address
        message.from_ = self.config.alerts.email.sender
        message.body = text

        conn = self.email.smtp['District Alerts'].conn
        conn.send(message)

    def send_sms(self, phone:'str', text:'str') -> 'None':

        payload = {'to': phone, 'text': text}

        conn = self.rest['SMS Gateway']
        conn.post(payload)
```

## The notification {#the-notification}

The addresses of the district come from `districts.ini`, and one line of text serves as the email subject, the email body and the SMS:

```python
    def handle(self) -> 'None':

        request = self.request.input
        district = self.config.districts[request.district]

        # One line that reads the same in an email and an SMS ..
        template = '{} alert in {}: {} cases in week {}'
        text = template.format(
            request.disease,
            request.district_name,
            request.cases,
            request.week,
        )

        # .. sent both ways ..
        self.send_email(district.email, text)
        self.send_sms(district.phone, text)

        # .. on the day that becomes the notification date.
        today = date.today()
        notified = today.isoformat()

        self.response.payload = {'notified': notified}
```

A cholera alert in the district named `North` reads `cholera alert in North: 3 cases in week 2026W40`.

## Next {#next}

[Alerts in DHIS2](https://zato.io/docs/dev/healthcare/dhis2/outbreak-alerts/dhis2-alert.html) records the alert in the DHIS2 alert program, and [Alerts in an EMS](https://zato.io/docs/dev/healthcare/dhis2/outbreak-alerts/ems.html) records it in an Odoo-based emergency management system instead.

## See also {#see-also}

- [SMTP](https://zato.io/docs/dev/examples/smtp.html) - Plain text and HTML email, attachments
- [Calling REST APIs](https://zato.io/docs/dev/rest/calling-apis.html) - Payloads and response objects

## Learn more {#learn-more}

- [Healthcare interface engine](https://zato.io/docs/dev/healthcare/) - Clinical messages, FHIR, conversion and operations
- [HL7 v2 parsing](https://zato.io/docs/dev/healthcare/hl7/v2/parsing/) - Messages as typed objects, with field access and validation
- [MLLP channels](https://zato.io/docs/dev/healthcare/hl7v2/mllp/) - Receiving and sending HL7 v2 over MLLP sockets
- [FHIR integrations](https://zato.io/docs/dev/healthcare/hl7/fhir/) - Read and write FHIR resources, with paths, bundles and extensions
- [HL7 v2 to FHIR](https://zato.io/docs/dev/healthcare/hl7/to-fhir/) - Converting v2 messages into FHIR bundles, codes and references included
- [EDIFACT](https://zato.io/docs/dev/healthcare/edifact/) - Parsing interchanges, dialects and the transports they arrive on
- [Transformation](https://zato.io/docs/dev/healthcare/transformation/) - One message in, another standard out, in ordinary Python
- [Audit log](https://zato.io/docs/dev/healthcare/audit-log.html) - Every message with its acknowledgment, searchable by patient identifier
- [AI in clinical interfaces](https://zato.io/docs/dev/healthcare/ai/) - Services calling LLMs and AI agents calling clinical services as tools
