Sending SMS Through PowerShell with Telstra’s New New API

Introduction

Back in the before time, I wrote a blog post entitled “Sending SMS Through PowerShell with Telstra’s New API”. Using some PowerShell scripts I provided back then, you could have a little play with using Telstra’s SMS gateways to amuse and annoy your pals (and I imagine apply some valid business use cases in there somewhere too). Fast forward to a few weeks ago, and I received an email from somebody who had read that blog post and reached out to me to say “Hey Dan, your scripts don’t work.” – that person is 100% correct, because much like time, API’s do not stand still and given enough change and too few updates, code you were using to send stuff might just break. This is the case with the code I published back in the day, which prompts me (for that one guy, and perhaps for you too) to write this follow-up blog post, which I have creatively titled “Sending SMS Through PowerShell with Telstra’s New New API”

At the time of writing, the current version of the Telstra SMS API available to the public is v2.2.9. Let’s quickly go over the changes in the API since the olden days when I wrote the other blog with the similar title.

ThenNow
Randomly created number on each SMS sendProvision a dedicated number and send from that
Send SMS to only a single number at a timeBroadcast to up to 10 recipients
Send SMS to Australian mobiles onlyAnnoy your friends in many countries (link: https://raw.githubusercontent.com/telstra/MessagingAPI-v2/master/docs/Telstra_Messaging_API_Country_Codes.csv)
SMS OnlyMMS joins the party
1000 free messages per monthStill 1000, but come on. That’s pretty good.

Lots of new cool features. So, lets update that dusty old PowerShell and show you how you can make use of the new API.

Getting Started

The very first thing you’re going to need to do, is create an account over at https://developer.telstra.com if you don’t already have one and create an app. This is a very painless affair, all you have to do is hit the “develop” drop down box at the top, then “my apps and keys” and then hit the plus button to “create application”. Once you’ve done that, you will be issued a client key and client secret, which we will make use of in our code.
Then, you can start playing! For the purposes of this example, I’m just going to paste my client key and secret as variables within the script, if you’re doing this in an actual production environment however, you should not do that. Depending on how you end up deploying this thing will determine the best way to keep your secrets secret, but this Kloud blog by my former colleague Dave Lee might give you some clues on the best way to approach that.

Generate an Access Token

The first thing we need to do code-wise, is create an access token, we can do this easily with the following snippet:

Now, if you take a peek at the $Authorization variable (by of course just typing $Authorization) you should see something like the following:

Create a Subscription

Now that we’re authorised, we need to perform a step which is new for this updated version of the API, and that is to create a “subscription”. What’s that exactly? Well, it seems more or less that we are creating a phone number within Telstra which will become ours, but only for the next 30 days on the free tier. This is however an improvement on the olden days where we got a new number each and every time we sent a message. At this point, you can also specify a “notify URL” in your POST, what this would do is POST any responses that number receives back to the URL you specify, so if you had a fully-fledged app, you could start getting into some automated replies. We’re keeping it simple for this example. The following code snippet will help you create a subscription:

Once we execute that code, we get a subscription which consists of a destinationAddress (mobile number where you will send and receive SMS message) and an expiry date in UNIX time. This is all we need to start bouncing out messages and receiving replies, so let’s do that! Executing the following code is going to fire an SMS out in to the world

Send an SMS

The SMS is received quicker than the time it took me to lift my phone after hitting enter – not bad.

And now, of course I can reply to that message from my phone as I would any other information cat-based message, and retrieve said reply via the following PowerShell command:

Which results in the following:

Bonus Round: MMS

OK, this was just going to be an update on my original samples of sending SMS via Telstras API’s, but one of the new capabilities is MMS, so why the heck not – let’s have a play with that functionality too.

All of the authentication bits and pieces are identical, and we can send using the same subscription (mobile number) we created before, but the JSON we end up posting is structured quite differently and contains an embedded image in BASE 64. Here’s what it looks like:

This time, the message did take slightly longer to deliver to my phone. I imagine this is something I could complain to my local MP about to have rectified. Based on my experience alone, I would suggest if you’re building out any sort of application which requires a timely back and forth between your app and the user, use SMS rather than MMS, and where timeliness is irrelevant but you want to embed lovely images – well, use MMS.

Summary

To summarise, yes indeed astute reader, my old code no longer works with the Telstra SMS API, this is because we have a brand spanking new API! I hope the update hadn’t broken your automated cat fact spammer, and you enjoy updating your code to get the app back on it’s feet.

This post has provided a simple view of how you can make use of the Telstra API at no cost – PowerShell is my choice of language due to my background, but you can really write this type of thing in any old language you like. If you are interested in playing with this stuff yourself, I would encourage you to sign up over at https://dev.telstra.com/ and also check out the wealth of documentation they’ve got on this (and many other) API’s. Happy tinkering!

 

Empowering your long running PowerShell Automation Scripts with SMS/Text Notifications

18 months ago I wrote this post that detailed integrating Push Notifications into your scripts. That still works great, but does require that you have the associated Push Bullet application installed in your browser or on your devices. More recently I wrote about using Burnt Toast for Progress Dialogs’ for long running scripts. That too is all great if you are present on the host running those scripts. But what if you want something a little more native and ubiquitous? Notifications for those autonomous or long running scripts where you aren’t active on the host running them, and you don’t want the hassle of another application specific for that purpose? How about SMS/Text notifications?

This post details how to use Twilio (a virtual telco) from your PowerShell scripts to send SMS/Text alerts from your scripts so you can receive notifications like this;

Everything is on Fire.PNG

Twilio

Twilio is a virtual telco (amongst other products), that allows you to use services such as the mobile network via your application. For their SMS/TXT service they even give you a credit to get started with their service. To send SMS/TXT messages using their service from Australia each message is AU$0.0550.

Sign-up for a Twilio trial account, enable and verify your account. Take note of the following items as you will need them for your script;

  • service mobile number (initially Trial Number)
  • Account SID
  • your Auth Token

Using these pieces of information via the Twilio API we can send SMS/Text Notifications from your PowerShell scripts (well any language, but I’m showing you how with PowerShell). You can get your Service Number, Account SID and Auth Token from the Dashboard after registering for a Trial Account.

Trial Account Dashboard.PNG

To enable SMS/TXT go to the Twilio Programmable SMS Dashboard here and create a New Messaging Service.

For my use (notifications from scripts) I selected Notifications, Outbound Only.

Create New Messaging Service.PNG

Once created you will see the following on the Programmable SMS Dashboard. That’s it, you’ve activated SMS/TXT in Trial Mode.

SMS Dashboard.PNG

The Script

Here is a Send-TextNotification PowerShell Function that takes;

  • Mobile Number to send the notification to
  • Mobile Number the message is coming from
  • Message to send
  • AuthN info (Account SID and Account Token)
  • Account SID

Line 46 sends a SMS/TXT notification using my Send-TextNotification script. Update;

  • Line 32 for your Twilio Account SID
  • Line 34 for your Twilio Account Token
  • Line 40 for the mobile number to send the message to
  • Line 42 for the authorised number you verified to send from
  • Line 44 for your notification message

Summary

Using the Twilio service, my small function and a few parameters you can quickly add SMS/TXT notifications to your PowerShell scripts. Once you have it up and running I encourage you to upgrade your account and pay the few dollars for use of the service which also removes the “Sent from your Twilio trial account” text from the messages. Twilio also has a WhatsApp Notification Service.

IMG-9658.JPG

 

Follow Us!

Kloud Solutions Blog - Follow Us!