How To: Send SMS Messages to Logentries in Under 5 Minutes (Maybe 10)
The “Internet Of Things” continues to be talked about a lot with an increasing number of devices now containing some sort of smart functionality which can be interacted with.
Join the DZone community and get the full member experience.
Join For FreeThe “Internet Of Things” continues to be talked about a lot with an increasing number of devices now containing some sort of smart functionality which can be interacted with. Here’s a great article about end-to-end IoT monitoring by colleague David Tracey.
However, not all IoT devices can be in locations with WiFi or 3/4G coverage, so they can not easily (or at all) send or receive data over the internet, and instead rely on standard cellular connectivity – or the devices may simply not need to send data frequently enough to need an internet connection. This is where the ability to send SMS messages to alert you of any issues can be useful.
Luckily, this can be done very easily using a service like Twilio, which provides a rich set of APIs that allow you to (among other things) send and receive text messages.
I wanted to try this out, and found that it was really quick to configure Twilio to receive SMS messages to a virtual number (availability does depend on your region), and then have any messages relayed on to Logentries.
So What Do You Need?
The first thing is the token for the Log file you want to write to (Don’t yet have a Logentries account? You can sign up for a free trial, it’s really quick).
Either create a new log using the “manual” option and take a note of the newly created token, or if you want to write to an existing log then browse to the Logset and click on the “token” link for the log. A modal will appear with the log token.
Sign up for your free Twilio trial. To receive SMS messages, you’ll need to add some funds to your account.
Browse to the “messaging / phone numbers” area. Select a phone number in the region you want – remember to ensure it has the “SMS” service available.
You’ll now be prompted to configure it. The bit we’re interested in is the Messaging section. Select “URL” and enter the following value into the Request URL:
https://js.logentries.com/v1/noformat/<Your Log Token>
Make sure that HTTP POST is selected, and then press Save – and that’s pretty much all your configuration done.
Let’s send a message:
And check the inbound messages in Twilio – the message has been received.
And now let’s check my log in Logentries
Note that it is not possible to format the request we get from Twilio – everything is URL encoded. This isn’t a big deal, as the message has Key Value Pairs that can be searched for in Logentries. If you want, you can write your own Twilio app that can accept the message, format it and then send it on to Logentries.
The messages look good – I can create an alert based on the message body, but there’s one small problem. The From number has the leading “+” encoded, which means that it can’t be searched for using a KVP search.
This is where Regular Expressions come in handy. The named capture group below allows me to group the incoming SMS messages by the number that sent them in.
where(/From=%2B(?P<FromNum>\d*)/) groupby(FromNum) sort(desc)
When I run this query, I get the number of SMS messages grouped by the number that sent them in – thereby identifying the devices that triggered the messages.
Hopefully, this simple demo shows that you don’t need to depend on all your devices having internet access in order to send log messages to Logentries.
Published at DZone with permission of Eoin Shanley. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments