SMS Messaging Server Temperature per City
Date added: April 5, 2012

Authors:

Content:

1. Background

Company Data Graber BV wanted to easily get the temperature in degrees Celsius and degrees Fahrenheit of a given city.

2. Goals of the new System

You send a sms with the city you want to know the temperature from.
The server will send you a sms back with the current degrees Celsius and degrees Fahrenheit of the given city.

Example 1:
Send SMS: 'Tokyo'
Reply : 'The current temperature in Tokyo is 15 degrees Celsius'

Example 2:
Send SMS: 'Amsterdam'
Reply : 'The current temperature in Amsterdam is 5 degrees Celsius'

3. ActiveXperts SMS Messaging Server Solution

The Trigger will read out the sms you send, with that information it will send a request to the Google server with the given city.

The server of Google will give some information back in XML code. In this block of code are many things located, like wind condition, Humidity, Fahrenheit and more.

With some XPath code the trigger will retrieve the current temperature in degrees Celsius and degrees Fahrenheit. With the information obtained from the XPath code, the trigger will create a sms.

In this sms are the city and the temperature in degrees Celsius and degrees Fahrenheit located.

The XML Code received from the Google server:
<xml_api_reply version="1">
    <weather module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0">
        <forecast_information>
            <city data="Amsterdam, North Holland"/>
            <postal_code data="Amsterdam"/>
            <latitude_e6 data=""/>
            <longitude_e6 data=""/>
            <forecast_date data="2012-04-11"/>
            <current_date_time data="1970-01-01 00:00:00 +0000"/>
            <unit_system data="US"/>
        </forecast_information>
        <current_conditions>
            <condition data="Partly Cloudy"/>
            <temp_f data="48"/>
            <temp_c data="9"/>
            <humidity data="Humidity: 76%"/>
            <icon data="/ig/images/weather/partly_cloudy.gif"/>
            <wind_condition data="Wind: SW at 16 mph"/>
        </current_conditions>
        <forecast_conditions>
            <day_of_week data="Wed"/>
            <low data="39"/>
            <high data="63"/>
            <icon data="/ig/images/weather/fog.gif"/>
            <condition data="Fog"/>
        </forecast_conditions>
        <forecast_conditions>
            <day_of_week data="Thu"/>
            <low data="41"/>
            <high data="50"/>
            <icon data="/ig/images/weather/chance_of_rain.gif"/>
            <condition data="Chance of Rain"/>
        </forecast_conditions>
        <forecast_conditions>
            <day_of_week data="Fri"/>
            <low data="36"/>
            <high data="55"/>
            <icon data="/ig/images/weather/chance_of_rain.gif"/>
            <condition data="Chance of Rain"/>
        </forecast_conditions>
        <forecast_conditions>
            <day_of_week data="Sat"/>
            <low data="41"/>
            <high data="52"/>
            <icon data="/ig/images/weather/fog.gif"/>
            <condition data="Fog"/>
        </forecast_conditions>
    </weather>
</xml_api_reply>

4. Source code

Small snapshot of source code:

' // ========================================================================
' // My Database Query Project.vbs
' // ------------------------------------------------------------------------
' // 
' // 
' // ========================================================================

Option Explicit

CONST STR_DEBUGFILE = _
"C:\Program Files (x86)\ActiveXperts\SMS Messaging Server\Sys\Tmp\My Database Query Project.txt"

' Declaration of global objects
Dim g_objMessageDB, g_objDebugger, g_objConstants

' Creation of global objects
Set g_objConstants      = CreateObject( "AxMmServer.Constants" )
Set g_objMessageDB      = CreateObject( "AxMmServer.MessageDB" ) 
Set g_objDebugger       = CreateObject( "ActiveXperts.VbDebugger" )

' Set Debug file - for troubleshooting purposes
g_objDebugger.DebugFile = STR_DEBUGFILE
g_objDebugger.Enabled   = False


' // ========================================================================
' // Function: ProcessMessage
' // ------------------------------------------------------------------------
' // ProcessMessage trigger function to process incoming messages
' // ========================================================================

Function ProcessMessage( numMessageID )
   Dim objMessageIn, objMessageOut

   g_objDebugger.WriteLine ">> ProcessMessage"

   ' Open the Message Database
   g_objMessageDB.Open
   If( g_objMessageDB.LastError <> 0 ) Then
      g_objDebugger.WriteLine "<< ProcessMessage,  unable to open database"
      Exit Function
   End If

   ' Retrieve the message that has just been received. If it fails then exit script 
   Set objMessageIn   = g_objMessageDB.FindFirstMessage( "ID = " & numMessageID ) 
   If g_objMessageDB.LastError <> 0 Then
      g_objMessageDB.Close
      g_objDebugger.WriteLine "<< ProcessMessage, FindFirstMessage failed, "&_
      "error: [" & g_objMessageDB.LastError & "]"
      Exit Function
   End If

   ' Change Status to from Pending to Success. If you don't do it, the message will be processed 
   ' by subsequent triggers (if defined) because message is still pending
   objMessageIn.StatusID = g_objConstants.MESSAGESTATUS_SUCCESS
   g_objMessageDB.Save objMessageIn   

   g_objDebugger.WriteLine "Incoming message saved, result: [" & g_objMessageDB.LastError & "]"

   ReplyMessage ( objMessageIn )
 
   ' Close the Message Database
   g_objMessageDB.Close

   g_objDebugger.WriteLine "<< ProcessMessage"

End Function


' // ========================================================================
' // ReplyMessage
' // ------------------------------------------------------------------------
' // Auto reply to every incoming SMS message
' // ========================================================================

Function ReplyMessage( objMessageIn )
   Dim objMessageOut
   Dim strResponse
   Dim strTempCelcius
   Dim temperature
   
   temperature = GetTemprature(objMessageIn.Body)

   If (temperature = "") Then
      strResponse = "The City you send doesn't exists"
   Else
      strResponse = "The current temperature in " & objMessageIn.Body &_ 
      " is: " & temperature & " degrees celsius."
   End If
   g_objDebugger.WriteLine ">> ReplyMessage: " & strResponse

   Set objMessageOut = g_objMessageDB.Create
   If( g_objMessageDB.LastError = 0 ) Then
     objMessageOut.DirectionID  = g_objConstants.MESSAGEDIRECTION_OUT
     objMessageOut.StatusID     = g_objConstants.MESSAGESTATUS_PENDING
     objMessageOut.TypeID       = objMessageIn.TypeID 
     objMessageOut.ToAddress    = objMessageIn.FromAddress
     objMessageOut.ChannelID    = 0
     objMessageOut.BodyFormatID = objMessageIn.BodyFormatID
     objMessageOut.Body         = strResponse
     g_objMessageDB.Save objMessageOut
   End If

  g_objDebugger.WriteLine "<< ReplyMessage"
End Function

' // ========================================================================
' // Function: GetTemprature
' // ------------------------------------------------------------------------
' // Get the temprature from the given city
' // ========================================================================
Function GetTemprature(strCity)
  Dim sContent, strXPath, objHttpEx, objXMLDoc, temp, temprature

  ' Create a HTTP instance
  Set objHttpEx = CreateObject("ActiveXperts.HttpEx")

  ' Get and Set sContent from Website
  sContent = objHttpEx.Get("http://www.google.com/ig/api?weather=" & strCity & "=en")

  ' If objHttpEx.Get gives an error
  If objHttpEx.LastError <> 0 Then
    WScript.Echo "Failed, error: " & objHttpEx.LastError & "; " &_
    objHttpEx.GetErrorDescription(objHttpEx.LastError)
    WScript.Quit
  End If

  ' Create Object XMLDOM
  Set objXMLDoc = CreateObject("Microsoft.XMLDOM")
  objXMLDoc.async = False
  ' Set Language to XPath
  objXMLDoc.setProperty "SelectionLanguage", "XPath"	
  ' Load XML File
  objXMLDoc.loadXML(sContent)

  ' Set strXPath to the path where the degrees celsius is located
  strXPath = "/xml_api_reply/weather/current_conditions/temp_c"

  ' Set temp to Single selected node located in strXPath
  On Error Resume Next
  Set temp = objXMLDoc.documentElement.selectSingleNode(strXPath)

  If (Err <> 0) Then
    WScript.Echo Err.number & ": " & Err.description
    WScript.Quit
  End If

  If temp is Nothing Then
    WScript.Echo "Incorrect URL"
  Else
    GetTemprature = temp.GetAttribute("data")
  End If
End Function