Free sending SMS from your megaphone number using a python script
What is it for?
It is worth considering two levels of using this feature:
- The usual, everyday need to send a relatively long message (unless of course you have the same print speed on the phone as on the computer)
- An unusual, geeky need to receive mailing information where there is no computer, mixed with unwillingness to pay extra money for the delivery of this information
Common use
So, most likely, many came across a situation when it was necessary to write a relatively long SMS message:
- copy-paste the necessary information to someone
- describe in detail any details of the meeting when your interlocutor has no opportunity to talk
- well, or to write something banal in the spirit of those same SMS-ok: 3
Unusual use
Here, the use cases are limited only by your imagination and ability to parse the necessary information flows, for example:
- weather forecast
- rss feed headers
- twitter feed
- your server load data
- ?????
- PROFIT!
Choosing a service to send
Immediately I hasten to warn that everything was initially done for myself, so the solution 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, therefore the sendsms.megafon.ru service is not suitable, all the more you need to enter captcha, and how to display it in the console? And if you also love the framebuffer , how do I love it? And if you take into account the recent incident with indexed SMSs?
Fortunately, the megaphone has a Service Guide subscriber self-service system, which I used for a long time precisely for these purposes, and there are other useful functions there, and https security inspires some confidence.
Relatively recently, at the entrance to 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’ll answer to those who are perplexed why write a console script instead of accessing the site and pressing three buttons: “enter” → “menu: send SMS” → “item: send SMS”:
- I would like to have a direct link to the submit form
- javascript is not enabled by default in text browsers
- the script can be used not only for normal use, but also to automate mailing.
Service Guide website analysis
We go to the service guide page and see the following:
- how to get, change, recover password
- special notes for users of the megaphone-internet tariff
- how to get a link to the Java application Service Guide for the phone
The last point seemed very attractive to me, especially since they promise not to charge traffic, but, unfortunately, it was not possible to install on my relatively ancient Samsung SGH-E200 phone due to the large volume of the application (it didn’t work to throw out extra files either).
We proceed to the analysis of the login form on the site. I use Mozilla Firefox and Firebug , Google Chrome has its own tools for this purpose.
We open the “Console” tab, turn on the display of AJAX requests, click the “Do not clear” button (so that when redirecting the page these same requests are not lost) and try to go to the site.
POST request:
www.serviceguide.megafonmoscow.ru/ps/scc/php/check.php?CHANNEL=WWW&LOGIN=ваш_логин&PASSWORD=ваш_парольAnswer: Everything seems to be clear, we easily get SESSION_ID.
AAAAkljkLFJ9JKJk3fowmcoOW3fds
1
U Script writing
An attentive reader will notice that when redirecting another POST request should be executed:
www.serviceguide.megafonmoscow.ru/SCWWW/ACCOUNT_INFO?CHANNEL=WWW&P_USER_LANG_ID=1&SESSION_ID=AAAAkljkLFJ9JKJk3fowmcoOW3fdsHere the answer is already much more complicated:
...
...
I didn’t have much desire to parse this format, so I used the familiar xpath and python-lxml (here is a good example of use ).
Suppose we get the data on the account balance:
The code was intentionally written without checking for correct execution, so as not to clutter up 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 balanceNow go 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_resultIt’s easy to write scripts that will send you (or someone else) SMS with the information you need: twitter feed, rss, weather forecast and more. Attaching this to a schedule or to any events is also not difficult.
Summarizing
Of course, many of the tricks used in the script fragments are not examples of good code. It might be worth using an ssl connection instead of just 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 phone’s keyboard, and also provide a tool for automatic SMS messages with newsletter information.
The plans are to write an article about the script for console viewing of the number of train tickets from the Russian Railways website. In principle, you can easily connect these two scripts: send an SMS notification that the tickets you need appeared.
UPD:
Thanks for the valuable comments, brief summary:
- in some regions, sending SMS through a service guide is paid, be careful
- in some places you need to enter captcha, maybe somewhere you can’t remove it, somewhere you can use such tricks:
- “Setting up a service guide” -> “Automatic access to systems” - removes captcha for some
- change user agent to iPhone / Android and go to the service guide - redirects to another page where there is no captcha
Comparison of shipping methods:
| Way | Cost | room | Note |
|---|---|---|---|
| Service Guide | Is free | Own | Not for all regions for free, but in some places the captcha remained |
| SMS + | 2 rub / day | Own | No more than 70 SMS per day, but there are other useful services there. |
| PHP library for the Megaphone SOAP API | Depends on your tariff | Own | Thanks for the library synchrone |
| GSM-modem | Depends on your tariff | Own | For Arduino lovers, a link from Prometheus |
| Via mail server | Is free | Other | Not used, information from the words interrupt_controller |