Skip to content

Instantly share code, notes, and snippets.

@jamielaundon
Last active August 29, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamielaundon/6aed9ca9dfc8568e0327 to your computer and use it in GitHub Desktop.
Save jamielaundon/6aed9ca9dfc8568e0327 to your computer and use it in GitHub Desktop.
An example class to send SMS text or synthesised voice calls using AQL.com APIs
/*
* AQL Messaging Class Example
* Jamie Laundon
* https://jamie.laundon.org
* Shouty Disclaimer:
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* This uses the excellent messaging APIs: http://www.aql.com/sms/developers/
* Not affiliated in any way with AQL.
*
* Example usage:
* //Send a text:
* AQL.MessagingAPI message = new AQL.MessagingAPI("myAQLusername", "myAQLpassword", AQL.messageType.SMS, "447977123456", "Hello this is a test", "Jamie Ltd");
* //Receive a voice call with speech synthesis:
* AQL.MessagingAPI message = new AQL.MessagingAPI("myAQLusername", "myAQLpassword", AQL.messageType.Voice, "447977123456", "Warning. Server is offline");
* message.Send();
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace AQL
{
//Enum of message types
public enum messageType
{
SMS,
Voice
}
class MessagingAPI
{
//AQL API URLs
private static string smsURL = "https://gw.aql.com/sms/sms_gw.php";
private static string voiceURL = "https://vp1.aql.com/voice_push.php";
//private variables
public string username { get; set; }
public string password { get; set; }
public string number { get; set; }
public string message { get; set; }
public string originator { get; set; }
public bool flash { get; set; }
public messageType type { get; set; }
public MessagingAPI()
{
//Default Constructor
}
//bare minimum for either mode
public MessagingAPI(string username, string password, messageType type, string number, string message)
: this(username, password, type, number, message, null, false)
{ }
// flash mode
public MessagingAPI(string username, string password, messageType type, string number, string message, bool flash)
: this(username, password, type, number, message, null, flash)
{ }
//with originator
public MessagingAPI(string username, string password, messageType type, string number, string message, string originator)
: this(username, password, type, number, message, originator, false)
{ }
//with all options
public MessagingAPI(string username, string password, messageType type, string number, string message, string originator, bool flash)
{
this.username = username;
this.password = password;
this.type = type;
this.number = number;
this.message = message;
this.originator = originator;
this.flash = flash;
}
public string Send()
{
string result = "";
try
{
StringBuilder URLbuilder = new StringBuilder();
switch (this.type)
{
case messageType.SMS:
URLbuilder.Append(smsURL);
URLbuilder.Append("?");
URLbuilder.Append("username=").Append(this.username).Append("&");
URLbuilder.Append("password=").Append(this.password).Append("&");
URLbuilder.Append("destination=").Append(this.number).Append("&");
URLbuilder.Append("message=").Append(System.Web.HttpUtility.UrlEncode(this.message));
//Optional extras
if (this.originator != null)
URLbuilder.Append("&originator=").Append(System.Web.HttpUtility.UrlEncode(this.originator));
if (this.flash == true)
URLbuilder.Append("&flash=1");
break;
case messageType.Voice:
URLbuilder.Append(voiceURL);
URLbuilder.Append("?");
URLbuilder.Append("username=").Append(this.username).Append("&");
URLbuilder.Append("password=").Append(this.password).Append("&");
URLbuilder.Append("msisdn=").Append(this.number).Append("&");
URLbuilder.Append("message=").Append(System.Web.HttpUtility.UrlEncode(this.message));
break;
default:
break;
}
string URLstring = URLbuilder.ToString();
// Create a request to the API
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(URLstring);
req.Method = "POST";
req.KeepAlive = false;
req.ContentType = "application/x-www-form-urlencoded";
//Get response
using (WebResponse response = req.GetResponse())
{
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
// Read the result from the web server
result = reader.ReadToEnd();
}
}
}
catch (Exception ex)
{
result += ex.Message;
}
return result;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment