How to receive SMS from Perl

The most simple way to recevie SMS messages 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 receive sms from perl
Figure 1 - How to receive SMS from Perl

Perl code to receive sms from mobile

The Perl sms code sample below demonstrates how you can receive 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.

ReceiveSms.pl
use Ozeki::Libs::Rest::Configuration;
use Ozeki::Libs::Rest::MessageApi;

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

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

my $result = $api->DownloadIncoming();

print($result->stringify, "\n");

foreach my $message (@{ $result->{ messages } }) {
    print($message->stringify, "\n");
}

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 receive the SMS messages 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 ReceiveSms.pl

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

What is in the ReceiveSms.pl.zip file?

The ReceiveSms.pl.zip 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 ReceiveSms.pl file in the zip, which contains the example code to show you how to receive an SMS. This example code is listed below.

Figure 2 - What is inside ReceiveSms.pl.zip

How to receive SMS in Perl (Easy guidelines)

To receive SMS in Perl:

  1. Download the ReceiveSms.pl.zip file
  2. Extract the .zip file from Downloads
  3. Open the ReceiveSms.pl file in a text editor
  4. Launch Ozeki SMS Gateway
  5. Click on HTTP user in the Users and applications tab
  6. Run ReceiveSms.pl to receive SMS using the command prompt
  7. Check the Inbox in Ozeki SMS Gateway

Install Ozeki SMS Gateway and create an HTTP API user

To be able to send, and receive 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 Receive 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 receive 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 to receive SMS with Perl

To receive 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.

GET /api?action=receivemsg&folder=inbox HTTP/1.1
Connection: TE, close
Content-Type: application/json
Authorization: Basic aHR0cF91c2VyOnF3ZTEyMw==
Host: 192.168.0.14:9509
Te: deflate,gzip;q=0.3
User-Agent: libwww-perl/6.43
	

HTTP request header to receive 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 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": "",
	"data": {
	  "folder": "inbox",
	  "limit": "1000",
	  "data": [
	    {
	      "message_id": "a860555d-db45-11eb-82d5-c00c4d4e2ea7",
	      "from_connection": "http_user@localhost",
	      "from_address": "+36201111111",
	      "from_station": "%",
	      "to_connection": "http_user@localhost",
	      "to_address": "http_user",
	      "to_station": "%",
	      "text": "Hello world 1",
	      "create_date": "2021-07-02 14:56:18",
	      "valid_until": "2021-07-09 14:56:18",
	      "time_to_send": "2021-07-02 14:56:18",
	      "submit_report_requested": true,
	      "delivery_report_requested": true,
	      "view_report_requested": true,
	      "tags": [
	        {
	          "name": "Type",
	          "value": "SMS:TEXT"
	        }
	      ]
	    },
	    {
	      "message_id": "a8607b04-db45-11eb-818e-c00c4d4e2ea7",
	      "from_connection": "http_user@localhost",
	      "from_address": "+36202222222",
	      "from_station": "%",
	      "to_connection": "http_user@localhost",
	      "to_address": "http_user",
	      "to_station": "%",
	      "text": "Hello world 2",
	      "create_date": "2021-07-02 14:56:18",
	      "valid_until": "2021-07-09 14:56:18",
	      "time_to_send": "2021-07-02 14:56:18",
	      "submit_report_requested": true,
	      "delivery_report_requested": true,
	      "view_report_requested": true,
	      "tags": [
	        {
	          "name": "Type",
	          "value": "SMS:TEXT"
	        }
	      ]
	    },
	    {
	      "message_id": "a8609539-db45-11eb-80db-c00c4d4e2ea7",
	      "from_connection": "http_user@localhost",
	      "from_address": "+36203333333",
	      "from_station": "%",
	      "to_connection": "http_user@localhost",
	      "to_address": "http_user",
	      "to_station": "%",
	      "text": "Hello world 3",
	      "create_date": "2021-07-02 14:56:18",
	      "valid_until": "2021-07-09 14:56:18",
	      "time_to_send": "2021-07-02 14:56:18",
	      "submit_report_requested": true,
	      "delivery_report_requested": true,
	      "view_report_requested": true,
	      "tags": [
	        {
	          "name": "Type",
	          "value": "SMS:TEXT"
	        }
	      ]
	    }
	  ]
	}
}

How to receive SMS with Perl using the Perl sms api (Video tutorial)

These videos will show you how to install the Ozeki::Libs::Rest module, and how to use the ReceiveSms.pl example program.The video will show you how you can download the ReceiveSMS.pl project folder, how to set up a system that you can start using. The video is only 1:30 long but it features all the steps you need to take. The tutorial is clear and easy to follow so you will have no problem copying the process.

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

Perl sms example: ReceiveSms.pl

On Figure 3, you can see the source code of the ReceiveSMS.pl. This code will download all the incoming messages from a server and print them out to the screen. You need to provide the login credentials of the server in the 'Username' and the 'Password' variable. If you are familiar with the Perl programming language, feel free to modify the code so it suits your needs perfectly.

Figure 3 - ReceiveSms.pl

How to receive SMS with the Perl code (Video tutorial)

In this video, you will see the code in work. We are going to present you with how to run the code and check its results. It will start with an opened terminal and the opened code and will take you to reading the received messages. The video is only 1 minute long and it is easy to follow. You will have no problem with following the steps.

Video 2 - Receiving SMS with the Perl code above

To sum it up

This article explained, how to receive SMS messages from Perl using the built-in HTTP REST SMS API of the Ozeki SMS Gateway. If you have followed the tutorial carefully, you are now able to create an HTTP user in the Ozeki SMS Gateway and use the given repository to receive SMS messages with the Perl SMS API. This library is provided to you free of charge, and you may use it and modify it in any of your projects.

The more knowledge you have, the more efficient you can be, so keep on reading. Check out the How to download the latest Perl SMS library from Github article, and learn how to use the Perl SMS API more widely.

Download Ozeki SMS Gateway now, and let the work begin!

More information