For those of you not familiar with it, Nuntium is an open-source tool we developed together with InSTEDD for easily building applications that rely on SMS for communication. Even though several other kinds of channels are supported, such as email or twitter, text messaging has been one of its most important features.
A common scenario that we face is how to easily test the flow of text messages in a development or manual testing environment. While using a local gateway is a possibility, most likely you won't want to spend actual text messages while developing; it is costly in the long run, and cumbersome to switch between the development station and a test cellphone.
One possibility to get around this is to use Nuntium's very interface for generating AT messages, and check the AO queue for responses.
This interface can be a bit unsuited, however, for sending messages and receiving their responses instantly on the same page. It also requires access to the Nuntium account, so if you are looking forward to using it for testing, it implies granting access to everyone who is to test it to the account. An elegant solution, provided by my colleague Adrian Romero, is to use an XMPP channel for mocking the SMS flow.
Setting up the XMPP channel
Step one is to create a new account that you will use for testing the application and an application within the account. Now you are ready to set up the XMPP channel. For this, you will need an XMPP account to act as the channel; creating a test account in gmail is always a good candidate, in which case configuration is pretty straightforward:
Make sure you change the protocol of the channel from XMPP to SMS. This will make Nuntium use this XMPP channel for routing SMS messages.
Configuring AT Rules
The trickiest part is setting up the message rules for mocking the SMS channel. The main challenge is that you want to simulate, from a single xmpp account, multiple SMS phones. In other words, you want to use your user@gmail.com account for pretending you are sending a text message from 555-1000, but also from 555-2000, 555-3000, and so on.
A nice solution to this is to actually specify the phone number as part of the message, and have Nuntium interpret it accordingly. So, if we send the message:
5551000 Hello world
We want Nuntium to convert it to a text message from sms://5551000 with the content "Hello world". To do this, we need to set up the following AT rules:
Condition: Body regex (d+)(s+)(.+) Action: From sms://${body.1} Body ${body.3}
Let's take a look at them more closely:
- The condition captures the number at the beginning in the message in a regex group, and leaves the rest of the message in another one. In our example, it would be 5551000 in the first group, and Hello world in the thrid one.
- The first action forces the from field to be equal to the phone number; for this, it uses the ${field.group} syntax. In this case, ${body.1} tells Nuntium to use the first capturing group from the regex matched on the body field, 5551000 in our example.
- The second action removes the phone number from the body, leaving only Hello world in our example.
Having these rules in place, we can use a single channel, xmpp in this case, to fake that we are multiple different senders.
Configuring AO Rules
Next step is to setup the AO rules now. Nuntium, instructed by your application, will attempt to send messages to phone numbers, and your xmpp channel will have a difficult time trying to send an IM to sms://5552000. So we want to divert these messages to our test jabber account, but still letting us know who was the original recipient for testing purposes.
So, if the application is trying to send a Hello user! message to sms://5552000, we want a message delivered to our jabber username@gmail.com account with the content:
To: 5552000 Message: Hello user!
To do this, we will set up the following set of rules that affect the To and the Body fields of the application originated message.
Condition: To regex sms://(.+) Body regex (.*) Actions: To username@gmail.com Body To: ${to.1} Message: ${body.1}
These rules are simpler than the previous ones. The conditions simply extract the phone number and body of the message; whereas the actions replace the target of the message with a hardcoded one, and condense the original target and the content in the body of the new message.
To sum up...
Nuntium offers greats flexibility by the usage of AT and AO rules, by granting the possibility to alter the flow of messages or their content. These tools, originally designed for connecting systems in real-world situations, can be also used for simplifying the development of a Nuntium-based application.