Introduction
As the title says this post describes how to send SMS messages from GO mobile website www.go.com.mt/mygo/ programmatically with C#. This is for educational purposes only and I’m not affiliated in any way to GO. Use the contents of this page at your own risk.
Requirements:
- GO Mobile phone line
- My Go account for the phone line (Free for every GO Mobile phone)
Tools Used:
Sending an SMS manually
Browse www.go.com.mt/mygo/
To send an SMS a user first has to login with his mobile number and password as shown in Fig:1
If the login is successful the user is given access to the form in Fig:2 that takes recipients and a maximum of 420 characters in an SMS. When finished typing the desired SMS click send SMS button.
How to achieve the above programmatically?
To send an SMS there are needed two POSTs, one for Login and the other for the actual SMS to be sent. So a method for sending a POST over HTTPS was created.
public static void httpsPost(String uri, String post_data, String referer, CookieContainer cookieC){
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.ProtocolVersion = HttpVersion.Version11;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; rv:5.0) Gecko/20100101 Firefox/5.0";
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
request.KeepAlive = true;
request.ReadWriteTimeout = 300;
request.Referer = referer;
request.CookieContainer = cookieC;
byte[] postBytes = Encoding.ASCII.GetBytes(post_data);
request.ContentLength = postBytes.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(postBytes, 0, postBytes.Length);
requestStream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
}
Parameters:
Uri – is the url the POST is sent.
Post_data – is the actual data that post will carry
Referrer – is the previous url the post request originated.
cookieC –keeps the cookie the Web Server sends to the browser to maintain the Session state. This is so that to keep the program logged to your My Go account between login and sending a SMS message.
To make sure the Web Server sends the same HTML code that it sends to the Web Browser, the UserAgent is spoofed to the same as the Web Browser used in this case FireFox 5.
Login Form
<form name="my_go_login" method="post" action="./servlet/authorisation">
<input name="msisdn" class="login-screen" type="textbox">
<input name="password" class="login-screen" type="password">
<input type=hidden name=actionx value="authenticatemain">
<INPUT class="login-button" TYPE="image" src="./images/login_screen/submit_button.jpg">
</form>
Note: the HTML code was cleared from irrelevant tags for clarity. The tags removed include table for formatting and client side validation on the form data.
From the HTML code at https://www.go.com.mt/mygo/ we investigate the login form.
referrer – https://www.go.com.mt/mygo/
url – https://www.go.com.mt/mygo/ servlet/authorisation
msisdn – mobile number to login with
password – the password for the account
actionx – a hidden input which is always set to the value “authenticatemain”
Send SMS form
<form name="smsform" action="./servlet/groupsms" method="post">
recipients:
<input type="text" name="phone_entries" id="phone_entries"/>
message:
<textarea name="smsInput"></textarea>
<input type="hidden" name="pentries" values=""/>
characters left:
<input readonly name="count" value="420 (0 SMS)">
<input class="transparent" type="button" value="send SMS" onClick="submitsmsform()">
</form>
This form is POSTed to the server by AJAX which requires more time to figure out what is really going on.
//identifies browser being used and then connects to servelet to retrieve data
function TxRxXML(inputText)
{
if(inputText != "" && inputText != "," && inputText != " ")
{
//if IE
if(window.ActiveXObject)
{
request = new ActiveXObject("Microsoft.XMLHTTP");
}
//if other browsers
else if(window.XMLHttpRequest)
{
request = new XMLHttpRequest();
}
//event handler that fires when the state of the page changes
request.onreadystatechange = processRequestChange;
request.open("POST", "./servlet/ajaxwebsms?rand=-4974711596692347436", true );
request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
request.send("phone_entries=" + inputText);
}
return false;
}
Development becomes easier with the use of Temper Data which empowers the developer to pause and inspect requests and their content. With some manual SMSes testing the query string was figured out.
url – https://www.go.com.mt/mygo/servlet/groupsms
referrer – https://www.go.com.mt/mygo/main_right.jsp
phone_entries – target mobile number
smsInput – url encoded message
message_template= left blank (not needed unless using the sms template facility)
count – Just a string that holds the number of characters left and in how many SMSes the message need to be split.
int len = 420 – message.Length; (message length is prior url encoding)
if (len > 279) num_of_smses = 1;
else if (len > 147) num_of_smses = 2;
else if (len > 11) num_of_smses = 3;
else if (len > 0) num_of_smses = 4;
count=” + len.ToString() + “+%28″+num_of_smses.ToString()+”+SMS%29&
save_message– save or not the message on the account (on/off)
pentries– target mobile number
So to Login and Send an SMS we get the following:
public static void sendSms(String mobile_Number, String password, String recipient, String message)
{
CookieContainer myCookie = new CookieContainer();
/* login */
String url = "https://www.go.com.mt/mygo/servlet/authorisation";
String referer = "http://www.go.com.mt/mygo/";
// Create Login Query String
String vars = msisdn="+mobile_Number+"&password="+password+"&actionx=authenticatemain";
// Login
httpsPost(url, vars, referer, myCookie);
/* send sms*/
url = "https://www.go.com.mt/mygo/servlet/groupsms";
referer = "https://www.go.com.mt/mygo/main_right.jsp";
// 420 is the maximum number of characters that can be sent at once
int len = 420 - message.Length;
// apply URL Encoding to the message
message = HttpUtility.UrlEncode(message);
// Calculate the number of SMSes to be sent
int num_of_smses = 1;
if (len > 279) num_of_smses = 1;
else if (len > 147) num_of_smses = 2;
else if (len > 11) num_of_smses = 3;
else if (len > 0) num_of_smses = 4;
else throw new Exception("You have exceeded the limit of 420 Characters");
// Create SMS Query string
vars = "phone_entries=" + recipient + "%2C&smsInput="+message+"&message_template=&count=" + len.ToString() +"+%28"+num_of_smses.ToString()+"+SMS%29&save_message=on&pentries=" + recipient + "%2C";
// Send SMS
httpsPost(url, vars, referer, myCookie);
}
Parameters:
mobile_number – the mobile number representing the account
password – password for the account
recipient – the number of the target mobile phone to receive the SMS
message – the actual SMS message in plain text
To make sure the Web Server sends the same HTML code to the application the UserAgent is set to the same as the Browser used in this case FireFox 5.
Test it
static void Main(string[] args)
{
sendSms("YOUR_NUMBER", "PASSWORD", "NUMBER_TO", "ACTUAL MESSAGE");
}
The code was tested in Visual Studio 2010 in a Console Application the Target Framework was changed to .NET Framework 4 as shown in Fig:3 so as to be able to include the reference to System.Web shown in fig:4.
Make sure the following name spaces are included
using System.Text;
using System.Net;
using System.IO;
Final note
If multiple SMS are to be sent, one could separate login from sending SMS but keep in mind that the session times out after being idle for some time.
What is not included:
Error Handling.
Message Templates.
An explanation of how to send to multiple recipients. This is left for the reader to try out. (probably just separate the numbers with coma)
How to access and retrieve My Go phone book and how to add entries to it programmatically.
Between login and sending an SMS from the website a real web browser sends some GET/POST requests for autocomplete mobile numbers in the phonebook stored in the account. These are skipped entirely.
Does not cater beyond the 8 free daily SMSes that require confirmation from the user to charge the account for the SMS.