Integrating 2-way SMS with an XML based horoscope service

Integrating 2-way SMS with an XML based horoscope service SearchSearch
Author Message
Nelson
New member
Username: Nelson

Post Number: 2
Registered: 01-2006
Posted on Friday, March 10, 2006 - 04:28 pm:   

Bryce,

I want to integrate NowSMS with an XML based horoscope service using 2-way SMS.

The way that the service works, I need to issue an HTTP GET request with the user's phone number and date of birth (presumably the user would supply this when they send in the SMS).

The service then returns an XML response, and I need to parse the XML for the text of the horoscope to be sent back via SMS.

XML-based information services like this seem to becoming more common. But I don't understand how it could be possible to use the 2-way SMS facility in NowSMS to integrate with such a service.

You can find more information about the horoscope service that I am looking at:

http://astroworld.net/Content.Service/urls.asp

You'll notice that they have examples for returning horoscope and other content in either an SMS or MMS format. I'd be happy to just figure out how to do this for SMS ... but MMS would be a bonus.

On the web site, they give an example URL of:

http://astroworld.net/Content.Service/CONSUMERNAME/?sname=DailyHoroscope&msisdn= 44123456789&lng=ENG&type=SMS&dob=07091962&opid=T-Mobile

This URL won't actually work, because you need to be provisioned with a "CONSUMERNAME" and an "opid" value.

But on their examples page, this URL is remapped to http://astroworld.net/Content.Service/Samples/SMS_DailyHoroscope.xml, which is an example of the type of XML response that they return for an SMS horoscope.

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<AstroWorldContent>
<response>
<timestamp>2006-01-16 07:45:17</timestamp>
<service>
<name>dailyhoroscope</name>
<type>SMS</type>
<consumer>CONSUMERNAME</consumer>
<opid>T-Mobile</opid>
<user>
<msisdn>44123456789</msisdn>
<language>ENG</language>
<dof>2006-01-17 10:45:00</dof>
</user>
</service>
<content>
<element scope="message">
<content_type>text/plain</content_type>
<content_length>135</content_length>
<data access="text">Make social life and having fun your top priority today. Feedback from a friend could make you see a problem from a positive new angle.</data>
</element>
</content>
</response>
</AstroWorldContent>

I basically would just need the "data" element from the XML response to send back as SMS. But I can't see how this can be done with the 2-way facility in NowSMS.

Can you help?

Nelson
Bryce Norwood - NowSMS Support
Board Administrator
Username: Bryce

Post Number: 5626
Registered: 10-2002
Posted on Friday, March 10, 2006 - 05:39 pm:   

Hi Nelson,

So much for giving me an easy Friday afternoon to ease into the weekend.

I'm not much of a horoscope man myself, but I agree with your point that many more information services are XML based. And I think that makes this topic even more interesting.

So, let's take a closer look.

NowSMS won't parse data from an XML response for sending a 2-way SMS response. In other words, if you configured a 2-way SMS command that connected directly to that service's URL, the returned SMS response would be the raw XML. (Or actually, it would be an error message indicating that the response content type is incorrect, as NowSMS expects "text/plain" ... not "text/xml".)

However, it is a rather easy exercise to write a script that performs the XML parsing. NowSMS would interface to this script via the 2-way facility, and the script would interface to the XML-based service.

This script would need to run on a separate web server.

Based upon your example site above, here's a sample ASP script. Let's call it XMLPARSE.ASP:

<%
Response.ContentType = "text/plain"

If Request.QueryString("msisdn") = "" Then
Response.Write ("ERROR : 'msisdn' parameter missing!")
Else If (Request.QueryString("dob") = "") Then
Response.Write ("ERROR : 'dob' parameter missing!")
Else
Set xml = Server.CreateObject("Microsoft.XMLHTTP")

' Real URL commented out, and sample URL used instead
' xml.Open "GET", "http://astroworld.net/Content.Service/CONSUMERNAME/?sname=DailyHoroscope&msisdn =" &
' Server.URLencode(Request.QueryString("msisdn")) & "&lng=eng&type=SMS&dob=" &
' Server.URLencode(Request.QueryString("dob")) & "&opid=T-Mobile", False

xml.Open "GET", "http://astroworld.net/Content.Service/Samples/SMS_DailyHoroscope.xml", False

xml.Send

Dim xmldom

Set xmldom=Server.CreateObject("Microsoft.XMLDOM")
xmldom.loadXML (xml.ResponseText)

If xmldom.parseError.errorCode <> 0 Then
Response.Write ("Error parsing XML response")
Else
Response.Write(xmldom.getElementsByTagName("data").item(0).text)
End If

End If
End If

%>

Now I'm not an ASP expert. I basically just put the above example together by doing some web searches for examples on how to issue an HTTP request to a remote server and parse XML.

To test this script with NowSMS, I installed the script on an IIS web server that is on the same local network as the NowSMS server. This particular IIS web server has an IP address of 192.168.1.10.

In NowSMS, I configured a 2-way command like this:

"SMS Command Prefix" = Horoscope
"Command to Execute" = http://192.168.1.10/xmlparse.asp?msisdn=@@SENDER@@&dob=@@SMS@@

This 2-way command assumes that to request a horoscope, the user will send in an SMS with the text "Horoscope" followed by their date of birth.

The xmlparse.asp script looks for "msisdn" and "dob" parameters. In this simple example, we are assuming that the inbound SMS has a valid date of birth in the text of the message. But this script could be enhanced to perform further validation of input parameters, and to take different actions based upon the text of the message.

The xmlparse.asp script then connects to the URL at the service provider, passing the "msisdn" and "dob" to the service provider script. (Note that we use "Server.URLencode" to ensure that the parameters are properly URL escaped.)

In this case, I've commented out the actual URL, and this script always connects to the example URL for testing purposes.

ASP has the ability to parse an XML response, and we extract the text of the "data" element, returning it back to the subscriber.

This should just be considered a starting point for the effort. Further script development, and especially error handling, would be required for deployment.

As you can see, it's relatively simple to parse an XML response in ASP. So while this initially seemed to be a complex task, in the end, it only took about a 20 line script to accomplish the task.

Have a good weekend.

-bn
Bryce Norwood - NowSMS Support
Board Administrator
Username: Bryce

Post Number: 5627
Registered: 10-2002
Posted on Friday, March 10, 2006 - 06:22 pm:   

Ok. I can't quit quite yet. This particular service also has the ability to return an MMS response. And I think it would be cool to show how a 2-way SMS into NowSMS could return an MMS response from the service.

Here's an example of the XML response for MMS content from this particular service provider:

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<AstroWorldContent>
<response>
<timestamp>2006-01-14 20:09:49</timestamp>
<service>
<name>dailyhoroscope</name>
<type>MMS</type>
<consumer>CONSUMERNAME</consumer>
<opid>T-Mobile</opid>
<user>
<msisdn>44123456789</msisdn>
<language>ENG</language>
<dof>2006-01-15 17:09:00</dof>
</user>
</service>
<content>
<element scope="message">
<content_type>text/plain</content_type>
<content_length>343</content_length>
<data access="text">You have extra insight into what is happening in a close relationship today. In fact, you can read others like an open book, so trust your judgement. If you set the scene and draw somebody out, you could discover something that you badly need to know. This is also a good day to plan domestic changes. A certain situation is not cast in stone.</data>
</element>
<element scope="image">
<content_type>image/jpeg</content_type>
<image_size>160x90</image_size>
<data access="url">http://astroworld.net/Content.Service/img/p_02_p_10.jpg</data>
</element>
</content>
</response>
</AstroWorldContent>

What we see here is 2 instances of the "data" element. The first instance contains text, and the second contains a URL for an image.

We can easily extract the text and the URL in our script. And then we can submit a request back to NowSMS to send an MMS message with that text and the image from the URL.

So here's another example, let's call this one XMLPARSEMMS.ASP:

<%
Response.ContentType = "text/plain"

If Request.QueryString("msisdn") = "" Then
Response.Write ("ERROR : 'msisdn' parameter missing!")
Else If (Request.QueryString("dob") = "") Then
Response.Write ("ERROR : 'dob' parameter missing!")
Else
Set xml = Server.CreateObject("Microsoft.XMLHTTP")

' Real URL commented out, and sample URL used instead
' xml.Open "GET", "http://astroworld.net/Content.Service/CONSUMERNAME/?sname=DailyHoroscope&msisdn =" &
' Server.URLencode(Request.QueryString("msisdn")) & "&lng=eng&type=MMS&dob=" &
' Server.URLencode(Request.QueryString("dob")) & "&opid=T-Mobile", False

xml.Open "GET", "http://astroworld.net/Content.Service/Samples/MMS_DailyHoroscope.xml", False

xml.Send

Dim xmldom

Set xmldom=Server.CreateObject("Microsoft.XMLDOM")
xmldom.loadXML (xml.ResponseText)

If xmldom.parseError.errorCode <> 0 Then
Response.Write ("Error parsing XML response")
Else
Set nowsms = Server.CreateObject("Microsoft.XMLHTTP")
nowsms.Open "GET", "http://127.0.0.1:8800/?user=insertuser&password=insertpassword&mmsfrom=horoscop es@nowsms.com&PhoneNumber=" & Server.URLencode(Request.QueryString("msisdn")) & "&MMSSubject=Daily+Horoscope&MMSText=" & Server.URLencode(xmldom.getElementsByTagName("data").item(0).text) & "&MMSFILE=" & Server.URLencode(xmldom.getElementsByTagName("data").item(1).text), False
nowsms.Send
End If

End If
End If

%>

This script is very similar to the first. But rather than returning a text response, we're extracting the content for the MMS message, and then the script is issuing a request to NowSMS to send an MMS message with that content.

You need to substitute in an IP address or host name for your NowSMS server, and you need to insert a username and password for that server into the URL.

Cheers,

-bn
Redmound
New member
Username: Kobsan

Post Number: 1
Registered: 03-2006
Posted on Saturday, March 18, 2006 - 01:08 am:   

I used your script to test and it worked, but when i remove the sample url and use the one they gave me. I get the Error Parsing XML REQUEST.

Can you help me solve this problem.
Bryce Norwood - NowSMS Support
Board Administrator
Username: Bryce

Post Number: 5686
Registered: 10-2002
Posted on Tuesday, March 21, 2006 - 05:50 pm:   

Do you mean "Error parsing XML response?"

What is the XML response that you get back with a live request?

What if you type the URL request directly into a web browser?

Try putting Response.Write (xml.ResponseText) into the script, and that will show the response that is coming back from the service provider.

What is that response?

Maybe there is an error with one of the parameters in the URL request?

-bn