Receive SMS in Perl

On this page, you will be able to learn about how you can use your Perl application to receive SMS messages. This example Perl source is capable of collecting the incoming messages from the SMS Gateway and use them in the application. It performs this operation by using HTTP request. This guide shows how easy you can create a Perl application and run it on your computer. So, let's start right now.

What is a Perl SMS API?

The Perl SMS API ensures the ability to send SMS messages from a program written in Perl. It uses HTTP requests and SSL encrypted communication to forward the SMS sending request to the SMS Gateway.

Prerequisites

Receive SMS in Perl

  1. Open Notepad
  2. Copy-Paste the example code from this page
  3. Save the file as ReceiveSMS.pl
  4. Open Ozeki SMS Gateway and select HTTP Server connection
  5. Open the HTML form of the HTTP Server connection
  6. Send some test messages
  7. Open Command Prompt
  8. Type 'perl ReceiveSMS.pl' in the Command Prompt and press Enter

Video 1 - How to receive SMS in Perl (Video tutorial)

This example code below is free to use in your own solution or application and you can modify it if you want to. The example code can collect the messages from the SMS Gateway and use it in the Perl application. If you would like to test the example just follow the step-by-step instructions below or check the video which clearly shows all steps that you need to perform to execute the Perl SMS example.

#!/usr/bin/perl 
use strict; 
use warnings; 
use XML::Simple;
use Data::Dumper;
require HTTP::Request;
require LWP::UserAgent;

sub main
{
    my $username = "john";
	my $password = "Xc3ffs";
	my $httpUrl = "https://127.0.0.1:9508/";
	my $folder = "inbox";
	my $limit = "3";

    my $sendString = $httpUrl."api?action=receivemessage&username="
                    .$username."&password=".$password."&folder="
                    .$folder."&limit=".$limit
                    ."&messagedata=&afterdownload=delete";
    
    print "Sending html request: ".$sendString."\n\n";

	my $request = HTTP::Request->new(GET => $sendString);
	my $ua = LWP::UserAgent->new (
   			ssl_opts => { verify_hostname => 0 },
			);
	my $response = $ua->request($request);
	print "Http response received :\n";

	DisplayMessages($response->content)
}

main();

sub DisplayMessages {
	my $data = XMLin(@_);
	my $sender = "";
	my $text = "";
 	if($data->{data}->{message} eq 'No more messages.')	{
 		print "The inbox is empty!";
 		return;
 	}

	if (ref($data->{data}->{message}) eq 'ARRAY') {
		foreach my $value (@{ $data->{data}->{message} }) {
			$sender = $value->{originator};
			$text =  $value->{messagedata};
			DisplayMessage($sender,$text);
		}
	} 
	else {
		$sender = $data->{data}->{message}->{originator};
		$text = $data->{data}->{message}->{messagedata};
		DisplayMessage($sender,$text);
	}
}

sub DisplayMessage {
	print $_[0].": ".$_[1]."\n";
}

Step 1 - Open Notepad

The first thing that you have to perform to complete this guide is to create the Perl file that you have to execute to see how it gets the SMS messages from the SMS Gateway. So, at this point, you need to open the Notepad application on your computer to be able to create the Perl file. For that, just like in Figure 1, click on its icon on the desktop.

open notepad
Figure 1 - Open notepad text editor

Step 2 - Copy the Perl source code from this page

Next, you need to get the Perl source code from this page to use it on your computer. So, just scroll up to the example code section and mark out the whole source code. Then, as you can see it in Figure 2, you need to copy it to your clipboard. This can be done by pressing Ctrl+C on your keyboard.

copy perl code
Figure 2 - Copy perl source code from the website

Step 3 - Paste the code into the text file

After you copied the Perl source code from this page, you need to place it into a text document. To do this, just go to the Notepad application that you opened before, and as you can see it in Figure 3, just press Ctrl+V on your keyboard to paste the whole source code into the text document.

paste perl sms source code
Figure 3 - Paste Perl SMS source code to notepad

Step 4 - Save the text as a Perl file

The source code is now placed into the text document, but it still hasn't got the right file format. But this is now a huge problem, you can solve it easily with Notepad. Just select the 'Save as' option, and here, after you gave a name to the file, use the '.pl' file extension as Figure 4 demonstrates it. If you click 'Save', the file will be saved in the right Perl file format that you need to execute later.

save perl file
Figure 4 - Save file as ReceiveSMS.pl

Step 5 - Send some test messages

After you set up the Perl file, you need some message to your inbox folder. To finish the test quickly, you can simulate incoming messages with the HTTP Server connection in SMS Gateway. If you haven't configured this connection yet, here you can see how to configure a HTTP Server connection. So, open SMS Gateway, and select the HTTP Server connection. In the menu of the connection, open the HTML form (Figure 5). Here, you can write some messages to the SMS Gateway by setting the recipient to 'Ozeki'.

simulate incoming sms
Figure 5 - Simulate some incoming SMS

Step 6 - Open Command Prompt

To run the Perl code, you need to Command Prompt since it provides a simple way to execute the code. So, first open the File Explorer and navigate to the folder, where you saved your Perl file. Here, as you can see it in Figure 6, type 'cmd' into the File Explorer. If you hit Enter, and Command Prompt opens in a new window.

Figure 6 - Open Command Prompt

Step 7 - Run the Perl SMS example

The last thing that you have to do is to execute the Perl program. For that, you just need to execute a simple command in Command Prompt. The command is 'perl *filename*.pl' as Figure 7 shows that. Then, just press Enter to execute the example program. The program prints the HTTP request that was sent to the SMS Gateway, and also prints the response message from the SMS Gateway. This contains all messages that was received by the SMS Gateway.

run perl code
Figure 7 - Run perl code in command prompt

Final Thoughts

In the article above, you have learned about receiving SMS using Pearl. Using a Pearl script to collect incoming messages means that you can use those messages in a PHP project. You can start automating the SMS receiving and sending with the power of Pearl and Ozeki SMS Gateway. Thanks to the Ozeki SMS Gateway, you will have no performance issues due to the 1000 SMS per second capability of gateway software. This will greatly improve communication within your organization or with your customers.

Visit our webpage if you want to learn about all the programming languages that the Ozeki SMS Gateway can handle. You can reach the article about managing SMS with PHP or Python.

If you want to work with a high-performance SMS Gateway software, download the Ozeki SMS Gateway!