Monday 19 December 2011

Sending SMS via AQL's SOAP gateway using vb.net

I'm working on a project currently and a requirement I have is for Systems Center Operations Manager to send certain alerts as an SMS message, SCOM has this functionality built in but requires a GSM modem connected to the RMS in order to do this. We currently use AQL to provide an SMS gateway service for another project so this looked like a good starting point as we were already paying for it. A quick email to AQL resulted in an apology from AQL saying they didn't have any SDK samples and would add it to their list, but they did send some sample code in C# which I was able to knock into some vb.net code (C# isn't my strong point - I could argue the same about vb.net!) Anyway the following should do the trick if you need it (You need to add a web service reference to the aql gateway http://gw.aql.com/soap/sendservice.php?WSDL - Web Service, Not a service Reference, Click Advanced under service reference and name it AQLSoapGateway. At the moment this will just build you a command line utility that can be used for sending SMS, I'll cover getting SCOM to utilise this later.
Imports System
Imports SendSMS.AQLSoapGateway
Imports System.Web.Services.Protocols


Module Module1

    Public Sub Main()

        ' AutoIncrement done with http://autobuildversion.codeplex.com

        'Display Info
        Console.WriteLine(vbCrLf & _
                "SendSMS.exe - SMS Sending Utility, using the AQL SMS Gateway. Version " & My.Application.Info.Version.ToString & vbCrLf & _
                "David Wallis, 2011" & vbCrLf)

        'Define Usage Message
        Dim strUsageMsg As String = vbCrLf & _
                "Usage: SendSMS.exe " & vbCrLf & vbCrLf & _
                "Available Options: " & vbCrLf & vbCrLf & _
                "/Username:AQLUserName" & vbCrLf & _
                "/Password:AQLPassword" & vbCrLf & _
                "/DestinationNumber:PhoneNumber" & vbCrLf & _
                "/Originator:Originator" & vbCrLf & _
                "/Message:Message" & vbCrLf

        Dim strDestinationNumber As String = "", strUserName As String = "", strPassword As String = "", strOriginator As String = "", strMessage As String = ""

        ' Match Command Line Arguments with variables
        For Each arg As String In My.Application.CommandLineArgs

            If arg.ToLower.StartsWith("/destinationnumber:") Then strDestinationNumber = arg.Remove(0, 19)
            If arg.ToLower.StartsWith("/username:") Then strUserName = arg.Remove(0, 10)
            If arg.ToLower.StartsWith("/password:") Then strPassword = arg.Remove(0, 10)
            If arg.ToLower.StartsWith("/originator:") Then strOriginator = arg.Remove(0, 12)
            If arg.ToLower.StartsWith("/message:") Then strMessage = arg.Remove(0, 9)

        Next

        ' Exit out if options arent passed, otherwise proceed and initalise API connection.

        If Len(strDestinationNumber) = 0 Or Len(strUserName) = 0 Or Len(strPassword) = 0 Or Len(strOriginator) = 0 Or Len(strMessage) = 0 Then
            Console.WriteLine(strUsageMsg & vbCrLf & vbCrLf & "Error:" & vbCrLf & "All parameters are mandatory.")
            Environment.ExitCode = 1
            Exit Sub
        End If


        Dim cpLatestBrief = New SendSmsService()

        ' Define destination number(s) from 
        ' Create an array to pass for the destination number(s) - this code supports one number for simplicity
        Dim destarr As String() = New String(0) {}


        If strDestinationNumber.Substring(0, 1) = 0 Then
            ' Number begins 0 - strip 0 and add country code (UK Specific) - Not sure if this is required, but the
            ' example code specified the number in the 447733333333 format, ie drop the leading 0 and shove in 44. 
            destarr(0) = "44" & strDestinationNumber.Substring(1, strDestinationNumber.Length - 1)
        End If

        ' create authentication object with AQL Username and Password
        cpLatestBrief.authValue = New auth()
        cpLatestBrief.authValue.username = strUserName
        cpLatestBrief.authValue.password = strPassword

        Dim creditsused As String
        Dim outdesr As String

        Dim dt As System.DateTime = DateTime.Now

        Dim cbe As SendSMS.AQLSoapGateway.callbackelement = New callbackelement()
        cbe.callbackurl = "http://url.com"
        cbe.callbacktype = SendSMS.AQLSoapGateway.callbacktypeoptions.HTTPGET

        If cpLatestBrief IsNot Nothing Then
            Try
                cpLatestBrief.SoapSendSms(destarr, strOriginator, strMessage, SendSMS.AQLSoapGateway.mtype.text, "2", dt, _
                 False, cbe, creditsused, outdesr)
                Console.WriteLine("Message Sent, " & creditsused & " credits used.")
            Catch e As SoapHeaderException
                Console.WriteLine("Name Entry Error" & vbCrLf & e.Message)
            Catch e As SoapException
                Dim node As System.Xml.XmlNode = e.Detail
                Dim errorcode As String = e.Detail.FirstChild.InnerText
                Dim errortext As String = e.Detail.FirstChild.NextSibling.InnerText

                If errorcode = "DESTERROR-INVALIDNUMBERS" Then
                    Console.WriteLine("Name Entry Error" & vbCrLf & e.Message)
                ElseIf errorcode = "MESSAGE-NOTSUPPLIED" Then
                    Console.WriteLine("Name Entry Error" & vbCrLf & e.Message)
                Else
                    Console.WriteLine("Error:" & vbCrLf & "Error Code " & errorcode & vbCrLf & e.Message & "  " & errortext)
                End If
            End Try
            Return
        Else

            Return
        End If
    End Sub
End Module