Notification
Short Message Service (SMS)#
This section covers the SMS Web API for sending and receiving short text messages.
Outbound SMS service#
The Web API supports sending outgoing SMS using the POST method. SMS can be sent to single or multiple destinations. One or more gateways need to be configured before using the service. An SMS will not be sent if there is no gateway configured. It needs a set of recipients and message text in JSON format as shown below.
Note
Recipients list will be partitioned if the size exceeds
MAX_ALLOWED_RECIPIENTSlimit of 200.
The Web API also supports a query parameter version, but the parameterized API can only be used for sending SMS to a single destination.
Gateway response codes#
Gateway may response with following response codes.
Inbound SMS service#
The Web API supports collecting incoming SMS messages using the POST method. Incoming messages routed towards the HRHIS4 Web API can be received using this API. The API collects inbound SMS messages and provides it to listeners for parsing, based on the SMS content (SMS Command). An example payload in JSON format is given below. Text, originator, received date and sent date are mandatory parameters. The rest are optional but the system will use the default value for these parameters.
The Web API also supports a query parameter-based version.
Gateway service administration#
The Web API exposes resources which provide a way to configure and update SMS gateway configurations.
The list of different gateways configured can be retrieved using a GET method.
Configurations can also be retrieved for a specific gateway type using GET method.
New gateway configuraitons can be added using POST. POST api requires type request parameter and currently its value can have either one http,bulksms,clickatell. First added gateway will be set to default. Only one gateway is allowed to be default at one time. Default gateway can only be changed through its api. If default gateway is removed then the next one the list will automatically becomes default.
Configuration can be updated with by providing uid and gateway configurations as mentioned below
Configurations can be removed for specific gateway type using DELETE method.
Default gateway can be retrieved and updated.
Default gateway can be set using the PUT method.
Gateway configuration#
The Web API lets you create and update gateway configurations. For each type of gateway there are different parameters in the JSON payload. Sample JSON payloads for each gateway are given below. POST is used to create and PUT to update configurations. Header parameter can be used in case of GenericHttpGateway to send one or more parameter as http header.
Clickatell#
Bulksms#
SMPP Gateway#
Generic HTTP#
In generic http gateway any number of parameters can be added.
HTTP.OK will be returned if configurations are saved successfully otherwise Error
Email#
The Web API features a resource for sending emails. For emails to be sent it is required that the SMTP configuration has been properly set up and that a system notification email address for the HRHIS4 instance has been defined. You can set SMTP settings from the email settings screen and system notification email address from the general settings screen in HRHIS4.
System notification#
The notification resource lets you send system email notifications with a given subject and text in JSON or XML. The email will be sent to the notification email address as defined in the HRHIS4 general system settings:
You can send a system email notification by posting to the notification resource like this:
Outbound emails#
You can also send a general email notification by posting to the
notification resource as mentioned below. F_SEND_EMAIL or ALL
authority has to be in the system to make use of this api. Subject
parameter is optional. "DHIS 2" string will be sent as default subject
if it is not provided in url. Url should be encoded in order to use this
API.
Test message#
To test whether the SMTP setup is correct by sending a test email to yourself you can interact with the test resource. To send test emails it is required that your HRHIS4 user account has a valid email address associated with it. You can send a test email like this:
Message conversations#
HRHIS4 features a mechanism for sending messages for purposes such as user feedback, notifications, and general information to users. Messages are grouped into conversations. To interact with message conversations you can send POST and GET request to the messageConversations resource.
Messages are delivered to the HRHIS4 message inbox but can also be sent to the user's email addresses and mobile phones as SMS. In this example, we will see how we can utilize the Web API to send, read and manage messages. We will pretend to be the HRHIS4 Administrator user and send a message to the Mobile user. We will then pretend to be the mobile user and read our new message. Following this, we will manage the admin user inbox by marking and removing messages.
Writing and reading messages#
The resource we need to interact with when sending and reading messages is the messageConversations resource. We start by visiting the Web API entry point at http://dev.hrhis.dhis2udsm.org/demo/api where we find and follow the link to the messageConversations resource at http://dev.hrhis.dhis2udsm.org/demo/api/messageConversations. The description tells us that we can use a POST request to create a new message using the following XML format for sending to multiple users:
For sending to all users contained in one or more user groups, we can use:
For sending to all users connected to one or more organisation units, we can use:
Since we want to send a message to our friend the mobile user we need to
look up her identifier. We do so by going to the Web API entry point and
follow the link to the users resource at /api/users. We continue by
following link to the mobile user at /api/users/PhzytPW3g2J where we learn
that her identifier is PhzytPW3g2J. We are now ready to put our XML
message together to form a message where we want to ask the mobile user
whether she has reported data for January 2014:
To test this we save the XML content into a file called message.xml. We use cURL to dispatch the message the HRHIS4 demo instance where we indicate that the content-type is XML and authenticate as the admin user:
A corresponding payload in JSON and POST command looks like this:
If all is well we receive a 201 Created HTTP status code. Also, note that we receive a Location HTTP header which value informs us of the URL of the newly created message conversation resource - this can be used by a consumer to perform further action.
We will now pretend to be the mobile user and read the message which was just sent by dispatching a GET request to the messageConversations resource. We supply an Accept header with application/xml as the value to indicate that we are interested in the XML resource representation and we authenticate as the mobile user:
In response we get the following XML:
From the response, we are able to read the identifier of the newly sent message which is ZjHHSjyyeJ2. Note that the link to the specific resource is embedded and can be followed in order to read the full message. We can reply directly to an existing message conversation once we know the URL by including the message text as the request payload. We are now able to construct a URL for sending our reply:
If all went according to plan you will receive a 200 OK status code.
In 2.30 we added an URL search parameter:
The filter searches for matches in subject, text, and senders for message conversations. The default query operator is token, however other operators can be defined in the query.
Managing messages#
As users receive and send messages, conversations will start to pile up in their inboxes, eventually becoming laborious to track. We will now have a look at managing a user's messages inbox by removing and marking conversations through the Web-API. We will do so by performing some maintenance in the inbox of the "DHIS Administrator" user.
First, let's have a look at removing a few messages from the inbox. Be
sure to note that all removal operations described here only remove the
relation between a user and a message conversation. In practical terms
this means that we are not deleting the messages themselves (or any
content for that matter) but are simply removing the message thread from
the user such that it is no longer listed in the
/api/messageConversations resource.
To remove a message conversation from a users inbox we need to issue a
DELETE request to the resource identified by the id of the message
conversation and the participating user. For example, to remove the user
with id xE7jOejl9FI from the conversation with id jMe43trzrdi:
If the request was successful the server will reply with a 200 OK. The response body contains an XML or JSON object (according to the accept header of the request) containing the id of the removed user.
On failure the returned object will contain a message payload which describes the error.
The observant reader will already have noticed that the object returned on success in our example is actually a list of ids (containing a single entry). This is due to the endpoint also supporting batch removals. The request is made to the same messageConversations resource but follows slightly different semantics. For batch operations, the conversation ids are given as query string parameters. The following example removes two separate message conversations for the current user:
If you have sufficient permissions, conversations can be removed on behalf of another user by giving an optional user id parameter.
As indicated, batch removals will return the same message format as for single operations. The list of removed objects will reflect successful removals performed. Partially erroneous requests (i.e. non-existing id) will therefore not cancel the entire batch operation.
Messages carry a boolean read property. This allows tracking whether a user has seen (opened) a message or not. In a typical application scenario (e.g. the HRHIS4 web portal) a message will be marked read as soon as the user opens it for the first time. However, users might want to manage the read or unread status of their messages in order to keep track of certain conversations.
Marking messages read or unread follows similar semantics as batch
removals, and also supports batch operations. To mark messages as read
we issue a POST to the messageConversations/read resource with a
request body containing one or more message ids. To mark messages as
unread we issue an identical request to the messageConversations/unread
resource. As is the case for removals, an optional user request parameter
can be given.
Let's mark a couple of messages as read by the current user:
The response is a 200 OK with the following JSON body:
You can add recipients to an existing message conversation. The resource is located at:
The options for this resource is a list of users, user groups and organisation units. The request should look like this:
Message Attachments#
Creating messages with attachments is done in two steps: uploading the file to the attachments resource, and then including one or several of the attachment IDs when creating a new message.
A POST request to the attachments resource will upload the file to the server.
The request returns an object that represents the attachment. The id of this object must be used when creating a message in order to link the attachment with the message.
When creating a new message, the ids can be passed in the request body to link the uploaded files to the message being created.
When replying to a message, the ids can be passed as a request parameter.
Tickets and Validation Result Notifications#
You can use the "write feedback" tool to create tickets and messages. The only difference between a ticket and a message is that you can give a status and a priority to a ticket. To set the status:
To set the priority:
In 2.29, messages generated by validation analysis now also be used in the status and priority properties. By default, messages generated by validation analysis will inherit the priority of the validation rule in question, or the highest importance if the message contains multiple rules.
In 2.30, validation rules can be assigned to any user while tickets still need to be assigned to a user in the system's feedback recipient group.
You can also add an internal message to a ticket, which can only be seen by users who have "Manage tickets" permissions. To create an internal reply, include the "internal" parameter, and set it to