Message Scheduling
Sometimes, it becomes necessary to compose your message and instruct the messaging server to not dispatch the messages immediately but rather at your own specified dates and time. When the specified date and time is due, the messages will automatically be dispatched to the added recipients.
We can easily achieve this by setting actual date and time at which the messages should be dispatched to the destinations in the message data.
Setting Schedule Date and Time
To schedule a message, we need to set the actual date and time that
the messages will have to be dispatched to the destinations. This is done
by specifying the date and time as dateTime property, with an optional
offset property for UTC timezone offset, to the message data.
The following message data includes the schedule property which is the container property for
dateTime and offset properties. Observe that the date and time have
been set to the dateTime property as a single value.
{
"text": "Hello World!",
"type": 0,
"sender": "TEST",
"destinations": ["233242053072", "0246314915"],
"schedule": {
"dateTime": "2023-10-24 15:45",
"offset": "+00:00"
}
}
As indicated earlier, the timezone offset from UTC is optional and can be omitted.
When not specified, a default timzone offset from UTC will be used from client account.
The following message data specifies the schedule dateTime without the timezone offset from UTC.
{
"text": "Hello World!",
"type": 0,
"sender": "TEST",
"destinations": ["233242053072", "0246314915"],
"schedule": {
"dateTime": "2023-10-24 15:45"
}
}
Example Schedule Request
The following is an example code that sends SMS by defining a specific date and time at which the message should be dispatched to the destinations.
<?php
// set up the request headers
$headers = [
'Host: api.smsonlinegh.com',
'Content-Type: application/json',
'Accept: application/json',
'Authorization: key d5c683a1b4c3d2f278be3d4c03c23191b2f133378b12b6e197c1ad5d9b34c128'
];
// set up the message data
$messageData = [
'text'=>'This is a test message',
'type'=> 0, // GSM default
'sender'=> 'TEST',
'destinations'=> ['0246314915', '0242053072']
];
// set the schedule date and time, as well as the UTC offset
$messageData['schedule']['dateTime'] = '2023-10-24 15:45';
$messageData['schedule']['offset'] = '+00:00';
// initialise cURL
$ch = curl_init('https://api.smsonlinegh.com/v5/sms/send');
// set cURL optionS
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($messageData));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Execute for response
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// close curl
curl_close($ch);
if ($httpCode == 200){
var_dump($response);
}
After the above sample message is submitted, a message response data similar to non-scheduled message response data will be received. The only difference between the message response data for scheduled and non-scheduled messages is the status of the destinations.
The following is a sample message response data for a scheduled message.
{
"handshake" : {
"id" : 0,
"label" : "HSHK_OK"
},
"data": {
"batch" : "cfa19ba67f94fbd6b19c067b0c87ed4f",
"category": 1,
"delivery": false,
"destinations": [
{
"id" : "093841e5-578a-41f4-5f5f-2f3910886c12",
"to" : "233246314915",
"status" : {
"id" : 2104,
"label" : "DS_SUBMIT_SCHEDULED"
}
},
{
"id" : "4abfa3bd-93f9-b8a1-4bbd-78725ca3e488",
"to" : "233242053072",
"status" : {
"id" : 2104,
"label" : "DS_SUBMIT_SCHEDULED"
}
}
]
}
}
Cancelling Scheduled Message
If the need arises to cancel the dispatch of a message that has been scheduled, then a request has to be made.
For every message request, the response data contains a batch identifier in
the data section. This batch identifier can be used to reference the
submitted message later. To cancel a message that was
scheduled, the batch identifier will be needed in making request to
cancel scheduled message.
The API endpoing for cancelling a scheduled message is given as follows:
https://api.smsonlinegh.com/v5/message/scheduled/cancel/[BATCH_ID]
Notice [BATCH_ID] at the end. This must be replaced with the batch identifier
that was returned in the message response data when the message was submitted for scheduling.
For example, if the batch identifier for the submitted message is cfa19ba67f94fbd6b19c067b0c87ed4f,
then the request to cancel the scheduled message will use the following API call:
https://api.smsonlinegh.com/v5/message/scheduled/cancel/cfa19ba67f94fbd6b19c067b0c87ed4f
Remember that the server needs to know the content type of the response that should be returned. Hence,
the Accept header must be set to either application/json or application/xml. The following
shows the request header for cancelling a scheduled message.
POST https://api.smsonlinegh.com/v5/message/scheduled/cancel/cfa19ba67f94fbd6b19c067b0c87ed4f
Host: api.smsonlinegh.comAccept: application/json
Authorization: key d5c683a1b4c3d2f278be3d4c03c23191b2f133378b12b6e197c1ad5d9b34c128
If the specified scheduled message has not already been dispatched to the destinations, then the dispatch operation will immediately be cancelled.