How to schedule an SMS in Perl

The most simple way to send SMS from Perl is to use the built in HTTP/Rest SMS api of Ozeki SMS Gateway. When you use this API, you will send SMS messages by issuing a HTTP Post request to the SMS gateway. The HTTP Post request will contain a message formatted in json format. The SMS gateway will send this SMS to the recipient phone, and it will return a HTTP 200 OK response to your request.

how to schedule an sms in perl
Figure 1 - How to schedule an SMS in Perl

Perl code to send scheduled sms to mobile

The Perl sms code sample below demonstrates how you can send SMS using the http rest sms api of Ozeki SMS Gateawy using the Perl Ozeki.Libs.Rest library. This library is provided to you free of charge, and you may use it and modify it in any of your projects.

SendScheduledSms.pl
use Ozeki::Libs::Rest::Configuration;
use Ozeki::Libs::Rest::MessageApi;
use Ozeki::Libs::Rest::Message;
use DateTime;

my $configuration = new Ozeki::Libs::Rest::Configuration();
$configuration->{ Username } = "http_user";
$configuration->{ Password } = "qwe123";
$configuration->{ ApiUrl } = "http://127.0.0.1:9509/api";

my $msg = new Ozeki::Libs::Rest::Message();
$msg->{ ToAddress } = "+36201111111";
$msg->{ Text } = "Hello world!";
$msg->{ TimeToSend } = DateTime->new(year=>2021,month=>7,day=>2,hour=>16,minute=>0,second=>0)->stringify;

my $api = new Ozeki::Libs::Rest::MessageApi($configuration);

my $result = $api->Send($msg);

print($result->stringify);

How to use the Perl sms example:

This Perl sms example can be used in any Perl application. To use it, you must add the Ozeki.Libs.Rest library to your project. After the library is added, you must put the using Ozeki.Libs.Rest; directive into the header section of your Perl source code. This will allow you to use the classes provided by the Ozeki.Libs.Rest library. You can use the Message class to create the SMS. You can use the MessageApi class to send the scheduled SMS to the SMS gateway. The SMS gateway will forward your message to the mobile network either through a wireless connection or through the Internet.

Download SendScheduledSms.pl

The source code explained in this article can be downloaded and used and modified free of charge.
Download: SendScheduledSms.pl.zip (76.1Kb)

What is in the SendScheduledSms.pl file?

The SendScheduledSms.pl file contains the Ozeki.Libs.Rest library, which gives you all the tools neccessary to send and receive SMS messages. You will also find the SendScheduledSms.pl file in the zip, which contains the example code to show you how to send a scheduled SMS. This example code is listed below.

Figure 2 - What is inside SendScheduledSms.pl.zip

How to send scheduled SMS from Perl (Simple guidelines)

To send scheduled SMS from Perl:

  1. Install HTTP API user
  2. Download the SendScheduledSms.pl.zip file
  3. Extract the .zip file from the Downloads folder
  4. Open the SendScheduledSms.pl file in a text editor
  5. Edit the file to schedule sms from Perl
  6. Launch Ozeki SMS Gateway
  7. Run SendScheduledSms.pl using the command prompt
  8. Check the logs

Install Ozeki SMS Gateway and create an HTTP API user

To be able to send SMS from Perl, first you need to install Ozeki SMS Gateway. The SMS gateway can be installed on the same computer, where you develop your Perl code in Visual studio. After installation, the next step is to connect Ozeki SMS Gateway to the mobile network. You can send a test sms from the Ozeki GUI to verify, that your mobile network connection works. The final step to prepare your environment is to create a HTTP sms api user. Create a user with a username of "http_user", and with a password of "qwe123" to make the example work without modification.

After the environment is setup, you can run your Perl code.

HTTP API url to use send sms from Perl

To send SMS from Perl, your Perl will will have to issue an HTTP request to the SMS gateway. The API url is shown below. Note that the IP address (127.0.0.1) should be replaced to the IP address of your SMS gateway. If Ozeki SMS Gateway is installed on the same computer where the Perl sms application is running, this can be 127.0.0.1. If it is installed on a different computer it should be the IP address of that computer.

http://127.0.0.1:9509/api?action=rest
	

HTTP authentication to use send sms from Perl

To authenticate the Perl sms client, you need to send the username and password in a base64 encoded string to the server in a HTTP request. The format used is: base64(username+":"+password). In Perl you can use the following code to do this encoding:

my ($self, $Username, $Password) = @_;
my $Username_Password = "${Username}:${Password}";
my $Username_Password_encoded = encode_base64($Username_Password);
return "Basic ${Username_Password_encoded}";
	

For example if you encode the username 'http_user' and the password 'qwe123', you will get the following base64 encoded string: aHR0cF91c2VyOnF3ZTEyMw==. To send

HTTP request header to send SMS from C#

To send the SMS messages, you need to include the following lines as headers in the HTTP request. Note that we include a content type and an Authorization header.

Content-Type: application/json
Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==

HTTP request to send scheduled SMS from Perl

To submit the SMS, your Perl application will send an HTTP request similar to the one below. Note, that this requst contains a HTTP header part and a http body part. The HTTP body is a JSON encoded data string. It contains the recipient number and the messages text.

POST /api?action=sendmsg HTTP/1.1
Connection: TE, close
Content-Length: 998
Content-Type: application/json; charset=utf8
Authorization: Basic aHR0cF91c2VyOnF3ZTEyMw==
Host: 192.168.0.14:9509
Te: deflate,gzip;q=0.3
User-Agent: libwww-perl/6.43

{
	"messages": [
		{
			"message_id": "ab4c1e78-db41-11eb-8298-c00c4d4e2ea7",
			"to_address": "+36201111111",
			"text": "Hello world 1",
			"create_date": "2021-07-02T14:27:45",
			"valid_until": "2021-07-09T14:27:45",
			"time_to_send": "2021-07-02T14:27:45",
			"submit_report_requested": true,
			"delivery_report_requested": true,
			"view_report_requested": true,
			"tags": []
		},
		{
	  		"message_id": "ab4c4523-db41-11eb-838d-c00c4d4e2ea7",
	  		"to_address": "+36202222222",
	  		"text": "Hello world 2",
	  		"create_date": "2021-07-02T14:27:45",
	  		"valid_until": "2021-07-09T14:27:45",
	  		"time_to_send": "2021-07-02T14:27:45",
	  		"submit_report_requested": true,
	  		"delivery_report_requested": true,
	  		"view_report_requested": true,
	  		"tags": []
		},
		{
			"message_id": "ab4c59cd-db41-11eb-82c0-c00c4d4e2ea7",
			"to_address": "+36203333333",
			"text": "Hello world 3",
			"create_date": "2021-07-02T14:27:45",
			"valid_until": "2021-07-09T14:27:45",
			"time_to_send": "2021-07-02T14:27:45",
			"submit_report_requested": true,
			"delivery_report_requested": true,
			"view_report_requested": true,
			"tags": []
		}
	]
}

HTTP response received by the Perl sms example

Once the SMS gateway receives this request, it will generate a HTTP response. The HTTP response will contain a status code, to indicate whether the SMS submit request was successful or not. It will also return a JSON encoded structure to provide you useful details about the messages submission.

HTTP/1.1 200 OK
User-Agent: OZEKI 10.3.123 (www.myozeki.com)
Content-Type: application/json; charset=utf8
Last-Modified: Fri, 02 Jul 2021 16:00:35 GMT
Server: 10/10.3.123
Transfer-Encoding: chunked

{
	"http_code": 200,
	"response_code": "SUCCESS",
	"response_msg": "Messages queued for delivery.",
	"data": {
	  "total_count": 1,
	  "success_count": 1,
	  "failed_count": 0,
	  "messages": [
	    {
	      "message_id": "7596081e-db3e-11eb-827e-c00c4d4e2ea7",
	      "from_station": "%",
	      "to_address": "+36201111111",
	      "to_station": "%",
	      "text": "Hello world!",
	      "create_date": "2021-07-02 14:04:47",
	      "valid_until": "2021-07-09 14:04:47",
	      "time_to_send": "2021-07-02 16:10:00",
	      "submit_report_requested": true,
	      "delivery_report_requested": true,
	      "view_report_requested": false,
	      "tags": [
	        {
	          "name": "Type",
	          "value": "SMS:TEXT"
	        }
	      ],
	      "status": "SUCCESS"
	    }
	  ]
	}
}

Perl sms example: SendScheduledSms.pl(Video tutorial)

In this video, you are going to learn about the download process of the SendScjheduledSMS.pl project file. The video will start with downloading the project folder. It will take you to having the full system that you can start using. You will see what you need to modify in the terminal. The video is only 1:36 long but it is very detailed. You will have no problem with following the steps.

Video 1 - How to download and set up the solution above

The example code

The example code below is part of the SendScheduledSms.pl.zip. This code will send scheduled SMS messages to the number stored in the 'ToAddress' variable. The text of the messages will be the text's in the 'Text' variable. Run the code to send all the messages to all the recipients. If you are familiar with the Perl programming language, feel free to modify the code to your liking.

Figure 3 - SendScheduledSms.pl

How to use the code (Video tutorial)

In this video, you are going to learn about the process of running the code and sending SMS messages to the recipient. The video will start with the opened code and will end with the log of the sent messages. At the end of the video, you will see a folder in Ozeki SMS Gateway, titled Postponed messages. In the folder, you will find all the sent messages. The video is only 1 minute long and it guides you through all the steps you need to take to send the postponed messages. The Ozeki SMS Gateway offers a very intuitive user interface, so you will have no problem with following the steps.

Video 2 - How to use the SendMultipleSms.pl file

Summary

The purpose of this article was to show the steps of SMS scheduling in Perl with the help of Ozeki SMS Gateway. This solution enables to postpone messages, which can be really useful when you want to make sure that you don't bother the customers when they are busy. By doing this, you can achieve a customer friendly image and improve efficiency. The clients are more likely to read the messages when they have free time to do that. It also has to be said that Ozeki SMS Gateway allows you to manage SMS costs and to keep track of SMS traffic.

Make sure that the reading is not finished here, visit Ozeki's tutorial page where more information can be found about topics like multiple SMS sending and SMS deleting in Perl.

The only thing to do now is to download Ozeki SMS Gateway and start working!

More information