📜 ⬆️ ⬇️

Free SMS sending from your megaphone number using a python script

What is it for?


It is worth considering two levels of using this feature:
  1. The usual, household need to send a relatively long message (unless of course you have the same typing speed on the phone as on the computer)
  2. Unusual, geeky need to receive mailing information where there is no computer, mixed with unwillingness to pay extra money for delivering this information

Normal application

So, for sure, many were faced with a situation when it is necessary to write a relatively long SMS message:

Unusual use

Here, use options are limited only by your imagination and skills to parse the necessary information flows, for example:


Select service to send


Immediately I hasten to warn you that initially everything was done for myself, so the decision is tied to the Moscow megaphone, but for other regions the megaphone also has similar services, I hope for other operators.

Obviously, for domestic use it is extremely necessary to send messages from your own number and without advertising, so the service sendsms.megafon.ru does not fit, the more you need to enter a captcha, and how to show it in the console? And if you also like framebuffer , how do I love him? And if you take into account the recent incident with prompted SMS?

Fortunately, the megaphone has a Service-Guide subscribers self-service system , which I used for these purposes for quite a long time, and there are other useful functions there, and https security inspires some confidence.
')
Relatively recently, when entering the service guide, they stopped offering to introduce captcha, which made me think about using this site for my “dirty” purposes.

Just in case, I will answer those who are wondering why to write a console script instead of entering the site and pressing three buttons: “enter” → “menu: sending sms” → “item: sending sms”:

Service Guide Site Analysis


Go to the service guide page and see the following:

The last point seemed to me very attractive, especially since the traffic promised not to charge, but, unfortunately, it was not possible to install my relatively old Samsung SGH-E200 phone due to the large volume of the application (I didn’t throw out the extra files either).

We proceed to the analysis of the form of entry to the site. I use Mozilla Firefox and Firebug , Google Chrome has its own toolkit for this purpose.

Open the "Console" tab, turn on the display of AJAX requests, click the "Do not clear" button (so that when you redirect the page, these same requests are not stuck) and try to access the site.
POST request:
www.serviceguide.megafonmoscow.ru/ps/scc/php/check.php?CHANNEL=WWW&LOGIN=_&PASSWORD=_
Answer:
<SESSION_ID>AAAAkljkLFJ9JKJk3fowmcoOW3fds</SESSION_ID>
<LANG_ID>1</LANG_ID>
<AUTH_MODE>U</AUTH_MODE>


It seems everything is clear, we easily get SESSION_ID.

Script writing


The attentive reader will notice that another POST request must be executed when redirecting:
www.serviceguide.megafonmoscow.ru/SCWWW/ACCOUNT_INFO?CHANNEL=WWW&P_USER_LANG_ID=1&SESSION_ID=AAAAkljkLFJ9JKJk3fowmcoOW3fds
Here the answer is much more complicated:
...
<text-phrase xmlns: xalan = " xml.apache.org/xalan " id = "js-dict-form"> </ text-phrase>
...
I didn’t have much desire to parse this format, so I used the xpath and python-lxml that I already know (this is a good example of use ).

Suppose we get the data on the balance on the account:

The code is intentionally written without checking for correct execution so as not to clutter the post with a bunch of try-except.

 #!/usr/bin/python import urllib from lxml import etree from StringIO import StringIO def get_root(url, params): params = urllib.urlencode(params) raw_response = urllib.urlopen(url, params).read() parser = etree.HTMLParser() tree = etree.parse(StringIO(raw_response), parser) root = tree.getroot() return root login='  ' password=' ' login_url="https://www.serviceguide.megafonmoscow.ru/ps/scc/php/check.php?" login_params={'CHANNEL':'WWW', 'LOGIN':login, 'PASSWORD':password} login_root=get_root(login_url,login_params) session_id = login_root.find(".//session_id").text lang_id = login_root.find(".//lang_id").text print 'session_id=',session_id account_info_url="https://www.serviceguide.megafonmoscow.ru/SCWWW/ACCOUNT_INFO?" account_info_params={'CHANNEL':'WWW', 'P_USER_LANG_ID':lang_id, 'SESSION_ID':session_id} account_info_root=get_root(account_info_url,account_info_params) balance=account_info_root.find(".//div[@class='balance_good td_def']").text print balance 


Now we proceed directly to sending SMS:


#
 prefix='926' print 'prefix=', prefix addr=raw_input('number (7 digits: 1234567): ') msg=raw_input('message: ') msg=msg.decode('utf8').encode('cp1251') sms_url="https://www.serviceguide.megafonmoscow.ru/SCWWW/SUBS_SEND_SMS_ACTION?" sms_params={'CUR_SUBS_MSISDN': login, 'prefix': prefix, 'addr': addr, 'CHANNEL': 'WWW', 'SESSION_ID': session_id, 'SUBSCRIBER_MSISDN': login, 'MSISDN_TO': prefix+addr, 'P_USER_LANG_ID': lang_id, 'MESSAGE': msg} sms_root=get_root(sms_url,sms_params) divs=sms_root.findall(".//div") for div in divs: print div.text.strip() 


Well, it would be nice to log out:

 #        logout_params={'CHANNEL':'WWW', 'CUR_SUBS_MSISDN':login, 'P_USER_LANG_ID':lang_id, 'SESSION_ID':session_id, 'SUBSCRIBER_MSISDN':login} logout_url="https://www.serviceguide.megafonmoscow.ru/SCWWW/CLOSE_SESSION?" logout_root=get_root(logout_url,logout_params) logout_result = etree.tostring(logout_root, pretty_print=True, method="html") print logout_result 


It is easy to write scripts that will send you (or someone else) sms with the information you need: twitter tape, rss, weather forecast and so on. Tying it to the schedule or any events is also not difficult.

Summarizing


Of course, many of the techniques used in the script snippets are not examples of good code. It may be worth using an ssl connection instead of a simple urllib.urlopen (). It is possible that you should not completely translate the page into an etree object, but try to deal with xmlns: xalan. But one of the goals of the article is to show that there is a convenient way to send SMS without typing it on the telephone keypad, and also to provide a tool for automatic SMS messages with sending information.

There are plans to write an article about a script for console viewing the number of tickets for trains from the Russian Railways site. In principle, it is easy to make a connection between these two scripts: send an SMS notification that the tickets you need have appeared.

UPD:
Thanks for the valuable comments, a brief summary:

Comparison of shipping methods:
WayCost ofroomNote
Service GuideIs freeYourNot for all regions for free, but in some places more captcha remained
SMS +2 rub / dayYourNo more than 70 sms per day, but there are other useful services
PHP-library for Megafon SOAP APIDepends on your fareYourThanks for the synchrone library
GSM-modemDepends on your fareYourFor fans of Arduino link from Prometheus
Via mail serverIs freeOtherNot used, information from the words interrupt_controller

Source: https://habr.com/ru/post/124457/


All Articles