Powershell sample - Receive SMS text through an SMPP provider

The ActiveXperts SMS Component is a software development kit (SDK) to enhance an application or script with SMS or Pager functionality. SMS messages can be sent/received using a GSM modem, an SMPP provider or an HTTP compliant SMSC.


Introduction

In this example we are going to create a Powershell Script to receive SMS messages.

A subscription to an SMPP provider is required. For this demo you can send a limited number of messages through our own gateway.

Step 1: Download and install The ActiveXperts SMS Component

Download the ActiveXperts SMS Component from the ActiveXperts Download Site and start the installation. The installation guides you through the installation process.

Step 2: Create a new script

Create a new script using your favorite editor. You can simply use notepad. However, a Powershell editor is recommended, so you can browse through objects, objects properties and object functions.

You're now able to write a more advanced Powershell script to send/receive SMS using SMS Component.

Step 3: Declare and create the ActiveXperts SMS Component objects

Create a new Powershell file called DEMO.PS1.

The following code will show you how to declare and create the GSM and SMS objects. We will use the 'objGsm' object to receive the message itself. The 'objSmsMessage' object will be used to store information of the message and the 'objSmsConstants' object containes constant values releated to the SMS objects.

Insert the following line to declare and create the GSM object:

$objSmpp         = new-object -comobject AxSms.Smpp
$objMessage      = new-object -comobject AxSms.Message
$objSmsConstants = new-object -comobject AxSms.Constants

Step 5: Receive SMS messages

You can now receive SMS messages.

The following Powershell code shows how to connect to a SMPP provider and receive a SMS message:

For ($i = 1; $i -le 12; $i++)
{
  $objMessage = $objSmpp.ReceiveMessage()
  $strResult = "ReceiveMessage, result: " + $objSmpp.LastError
  write-host $strResult
  
  If ($objSmpp.LastError -eq 0)
  {
    write-host "Received FromAddress:" $objMessage.FromAddress
    write-host "Body:" $objMessage.Body  
    $i = 12
    break;
  }
  sleep 5
} 

Appendix: Full source code

Following you can find the full source code which is also included in the ActiveXperts SMS Component package.

# ***************************************************************************
#
# ActiveXperts SMS Component
#
#    Receive messages through an SMPP connection.
#    This script will listen for incoming messages for one minute.
#
# (c) Copyright ActiveXperts Software - www.activexperts.com
#
# ***************************************************************************

# ***************************************************************************
# Function GetSmsAccountInfo
# ***************************************************************************
Function GetSmsAccountInfo([ref]$strSystemID, [ref]$strPassword)
{
  $objRegistery = new-object -comobject WScript.Shell
  $objFileSystem = new-object -comobject Scripting.FileSystemObject
  $strInstallRoot = 
     $objRegistery.RegRead("HKLM\SOFTWARE\ActiveXperts\SMS Component\InstallRoot")
  
  If ($strInstallRoot -ne "")
  {
    $strAccountFile = $strInstallRoot + "\Utilities\activexperts-labs.txt"
    
    If ($objFileSystem.FileExists($strAccountFile) -ne $True)
    {
      write-host "Account details for ActiveXperts Demo gateway not found."    
      exit
    }
    else
    {
      $objFile = $objFileSystem.OpenTextFile($strAccountFile)
      $Dummy =              $objFile.ReadLine() # 1st line is a comment line; skip it
      $strSystemID.Value =  $objFile.ReadLine() # 2nd line is the SystemID
      $strPassword.Value =  $objFile.ReadLine() # 3rd line is the password
      $objFile.Close()
    } 
  }
}

# ***************************************************************************
# Function ReadInput
# ***************************************************************************
Function ReadInput($strPrompt, $strDefaultValue, $bAllowEmpty)
{ 
  $strReturn = ""  
  If ($strDefaultValue -ne "")
  {
     $strPrompt += " leave empty for " + $strDefaultValue
  }
  
  Do 
  {       
    write-host $strPrompt
    $strReturn = read-host
    
    If ($strReturn -eq "" -and $strDefaultValue -ne "")
    {
      $strReturn = $strDefaultValue
      write-host $strReturn
    }
    elseif ($strReturn -eq "" -and $bAllowEmpty -eq $True)
    {
      break
    }   
  } While ($strReturn -eq "") 
  
  write-host ""
  return $strReturn
}
# ***************************************************************************
# The script itself
# ***************************************************************************
cls

$objSmpp = new-object -comobject AxSms.Smpp
$objMessage = new-object -comobject AxSms.Message
$objSmsConstants = new-object -comobject AxSms.Constants

# Display SMS Component Version
write-host "SMS Component Version" $objSmpp.Version "; " + 
                                      "Build" $objSmpp.Build "; " +
                                      "Module" $objSmpp.Module

# Display if the ActiveXperts SMS Component is registered.
write-host "License Status:" $objSmpp.LicenseStatus
write-host ""

# Set Logfile
$objSmpp.Logfile = $env:temp + "\SMPP_sendsms_text.txt"
write-host "Log file can be found here:"
write-host $objSmpp.Logfile
write-host ""

$strSystemID = ""
$strPassword = ""
GetSmsAccountInfo ([ref]$strSystemID) ([ref]$strPassword)

# Set SMPP Server
$strServer      = ReadInput "Enter hostname of SMPP server" "smpp.activexperts-labs.com" $False

# Set SMPP ServerPort
$nPort          = ReadInput "Enter portnumber of SMPP server" "2775" $False

# Set SMPP SystemID
$strUsername    = ReadInput "Enter account systemID" $strSystemID $False 

# Set SMPP Password
$strPassword    = ReadInput "Enter account password" $strPassword $False

# Set your phonenumber
$strPhoneNumber = ReadInput "Enter your PhoneNumber start with + and your areacode" "" $False
$strPhoneNumber = "S" + $strPhoneNumber

$nTimeout = 2000

# Connect to SMPP provider
$objSmpp.Connect($strServer, $nPort, $nTimeout)
$strResult = "Connect, result: " + $objSmpp.LastError
write-host $strResult

If ($objSmpp.LastError -ne 0)
{
  exit
}

$objSmpp.Bind($objSmsConstants.SMPP_BIND_TRANSCEIVER, $strUsername, $strpassword, "", 
              $objSmsConstants.SMPP_VERSION_34, 0, 0, $strPhoneNumber, $nTimeout)
$strResult = "Bind, result: " + $objSmpp.LastError
write-host $strResult

If ($objSmpp.LastError -ne 0)
{
  $objSmpp.Disconnect
  exit
}

write-host "Connected to provider"
write-host "Send an SMS message to the SMPP gateway (Mobile number: +31625380808) and click OK"
write-host "This demo will check for new messages during 60 seconds."

For ($i = 1; $i -le 12; $i++)
{
  $objMessage = $objSmpp.ReceiveMessage()
  $strResult = "ReceiveMessage, result: " + $objSmpp.LastError
  write-host $strResult
  
  If ($objSmpp.LastError -eq 0)
  {
    write-host "Received FromAddress:" $objMessage.FromAddress
    write-host "Body:" $objMessage.Body  
    $i = 12
    break;
  }
  sleep 5
} 

$objSmpp.Unbind()
$objSmpp.Disconnect()

write-host "Disconnected"
write-host "Ready."

You can download the full source code of this project from the ActiveXperts FTP site: ftp.activexperts-labs.com/samples/sms-component. There are many other working samples included with the product or on the FTP site.

NOTE: Demo Projects are created with Microsoft Visual Studio 2008

The ActiveXperts SMS Component project ships with a set of Microsoft Visual Studio .NET samples. The projects are created with Microsoft Visual Studio 2008.

Users with a later version of Microsoft Visual Studio can open such a project. The Visual Studio Conversion Wizard will guide you through the process of converting the project to the version used.