District notifications
Send each alert to the district by email and SMS and return the date of the notification.
This page follows Thresholds in the Outbreak alerts 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
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:
# -*- 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 addresses of the district come from districts.ini, and one line of text serves as the email subject, the email body and the SMS:
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
Alerts in DHIS2 records the alert in the DHIS2 alert program, and Alerts in an EMS records it in an Odoo-based emergency management system instead.
See also
| Feature | What it does |
|---|---|
| SMTP | Plain text and HTML email, attachments |
| Calling REST APIs | Payloads and response objects |