Thursday, October 29, 2015

Output SMS from Powershell (RedOxygen)

I have been completing some investigatory work around SMS gateways recently, specifically for our new contact centre suite to contact our customers.

I came across a SaaS based gateway product called RedOxygen.  They have a number of methods of sending SMS messages, ranging from an Office plugin, to a documented API.

My interest was the latter option as this is what we plan to use.  You can sign up for a free account which gives you 25 free messages (very useful for testing).  There is an interface option for HTTP GET and POST to send your messages which is easy to combine with lots of programming/scripting languages.

I am not a programmer or developer but I have worked with Powershell before, so I set myself the challenge of creating a little GUI which will take input from a user to type a telephone number and a message which upon hitting a button, would send the message to a mobile phone.  I also wanted it to display a return result to inform the user if there was a problem.
Here is the result.

[system.reflection.assembly]::LoadWithPartialName( "System.Windows.Forms")
[system.reflection.assembly]::LoadWithPartialName("System.Drawing")

#if like me you have a authenticated proxy server in the way
$global:PSDefaultParameterValues = @{
        'Invoke-WebRequest:Proxy'='http://proxy:8080'
        '*:ProxyUseDefaultCredentials'=$true}

#Fill in RedOxygen account details here.
$AccountId='accountnumber'
$emailID='emailaddress'
$password='password'

#This function will send the SMS
Function SendText
{
$global:SMSresultlabel.text = ""

#Retrieve SMS message from textbox
$global:SMS = $smsinput.text
    
#Retrieve Telephone number and remove trailing 0
$global:number = $numberinput.text -replace '^0'

#build POST data
$POSTDATA = @"
[SENDER]
ID=$AccountID

[FROM]
Email=$emailID
PW=$password

[1]
Number=$number
Message=$sms
"@

#Run the HTTP POST command and return result
$global:result = Invoke-WebRequest -Uri https://www.redoxygen.net/sms.dll?Action=SendSMS -Method POST -Body $POSTDATA | select-object -ExpandProperty RawContent

#Display result in variable
$global:SMSresultlabel.text = $result
}

# General Form option
$form = New-Object Windows.Forms.Form
$Form.Size = New-Object System.Drawing.Size 400,500 
$form.text = "SMS Sender"

# This is a simple infolabel for Telephone Number
$Telinfolabel = New-Object Windows.Forms.label
$Telinfolabel.Location = New-Object Drawing.Point 50,30
$Telinfolabel.Size = New-Object Drawing.Point 300,20
$Telinfolabel.text = "Enter the telephone number below"

# This is where an user types Telephone Number
$numberinput = New-Object Windows.Forms.textbox
$numberinput.Location = New-Object Drawing.Point 50,60
$numberinput.Size = New-Object Drawing.Point 200,30

# This is a simple infolabel for SMS message
$SMSinfolabel = New-Object Windows.Forms.label
$SMSinfolabel.Location = New-Object Drawing.Point 50,90
$SMSinfolabel.Size = New-Object Drawing.Point 300,20
$SMSinfolabel.text = "Enter the message below"

# This is where an admin types in the SMS message
$SMSinput = New-Object Windows.Forms.textbox
$SMSinput.Location = New-Object Drawing.Point 50,120
$SMSinput.Size = New-Object Drawing.Point 200,200
$SMSinput.Add_KeyDown({if ($_.KeyCode -eq "Enter") 
    {SendText}})

# This button is used to run the SendText function
$Go1button = New-Object Windows.Forms.Button
$Go1button.text = "Go!"
$Go1button.Location = New-Object Drawing.Point 260,60
$Go1button.Add_Click({SendText})

# This is a simple infolabel for SMS result
$SMSresultlabel = New-Object Windows.Forms.label
$SMSresultlabel.Location = New-Object Drawing.Point 50,200
$SMSresultlabel.Size = New-Object Drawing.Point 300,200

# Add the controls to the Form
$form.controls.add($Go1button)
$form.controls.add($Telinfolabel)
$form.controls.add($SMSinfolabel)
$form.controls.add($SMSinput)
$form.controls.add($numberinput)
$form.controls.add($MsgBox)
$form.controls.add($SMSresultlabel)

# Puts focus in the text box on launch
$Form.Add_Shown({$Form.Activate(); $numberinput.focus()})

# Display the dialog
$form.ShowDialog()
}

Here is how it looks
Successful text

Text Received on my phone

Unsuccessful Text

Clearly the powershell code above could be improved upon with number validation and improved feedback rather than the RawContent from the Invoke-WebRequest.  The main thing I wanted to do is prove how easy you can send SMS's from Powershell.

I guess this might be useful for those weekend scripts or if certain systems fail.

Have fun with it.