Automating sms sending by through Way2sms in Perl

4.5k views Asked by At

I am trying to send sms through Way2sms using Perl LWP. The login part is being successful, after which I save the cookies to a local file. The welcome page after being logged in shows a Send SMS link, clicking on which one is redirected to another page with two inputs for mobile number and sms text and a button for submitting and sending the sms. Firebug reveals the page structure as shown in the figure. From the Iframe url and the form's action attribute, I constructed the form action's absolute URL and submit the form accordingly, with the cookie stored in the file. However, the sms isn't sent. What I am doing wrong here? The code is as follows. (The name attributes for the two text inputs are correct, taken by observing the source code in Firebug, although that's not included in the image)

use LWP::UserAgent;
open f, "> way2sms.txt";
use HTTP::Cookies;
my $cookie_jar = HTTP::Cookies->new(
file => "cookies.txt",
autosave => 1,
);

my $ua = LWP::UserAgent->new(
    agent =>
      'Mozilla/5.0 (X11; Linux x86_64; rv:14.0) Gecko/20100101 Firefox/14.0.1',
    cookie_jar => $cookie_jar,
);
my $response = $ua->post(
    'http://site2.way2sms.com/contentt/bar/Login1.action',
    {
        username => $user,
        password => $pass,
    }
);

if ( $response->is_redirect ) {
    $response = $ua->get( $response->header('Location') );
    print 5 if $response->decoded_content =~ /Kaustav Mukherjee/i; #prints it, showing that the login is successful
}   
my $smsresp = $ua->post("http://site5.way2sms.com/jsp/quicksms.action",[MobNo=>$mob,textArea=>'Hello World']);

enter image description here

8

There are 8 answers

12
Prasanth On BEST ANSWER

WORKING SCRIPT: (Tested on 6/Sep/2012 3:00 PM GMT+5:30)

    use LWP::UserAgent;
    use HTTP::Cookies;

    my $ua = LWP::UserAgent->new(agent=>"Mozilla/5.0 (X11; Linux x86_64; rv:14.0) Gecko/20100101 Firefox/14.0.1");

    $ua->cookie_jar({ file => "/absolute/path/to/cookies.txt", autosave => 1 });
    # if windows, use \ instead of /

    # $ua->proxy(['http', 'ftp'], 'http://localhost:9666/');

    # below line seems to have done the trick
    push @{ $ua->requests_redirectable }, 'POST';

    my $response = $ua->post(
            'http://site5.way2sms.com/Login1.action',{
            "username" => "1234567890", # set your username
            "password" => "passwd0123", # set your password
            "userLogin" => "yes",
            "message" => "",
            "mobileNo" => "",
            }
    );

    if($response->is_success && $response->decoded_content =~ /Logout/i){ # you can have your name too in place of Logout
            print "Logged in!\r\n";
    }

    my $mob = "1234567890"; # mobile to send message to
    my $mes = "Hello! 123."; # message

    my $smsresp = $ua->post(
            "http://site5.way2sms.com/quicksms.action",
            {
                    'Action' => 'dsf45asvd5',
                    'HiddenAction' => 'instantsms',
                    'catnamedis' => 'Birthday',
                    'chkall' => 'on',
                    'MobNo' => $mob,
                    'textArea' => $mes,
            });

     if ($smsresp->is_success && $smsresp->decoded_content =~ /Submitted/i) {
         print "Sent!\r\n";
     }

P.S. I wish I could get that expired bounty O_o :)

0
Abhishake Gupta On

An answer here using Python

To send sms using way2sms account, you may use below code snippet.

Before that you would be required to create an API Key from here

import requests
url = "https://smsapi.engineeringtgr.com/send/"
params = dict(
    Mobile='login username',
    Password='login password',
    Key='generated from above sms api',
    Message='Your message Here',
    To='recipient')

resp = requests.get(url, params)
print(resp, resp.text)

N.B: There is a limit of approx 20 sms per day

0
Prashere On

Well

  1. Way2SMS.com is changing the value of hidden parameter 'catnamedis' monthly once.
  2. Way2SMS.com is adding new hidden parameters less frequently, like now they have added new parmeter called 'token' which varies depending on the user, and the same with 'Action' hidden parameter which also varies depending on the user.

So a script works for someone, does not works for other, because of this changing values, You have to manually login into your way2sms.com account in a real browser and inspect element and find the value for Token and Action parameter for your account (this value varies for every user).

Here is my Python Script through which I am able to make it achieve. You can use it as a reference for your Perl program

Note:- Before trying out my script, change the value of the variable 'act' in the script according to the value of Action parameter for your account, that you can find with the inspect element or Firebug.

3
Fayland Lam On

I don't have your login info so that I can't test it for you.

but you can use some Firefox addon like TamperData or HttpFox to get which url is posted and what params are sent.

use Perl to make the same requests as browser and that will be good.

BTW, you can use one $ua to send two requests, no need to create another LWP::UserAgent instance.

Thanks

8
capfan On

You don't have to set up a new user agent, because you can simply reuse the previous one. AFAIK, the cookies will be in that user agent already.

Please note: the following is slightly OT.

I had a look at https://metacpan.org/pod/Net::SMS::WAY2SMS

It installed fin on my pc. Maybe you want to try it again? Open a new question here with your concrete problem. Please feel free to notify me, so we may have a look at it.

2
Ashish Kumar On

Have you tried using the Perl module Net::SMS::WAY2SMS for sending messages through way2sms.com? It works great for me.

To install try:

C:\> perl -MCPAN -e "install Net::SMS::WAY2SMS"

Here is a code sample for sending SMS:

use strict;
use warnings;
use Net::SMS::WAY2SMS;

my $sms = Net::SMS::WAY2SMS->new(
    'user' => 'user_name' ,
    'password' => 'secret_password',
    'mob' => ['1234567890', '0987654321']
);

# multi line sms
$sms->send(q[testing
sending
sms]);
0
Jon On

Can't really see what's wrong but here are the things I'd start looking at:

  • Check your real post request from the browser. Make sure javascript or something else isn't adding a variable not visible in the html.
  • Tripple check that MobNo and textArea are really the only needed parameters. Often the submit button is checked for instance, to see what action was done.
  • See what output you get when doing your post. Does it say something about a failure?
  • Try to make the post directly from your browse (there are FF plugins for that) to make sure you're not busted on the lack of http-referrer

I've done a lot of scraping/pretending to be a browser and it's often very tedious. People add "security" checks. On common is that require you to post back a session variable for instance.

Good luck!

*edit: The site requires you to have a valid +91 number to register so I can't login and try for myself. Considering the amount of ads on that page do I really think they are adding some sort of variable since they live off ads and you're trying to avoid them.

4
Anirudh Ramanathan On

You are not including all the POST fields required

MobNo=
textArea=
HiddenAction=instantsms
login=
pass=
Action=

These all need to be included. There is this script in PHP that definitely works and you can refer to.