The Pull Method

The pull method is essentially where you poll Esendex to see if there are any new Messages in your Inbox: below is an example of how to do it.


function processForm()
on error resume next

'Setup the service object
dim inboxService
set inboxService = Server.CreateObject("Esendex.InboxService.2") 

'Error Handling
if err.number <> 0 then
processForm = "Unable to create Esendex.InboxService.2 object. Ensure COM object is registered. Error description: " + err.Description 
exit function
end if

'Setup the send service object
inboxService.Initialise Request.Form("Username"), Request.Form("Password"), Request.Form("Account")

'Attempt to retrieve the messages and handle any errors
dim messageArray
set messageArray = inboxService.GetMessages( )

if err.number <> 0 then
'Display the generated error
processForm = "Unable to retrieve messages: " + err.Description

on error goto 0
exit function
else
'Do something with the returned messages
Dim index
processForm = Cstr(messageArray.Count) + " messages were returned." + vbcrlf
for index = 1 TO messageArray.Count
processForm = processform + messageArray(index).body + vbcrlf
next

on error goto 0
exit function
end if

end function

As with the other examples you have to first create an instance of an Esendex service, this time it is the InboxService. Then you need to initialise the inbox with your account details.


'Setup the send service object
inboxService.Initialise Request.Form("Username"), Request.Form("Password"), Request.Form("Account")

Now you can use the methods available on the Inbox Service class to receive Messages. When you receive a Message you get back a Message object as detailed here Receive SMS. Simply declare a Message array object and assign it to the return value of the GetMessages method.


dim messageArray
set messageArray = inboxService.GetMessages( )

Now you have an array of the Messages from your Inbox you can loop through the Messages and perform the appropriate actions for your application, for the purposes of this example the Message Bodies are simply output to the screen using a variable called processForm.


processForm = Cstr(messageArray.Count) + " messages were returned." + vbcrlf
for index = 1 TO messageArray.Count
processForm = processform + messageArray(index).body + vbcrlf
next

One of the limitations of this method is that you can only get the first 50 Messages in your Inbox. Because of this, once you have downloaded a Batch of Messages and have stored them on your server or processed them in whatever way your business requires it is recommended that you delete them in order to get the next batch of Messages. The following code sample shows how to do this.


'Generate a comma separated list of the ID's ready to use when deleting them
Dim index
Dim messageIDs
processForm = Cstr(messageArray.Count) + " messages were returned." + vbcrlf
for index = 1 TO messageArray.Count
'Comma separate the message id's
if index > 1 then
messageIDs = messageIDs + ","
end if

messageIDs = CStr(messageArray(index).id)
processForm = processform + messageArray(index).body + vbcrlf
next

'Now attempt to delete the messages that were retrieved
inboxService.DeleteMessages messageIDs

The DeleteMessages method on the Inbox Service requires a comma separated list of Message Ids which is created from the Message array returned by the GetMessages method. Then you simply call DeleteMessages with this list as a parameter and you will delete the Messages from your Esendex Inbox. Now when you call GetMessages you will get the next 50 Messages in your inbox.