How to Delete an SMS in Perl

The simplest way to delete 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 delete an sms in perl
Figure 1 - How to Delete an SMS in Perl

Perl code to recive sms from mobile

The Perl sms code sample below demonstrates how you can recive SMS using the http rest sms api of Ozeki SMS Gateway 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.

DeleteSms.pl

use Ozeki::Libs::Rest::Configuration;
use Ozeki::Libs::Rest::MessageApi;
use Ozeki::Libs::Rest::Message;
use Ozeki::Libs::Rest::Folder;

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();
#You have to change this ID attribute to delete a certain message
$msg->{ ID } = "19fed2a2-da46-11eb-8339-ffacbeab4160";

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

my $result = $api->Delete(Ozeki::Libs::Rest::Folder->Inbox, $msg);

print($result);
	

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 delete 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 DeleteSms.pl

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

What is in the DeleteSms.pl.zip file?

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

Figure 2 - What is inside DeleteSms.pl.zip

How to delete SMS using Perl (Quick steps)

To delete SMS using Perl:

  1. Download the DeleteSms.pl.zip file
  2. Extract the .zip file from Downloads
  3. Open the DeleteSms.pl file in any text editor
  4. Launch Ozeki SMS Gateway
  5. Select http_user in the Users and applications tab
  6. Check the Message details in the Inbox
  7. Insert the ID of the SMS message into DeleteSms.pl code
  8. Run DeleteSms.pl to delete SMS with Perl

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 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 delete SMS with Perl

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

POST /api?action=deletemsg HTTP/1.1
Connection: TE, close
Content-Length: 153
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

{
	"folder":"inbox",
	"message_ids":	[
		"da837770-e3bd-4970-a824-579423d9b242"
	]
}
	

HTTP request header to delete SMS from Perl

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 message's 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",
	  "message_ids": [
	    "da837770-e3bd-4970-a824-579423d9b242"
	  ]
	}
}
	

How to download the code (Video tutorial)

In this video, we are going to show you how you can download the project files from this tutorial page. The video will start with finding the download link and will end with the code ready to use. You will learn what commands to type in the terminal to prepare it for running the code. The video is only 1:34 long and very detailed. You will have no problem with following the steps.

Video 1 - How to download and run the example solution (Video tutorial)

Perl sms example: DeleteSms.pl

The example code below is part of the DeleleSms.pl (Figure 3). When you run this code, it will delete a message with the ID provided in the code. You can find the provided ID in the 'ID' variable. Run the code and it will search for the message and delete it. If you are familiar with the Perl programming language, feel free to modify the code.

Figure 3 - DeleteSms.pl

Using the 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 show you the empty inbox folder at the end. The video is only 1:14 long and it is easy to follow. You will have no problem with following the steps.

Video 2 - How to delete SMS with the Perl code above (Video tutorial)

Summary

On this page, you read about how to delete SMS messages from Perl with the HTTP REST SMS API of the Ozeki SMS Gateway. Now, you can create the conditions for using the downloadable Perl repository to delete SMS messages. This solution can be a game-changer in your messaging system due to the well-written method calls and a well-designed SMS class that allows you to configure the SMS parameters.

The Ozeki SMS Gateway has a lot more in store for you, so go and jump into another intriguing article. I recommend the How to download the latest Perl SMS library from Github guide for more.

Download Ozeki SMS Gateway and set up your system now!

More information